Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix openapi:generate Meta to affect required validations #3452

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion http/codegen/openapi/json_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,14 @@ func initAttributeValidation(s *Schema, at *expr.AttributeExpr) {
s.MaxLength = val.MaxLength
}
}
s.Required = val.Required
for _, v := range val.Required {
if a := at.Find(v); a != nil {
if !mustGenerate(a.Meta) {
continue
}
}
s.Required = append(s.Required, v)
}
}

// toSchemaHrefs produces hrefs that replace the path wildcards with JSON
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"swagger":"2.0","info":{"title":"","version":"0.0.1"},"host":"goa.design","consumes":["application/json","application/xml","application/gob"],"produces":["application/json","application/xml","application/gob"],"paths":{"/":{"get":{"tags":["testService"],"summary":"testEndpoint testService","operationId":"testService#testEndpoint","parameters":[{"name":"TestEndpointRequestBody","in":"body","required":true,"schema":{"$ref":"#/definitions/TestServiceTestEndpointRequestBody"}}],"responses":{"200":{"description":"OK response.","schema":{"$ref":"#/definitions/TestServiceTestEndpointResponseBody"}}},"schemes":["https"]}}},"definitions":{"TestServiceTestEndpointRequestBody":{"title":"TestServiceTestEndpointRequestBody","type":"object","properties":{"string":{"type":"string","example":""}},"example":{"string":""}},"TestServiceTestEndpointResponseBody":{"title":"TestServiceTestEndpointResponseBody","type":"object","properties":{"int":{"type":"integer","example":0,"format":"int64"}},"example":{"int":0}}}}
{"swagger":"2.0","info":{"title":"","version":"0.0.1"},"host":"goa.design","consumes":["application/json","application/xml","application/gob"],"produces":["application/json","application/xml","application/gob"],"paths":{"/":{"get":{"tags":["testService"],"summary":"testEndpoint testService","operationId":"testService#testEndpoint","parameters":[{"name":"TestEndpointRequestBody","in":"body","required":true,"schema":{"$ref":"#/definitions/TestServiceTestEndpointRequestBody","required":["required_string"]}}],"responses":{"200":{"description":"OK response.","schema":{"$ref":"#/definitions/TestServiceTestEndpointResponseBody","required":["required_int"]}}},"schemes":["https"]}}},"definitions":{"TestServiceTestEndpointRequestBody":{"title":"TestServiceTestEndpointRequestBody","type":"object","properties":{"required_string":{"type":"string","example":""},"string":{"type":"string","example":""}},"example":{"required_string":"","string":""},"required":["required_string"]},"TestServiceTestEndpointResponseBody":{"title":"TestServiceTestEndpointResponseBody","type":"object","properties":{"int":{"type":"integer","example":0,"format":"int64"},"required_int":{"type":"integer","example":0,"format":"int64"}},"example":{"int":0,"required_int":0},"required":["required_int"]}}}
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,33 @@ paths:
required: true
schema:
$ref: '#/definitions/TestServiceTestEndpointRequestBody'
required:
- required_string
responses:
"200":
description: OK response.
schema:
$ref: '#/definitions/TestServiceTestEndpointResponseBody'
required:
- required_int
schemes:
- https
definitions:
TestServiceTestEndpointRequestBody:
title: TestServiceTestEndpointRequestBody
type: object
properties:
required_string:
type: string
example: ""
string:
type: string
example: ""
example:
required_string: ""
string: ""
required:
- required_string
TestServiceTestEndpointResponseBody:
title: TestServiceTestEndpointResponseBody
type: object
Expand All @@ -49,5 +59,12 @@ definitions:
type: integer
example: 0
format: int64
required_int:
type: integer
example: 0
format: int64
example:
int: 0
required_int: 0
required:
- required_int
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"openapi":"3.0.3","info":{"title":"Goa API","version":"0.0.1"},"servers":[{"url":"https://goa.design"}],"paths":{"/":{"get":{"tags":["testService"],"summary":"testEndpoint testService","operationId":"testService#testEndpoint","requestBody":{"required":true,"content":{"application/json":{"schema":{"$ref":"#/components/schemas/TestEndpointRequestBody"},"example":{"string":""}}}},"responses":{"200":{"description":"OK response.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Result"},"example":{"int":0}}}}}}}},"components":{"schemas":{"Result":{"type":"object","properties":{"int":{"type":"integer","example":0,"format":"int64"}},"example":{"int":0}},"TestEndpointRequestBody":{"type":"object","properties":{"string":{"type":"string","example":""}},"example":{"string":""}}}},"tags":[{"name":"testService"}]}
{"openapi":"3.0.3","info":{"title":"Goa API","version":"0.0.1"},"servers":[{"url":"https://goa.design"}],"paths":{"/":{"get":{"tags":["testService"],"summary":"testEndpoint testService","operationId":"testService#testEndpoint","requestBody":{"required":true,"content":{"application/json":{"schema":{"$ref":"#/components/schemas/TestEndpointRequestBody"},"example":{"required_string":"","string":""}}}},"responses":{"200":{"description":"OK response.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Result"},"example":{"int":0,"required_int":0}}}}}}}},"components":{"schemas":{"Result":{"type":"object","properties":{"int":{"type":"integer","example":0,"format":"int64"},"required_int":{"type":"integer","example":0,"format":"int64"}},"example":{"int":0,"required_int":0},"required":["required_int"]},"TestEndpointRequestBody":{"type":"object","properties":{"required_string":{"type":"string","example":""},"string":{"type":"string","example":""}},"example":{"required_string":"","string":""},"required":["required_string"]}}},"tags":[{"name":"testService"}]}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ paths:
schema:
$ref: '#/components/schemas/TestEndpointRequestBody'
example:
required_string: ""
string: ""
responses:
"200":
Expand All @@ -28,6 +29,7 @@ paths:
$ref: '#/components/schemas/Result'
example:
int: 0
required_int: 0
components:
schemas:
Result:
Expand All @@ -37,15 +39,28 @@ components:
type: integer
example: 0
format: int64
required_int:
type: integer
example: 0
format: int64
example:
int: 0
required_int: 0
required:
- required_int
TestEndpointRequestBody:
type: object
properties:
required_string:
type: string
example: ""
string:
type: string
example: ""
example:
required_string: ""
string: ""
required:
- required_string
tags:
- name: testService
9 changes: 8 additions & 1 deletion http/codegen/openapi/v3/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,14 @@ func (sf *schemafier) schemafy(attr *expr.AttributeExpr, noref ...bool) *openapi
s.MaxLength = val.MaxLength
}
}
s.Required = val.Required
for _, v := range val.Required {
if a := attr.Find(v); a != nil {
if !mustGenerate(a.Meta) {
continue
}
}
s.Required = append(s.Required, v)
}

return s
}
Expand Down
14 changes: 14 additions & 0 deletions http/codegen/testdata/openapi_dsls.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,13 @@ var NotGenerateAttributeDSL = func() {
Attribute("string", String, func() {
Example("")
})
Attribute("required_int", Int, func() {
Meta("openapi:generate", "false")
})
Attribute("required_string", String, func() {
Example("")
})
Required("required_int", "required_string")
})
var ResultT = Type("Result", func() {
Attribute("int", Int, func() {
Expand All @@ -780,6 +787,13 @@ var NotGenerateAttributeDSL = func() {
Attribute("string", String, func() {
Meta("openapi:generate", "false")
})
Attribute("required_int", Int, func() {
Example(0)
})
Attribute("required_string", String, func() {
Meta("openapi:generate", "false")
})
Required("required_int", "required_string")
})
Service("testService", func() {
Method("testEndpoint", func() {
Expand Down
Loading