From d07331da48b337e7c986b2a37c3f81f1c26b7708 Mon Sep 17 00:00:00 2001 From: Taichi Sasaki Date: Thu, 18 Jan 2024 01:00:16 +0900 Subject: [PATCH 1/2] Fix test cases about openapi:generate Meta for Attribute --- .../not-generate-attribute_file0.golden | 2 +- .../not-generate-attribute_file1.golden | 17 +++++++++++++++++ .../golden/not-generate-attribute_file0.golden | 2 +- .../golden/not-generate-attribute_file1.golden | 15 +++++++++++++++ http/codegen/testdata/openapi_dsls.go | 14 ++++++++++++++ 5 files changed, 48 insertions(+), 2 deletions(-) diff --git a/http/codegen/openapi/v2/testdata/TestSections/not-generate-attribute_file0.golden b/http/codegen/openapi/v2/testdata/TestSections/not-generate-attribute_file0.golden index 5a1ac82f06..735354eb38 100644 --- a/http/codegen/openapi/v2/testdata/TestSections/not-generate-attribute_file0.golden +++ b/http/codegen/openapi/v2/testdata/TestSections/not-generate-attribute_file0.golden @@ -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}}}} \ No newline at end of file +{"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"]}}} \ No newline at end of file diff --git a/http/codegen/openapi/v2/testdata/TestSections/not-generate-attribute_file1.golden b/http/codegen/openapi/v2/testdata/TestSections/not-generate-attribute_file1.golden index 0f1306d18c..ac7d0e28cb 100644 --- a/http/codegen/openapi/v2/testdata/TestSections/not-generate-attribute_file1.golden +++ b/http/codegen/openapi/v2/testdata/TestSections/not-generate-attribute_file1.golden @@ -24,11 +24,15 @@ 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: @@ -36,11 +40,17 @@ definitions: 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 @@ -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 diff --git a/http/codegen/openapi/v3/testdata/golden/not-generate-attribute_file0.golden b/http/codegen/openapi/v3/testdata/golden/not-generate-attribute_file0.golden index 737b5441d1..99bc23b98e 100644 --- a/http/codegen/openapi/v3/testdata/golden/not-generate-attribute_file0.golden +++ b/http/codegen/openapi/v3/testdata/golden/not-generate-attribute_file0.golden @@ -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"}]} \ No newline at end of file +{"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"}]} \ No newline at end of file diff --git a/http/codegen/openapi/v3/testdata/golden/not-generate-attribute_file1.golden b/http/codegen/openapi/v3/testdata/golden/not-generate-attribute_file1.golden index cf61fdcf62..02f5d2cc66 100644 --- a/http/codegen/openapi/v3/testdata/golden/not-generate-attribute_file1.golden +++ b/http/codegen/openapi/v3/testdata/golden/not-generate-attribute_file1.golden @@ -18,6 +18,7 @@ paths: schema: $ref: '#/components/schemas/TestEndpointRequestBody' example: + required_string: "" string: "" responses: "200": @@ -28,6 +29,7 @@ paths: $ref: '#/components/schemas/Result' example: int: 0 + required_int: 0 components: schemas: Result: @@ -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 diff --git a/http/codegen/testdata/openapi_dsls.go b/http/codegen/testdata/openapi_dsls.go index af135c397f..a76aac5bb0 100644 --- a/http/codegen/testdata/openapi_dsls.go +++ b/http/codegen/testdata/openapi_dsls.go @@ -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() { @@ -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() { From a8d4d7e5f21371d4a425017dc3f7add7a82666e8 Mon Sep 17 00:00:00 2001 From: Taichi Sasaki Date: Thu, 18 Jan 2024 01:03:03 +0900 Subject: [PATCH 2/2] Fix openapi:generate Meta to affect required validations --- http/codegen/openapi/json_schema.go | 9 ++++++++- http/codegen/openapi/v3/types.go | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/http/codegen/openapi/json_schema.go b/http/codegen/openapi/json_schema.go index e1ba5fae54..b41e16358c 100644 --- a/http/codegen/openapi/json_schema.go +++ b/http/codegen/openapi/json_schema.go @@ -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 diff --git a/http/codegen/openapi/v3/types.go b/http/codegen/openapi/v3/types.go index ee675afeb2..f54b2e8bc6 100644 --- a/http/codegen/openapi/v3/types.go +++ b/http/codegen/openapi/v3/types.go @@ -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 }