diff --git a/internal/test/issues/issue-312/spec.yaml b/internal/test/issues/issue-312/spec.yaml index bcf273ef3..47be65fe7 100644 --- a/internal/test/issues/issue-312/spec.yaml +++ b/internal/test/issues/issue-312/spec.yaml @@ -1,4 +1,3 @@ - openapi: "3.0.0" info: version: 1.0.0 diff --git a/internal/test/issues/issue-579/gen.go b/internal/test/issues/issue-579/gen.go new file mode 100644 index 000000000..6915f26a1 --- /dev/null +++ b/internal/test/issues/issue-579/gen.go @@ -0,0 +1,3 @@ +package issue_579 + +//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen --package=issue_579 --generate=types,skip-prune -o issue.gen.go spec.yaml diff --git a/internal/test/issues/issue-579/issue.gen.go b/internal/test/issues/issue-579/issue.gen.go new file mode 100644 index 000000000..dae16ef5b --- /dev/null +++ b/internal/test/issues/issue-579/issue.gen.go @@ -0,0 +1,17 @@ +// Package issue_579 provides primitives to interact with the openapi HTTP API. +// +// Code generated by github.com/deepmap/oapi-codegen version (devel) DO NOT EDIT. +package issue_579 + +import ( + openapi_types "github.com/deepmap/oapi-codegen/pkg/types" +) + +// AliasedDate defines model for AliasedDate. +type AliasedDate openapi_types.Date + +// Pet defines model for Pet. +type Pet struct { + Born *AliasedDate `json:"born,omitempty"` + BornAt *openapi_types.Date `json:"born_at,omitempty"` +} diff --git a/internal/test/issues/issue-579/issue_test.go b/internal/test/issues/issue-579/issue_test.go new file mode 100644 index 000000000..4c1b90118 --- /dev/null +++ b/internal/test/issues/issue-579/issue_test.go @@ -0,0 +1,14 @@ +package issue_579 + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestAliasedDate(t *testing.T) { + pet := Pet{} + err := json.Unmarshal([]byte(`{"born": "2022-05-19", "born_at": "2022-05-20"}`), &pet) + require.NoError(t, err) +} diff --git a/internal/test/issues/issue-579/spec.yaml b/internal/test/issues/issue-579/spec.yaml new file mode 100644 index 000000000..2bf359c4d --- /dev/null +++ b/internal/test/issues/issue-579/spec.yaml @@ -0,0 +1,27 @@ +openapi: "3.0.0" +info: + version: 1.0.0 + title: Issue 579 test +paths: + /placeholder: + get: + responses: + 200: + description: placeholder + content: + text/plain: + schema: + type: string +components: + schemas: + Pet: + type: object + properties: + born: + $ref: "#/components/schemas/AliasedDate" + born_at: + type: string + format: date + AliasedDate: + type: string + format: date diff --git a/pkg/codegen/schema.go b/pkg/codegen/schema.go index 13f2a81d1..3843e0ac2 100644 --- a/pkg/codegen/schema.go +++ b/pkg/codegen/schema.go @@ -315,7 +315,7 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) { } return outSchema, nil } else if len(schema.Enum) > 0 { - err := resolveType(schema, path, &outSchema) + err := oapiSchemaToGoType(schema, path, &outSchema) if err != nil { return Schema{}, fmt.Errorf("error resolving primitive type: %w", err) } @@ -352,7 +352,7 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) { } //outSchema.RefType = typeName } else { - err := resolveType(schema, path, &outSchema) + err := oapiSchemaToGoType(schema, path, &outSchema) if err != nil { return Schema{}, fmt.Errorf("error resolving primitive type: %w", err) } @@ -360,8 +360,9 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) { return outSchema, nil } -// resolveType resolves primitive type or array for schema -func resolveType(schema *openapi3.Schema, path []string, outSchema *Schema) error { +// oapiSchemaToGoType converts an OpenApi schema into a Go type definition for +// all non-object types. +func oapiSchemaToGoType(schema *openapi3.Schema, path []string, outSchema *Schema) error { f := schema.Format t := schema.Type