Repository: goadesign/goa
Pull request #3447 (2024-01-12 11:47:53)

Properly generate format for primitive types in OpenAPI specs
This fix is for types that do not have format validation, but have some other validations.


Patch:
diff --git a/http/codegen/openapi/json_schema.go b/http/codegen/openapi/json_schema.go
index b7d3724d..668904ba 100644
--- a/http/codegen/openapi/json_schema.go
+++ b/http/codegen/openapi/json_schema.go
@@ -495,7 +495,9 @@ func initAttributeValidation(s *Schema, at *expr.AttributeExpr) {
 		return
 	}
 	s.Enum = val.Values
-	s.Format = string(val.Format)
+	if val.Format != "" {
+		s.Format = string(val.Format)
+	}
 	s.Pattern = val.Pattern
 	if val.ExclusiveMinimum != nil {
 		s.ExclusiveMinimum = val.ExclusiveMinimum
diff --git a/http/codegen/openapi/v3/types.go b/http/codegen/openapi/v3/types.go
index f8661a51..42b1f181 100644
--- a/http/codegen/openapi/v3/types.go
+++ b/http/codegen/openapi/v3/types.go
@@ -265,7 +265,9 @@ func (sf *schemafier) schemafy(attr *expr.AttributeExpr, noref ...bool) *openapi
 		return s
 	}
 	s.Enum = val.Values
-	s.Format = string(val.Format)
+	if val.Format != "" {
+		s.Format = string(val.Format)
+	}
 	s.Pattern = val.Pattern
 	if val.ExclusiveMinimum != nil {
 		s.ExclusiveMinimum = val.ExclusiveMinimum


Test patch:
diff --git a/http/codegen/openapi/v2/testdata/TestValidations/integer_file0.golden b/http/codegen/openapi/v2/testdata/TestValidations/integer_file0.golden
index 9a4bca23..84ad429f 100644
--- a/http/codegen/openapi/v2/testdata/TestValidations/integer_file0.golden
+++ b/http/codegen/openapi/v2/testdata/TestValidations/integer_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":{"/":{"post":{"tags":["testService"],"summary":"testEndpoint testService","operationId":"testService#testEndpoint","parameters":[{"name":"int","in":"body","required":true,"schema":{"type":"integer","minimum":0,"maximum":42}}],"responses":{"200":{"description":"OK response.","schema":{"type":"integer","minimum":0,"maximum":42}}},"schemes":["https"]}}}}
\ 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":{"/":{"post":{"tags":["testService"],"summary":"testEndpoint testService","operationId":"testService#testEndpoint","parameters":[{"name":"int","in":"body","required":true,"schema":{"type":"integer","format":"int64","minimum":0,"maximum":42}}],"responses":{"200":{"description":"OK response.","schema":{"type":"integer","format":"int64","minimum":0,"maximum":42}}},"schemes":["https"]}}}}
\ No newline at end of file
diff --git a/http/codegen/openapi/v2/testdata/TestValidations/integer_file1.golden b/http/codegen/openapi/v2/testdata/TestValidations/integer_file1.golden
index 60305b53..211809ec 100644
--- a/http/codegen/openapi/v2/testdata/TestValidations/integer_file1.golden
+++ b/http/codegen/openapi/v2/testdata/TestValidations/integer_file1.golden
@@ -24,6 +24,7 @@ paths:
                   required: true
                   schema:
                     type: integer
+                    format: int64
                     minimum: 0
                     maximum: 42
             responses:
@@ -31,6 +32,7 @@ paths:
                     description: OK response.
                     schema:
                         type: integer
+                        format: int64
                         minimum: 0
                         maximum: 42
             schemes:
diff --git a/http/codegen/openapi/v3/testdata/golden/integer_file0.golden b/http/codegen/openapi/v3/testdata/golden/integer_file0.golden
index 7a518979..3d7b9770 100644
--- a/http/codegen/openapi/v3/testdata/golden/integer_file0.golden
+++ b/http/codegen/openapi/v3/testdata/golden/integer_file0.golden
@@ -1,1 +1,1 @@
-{"openapi":"3.0.3","info":{"title":"Goa API","version":"0.0.1"},"servers":[{"url":"https://goa.design"}],"paths":{"/":{"post":{"tags":["testService"],"summary":"testEndpoint testService","operationId":"testService#testEndpoint","requestBody":{"required":true,"content":{"application/json":{"schema":{"type":"integer","example":1,"minimum":0,"maximum":42},"example":1}}},"responses":{"200":{"description":"OK response.","content":{"application/json":{"schema":{"type":"integer","example":1,"minimum":0,"maximum":42},"example":1}}}}}}},"components":{},"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":{"/":{"post":{"tags":["testService"],"summary":"testEndpoint testService","operationId":"testService#testEndpoint","requestBody":{"required":true,"content":{"application/json":{"schema":{"type":"integer","example":1,"format":"int64","minimum":0,"maximum":42},"example":1}}},"responses":{"200":{"description":"OK response.","content":{"application/json":{"schema":{"type":"integer","example":1,"format":"int64","minimum":0,"maximum":42},"example":1}}}}}}},"components":{},"tags":[{"name":"testService"}]}
\ No newline at end of file
diff --git a/http/codegen/openapi/v3/testdata/golden/integer_file1.golden b/http/codegen/openapi/v3/testdata/golden/integer_file1.golden
index e1fa665c..ad54b529 100644
--- a/http/codegen/openapi/v3/testdata/golden/integer_file1.golden
+++ b/http/codegen/openapi/v3/testdata/golden/integer_file1.golden
@@ -18,6 +18,7 @@ paths:
                         schema:
                             type: integer
                             example: 1
+                            format: int64
                             minimum: 0
                             maximum: 42
                         example: 1
@@ -29,6 +30,7 @@ paths:
                             schema:
                                 type: integer
                                 example: 1
+                                format: int64
                                 minimum: 0
                                 maximum: 42
                             example: 1