Skip to content

Invalid typedefs when spec uses same ref name for request body and schema #407

@darkliquid

Description

@darkliquid

When I have a spec that defines the same thing twice (but in different namespaces) I get invalid code generated. For example, with a spec containing something like this (notice the reuse of the same identifier myThing for both the request body and the schema):

paths:
  /my/end/point:
    put:
      requestBody:
        $ref: '#/components/requestBodies/myThing'
components:
  requestBodies:
    myThing:
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/myThing'
      required: true
  schema:
    myThing:
      type: object
      properties:
        thing:
          type: string

Then I used up with a typedef in the generated code like: type MyThing MyThing which is invalid.

I've currently worked around this by altering typedef.tmpl to do the following:

{{range .Types}}
{{if ne .TypeName .Schema.TypeDecl }}
{{ with .Schema.Description }}{{ . }}{{ else }}// {{.TypeName}} defines model for {{.JsonName}}.{{ end }}
type {{.TypeName}} {{if and (opts.AliasTypes) (.CanAlias)}}={{end}} {{.Schema.TypeDecl}}
{{end}}
{{end}}

Which skips doing a typedef with a type name equal to it's schema declaration, but that is a bit of a hack and ideally the generator would create either unique names or be able to deduplicate identical types internally before iterating over them in the templates.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions