Skip to content

Commit

Permalink
Fix marshaling of aliased custom types
Browse files Browse the repository at this point in the history
This should fix #579 by detecting aliasing of Date and adding
JSON marshal/unmarshal hooks to the alias.
  • Loading branch information
marcinromaszewicz committed May 20, 2022
1 parent ec460d3 commit 920beb4
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 5 deletions.
1 change: 0 additions & 1 deletion internal/test/issues/issue-312/spec.yaml
@@ -1,4 +1,3 @@

openapi: "3.0.0"
info:
version: 1.0.0
Expand Down
3 changes: 3 additions & 0 deletions 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
17 changes: 17 additions & 0 deletions internal/test/issues/issue-579/issue.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions 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)
}
27 changes: 27 additions & 0 deletions 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
9 changes: 5 additions & 4 deletions pkg/codegen/schema.go
Expand Up @@ -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)
}
Expand Down Expand Up @@ -352,16 +352,17 @@ 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)
}
}
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

Expand Down

0 comments on commit 920beb4

Please sign in to comment.