Repository: goadesign/goa
Pull request #3452 (2024-01-17 16:02:55)

Fix openapi:generate Meta to affect required validations
Related to https://github.com/goadesign/goa/pull/3437

raphael: Good catch, thank you.

Patch:
diff --git a/http/codegen/openapi/json_schema.go b/http/codegen/openapi/json_schema.go
index e1ba5fae..b41e1635 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 ee675afe..f54b2e8b 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
 }


Test patch:
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 5a1ac82f..735354eb 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 +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 0f1306d1..ac7d0e28 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 737b5441..99bc23b9 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 +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 cf61fdcf..02f5d2cc 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 af135c39..a76aac5b 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() {