Skip to content

Commit

Permalink
Fix schema shadowing issue by enums (deepmap#833)
Browse files Browse the repository at this point in the history
This fixes deepmap#832 by creating errors upon type conflict, and allowing
renaming enum types via `x-go-name`.

Co-authored-by: marcinromaszewicz <marcinromaszewicz@deepmap.ai>
  • Loading branch information
deepmap-marcinr and marcinromaszewicz committed Oct 28, 2022
1 parent 80a2aca commit 3bd0507
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 8 deletions.
5 changes: 5 additions & 0 deletions internal/test/issues/issue-832/config.yaml
@@ -0,0 +1,5 @@
package: issue_832
generate:
models: true
embedded-spec: true
output: issue.gen.go
3 changes: 3 additions & 0 deletions internal/test/issues/issue-832/generate.go
@@ -0,0 +1,3 @@
package issue_832

//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen --config=config.yaml spec.yaml
121 changes: 121 additions & 0 deletions internal/test/issues/issue-832/issue.gen.go

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

43 changes: 43 additions & 0 deletions internal/test/issues/issue-832/spec.yaml
@@ -0,0 +1,43 @@
openapi: 3.0.2
info:
version: '0.0.1'
title: example
description: |
Make sure that recursive types are handled properly
paths:
/example:
get:
operationId: exampleGet
responses:
'200':
description: "OK"
content:
'application/json':
schema:
$ref: '#/components/schemas/Document'
/example2:
get:
operationId: exampleGet2
responses:
'200':
description: "OK"
content:
'application/json':
schema:
$ref: '#/components/schemas/DocumentStatus'
components:
schemas:
Document:
type: object
properties:
name:
type: string
status:
type: string
x-go-name: Document_Status
enum: [one, two, three, four]
DocumentStatus:
type: object
properties:
value:
type: string
13 changes: 8 additions & 5 deletions pkg/codegen/codegen.go
Expand Up @@ -592,14 +592,17 @@ func GenerateTypes(t *template.Template, types []TypeDefinition) (string, error)
m := map[string]bool{}
ts := []TypeDefinition{}

for _, t := range types {
if found := m[t.TypeName]; found {
continue
for _, typ := range types {
if found := m[typ.TypeName]; found {
// We want to create an error when we try to define the same type
// twice.
return "", fmt.Errorf("duplicate typename '%s' detected, can't auto-rename, "+
"please use x-go-name to specify your own name for one of them", typ.TypeName)
}

m[t.TypeName] = true
m[typ.TypeName] = true

ts = append(ts, t)
ts = append(ts, typ)
}

context := struct {
Expand Down
16 changes: 13 additions & 3 deletions pkg/codegen/schema.go
Expand Up @@ -220,7 +220,7 @@ func PropertiesEqual(a, b Property) bool {
func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
// Add a fallback value in case the sref is nil.
// i.e. the parent schema defines a type:array, but the array has
// no items defined. Therefore we have at least valid Go-Code.
// no items defined. Therefore, we have at least valid Go-Code.
if sref == nil {
return Schema{GoType: "interface{}"}, nil
}
Expand Down Expand Up @@ -438,7 +438,18 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
}
}
if len(path) > 1 { // handle additional type only on non-toplevel types
typeName := SchemaNameToTypeName(PathToTypeName(path))
// Allow overriding autogenerated enum type names, since these may
// cause conflicts - see https://github.com/deepmap/oapi-codegen/issues/832
var typeName string
if extension, ok := schema.Extensions[extGoName]; ok {
typeName, err = extTypeName(extension)
if err != nil {
return outSchema, fmt.Errorf("invalid value for %q: %w", extGoName, err)
}
} else {
typeName = SchemaNameToTypeName(PathToTypeName(path))
}

typeDef := TypeDefinition{
TypeName: typeName,
JsonName: strings.Join(path, "."),
Expand All @@ -447,7 +458,6 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
outSchema.AdditionalTypes = append(outSchema.AdditionalTypes, typeDef)
outSchema.RefType = typeName
}
// outSchema.RefType = typeName
} else {
err := oapiSchemaToGoType(schema, path, &outSchema)
if err != nil {
Expand Down

0 comments on commit 3bd0507

Please sign in to comment.