Skip to content

[BUG] [Go] Incomplete generation for refs #2980

@mcristina422

Description

@mcristina422
Description

I'm attempting to generate a client for a spec which includes complex references https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/

My first thought was this was a bug because it includes a circular reference but it seems to be due to the $ref references. I've linked to PRs discussing similar issues.

openapi-generator version

v4.0.0
(I also tried todays latest sha256:f6107b18532b334798cfafc7cafdb82b479b6955a2417d61f12b66eda98289f4)

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  version: 1.0.0
  title: Example
  license:
    name: MIT
servers:
  - url: http://api.example.xyz/v1
paths:
  /resource/{resourceId}:
    get:
      parameters:
        - name: resourceId
          in: path
          required: true
          description: The id of the resource to retrieve
          schema:
            allOf:
              - $ref: '#/components/schemas/ResourceID'
      operationId: list
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Resource'
components:
  schemas:
    Resource:
      type: object
      properties:
        id:
          allOf:
            - $ref: '#/components/schemas/ResourceID'
        children:
          allOf:
            - $ref: "#/components/schemas/ResourceChildren"
    ResourceChildren:
      description: A map of alias to child object.
      type: object
      additionalProperties:
        $ref: '#/components/schemas/ResourceChild'
    ResourceChild:
      description: A specification of a resource's child.
      required:
        - id
      properties:
        id:
          description: The ID of the child resource.
          allOf:
            - $ref: '#/components/schemas/ResourceID'
        resource:
          description: The child resource.
          nullable: true
          allOf:
            - $ref: '#/components/schemas/Resource'
    ResourceID:
      type: string
      example: 1234myname
Command line used for generation
	docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v4.0.0 generate \
        -i /local/spec.yaml \
        -g go \
        -o /local/client
Steps to reproduce

The command above generates

type Resource struct {
	Id string `json:"id,omitempty"`
	Children ModelMap `json:"children,omitempty"`
}
type ResourceChild struct {
	// The ID of the child resource.
	Id string `json:"id"`
	// The child resource.
	Resource *Resource `json:"resource,omitempty"`
}

where ModelMap is undefined.

or optionally with the flag

--generate-alias-as-model
type Resource struct {
	Id string `json:"id,omitempty"`
	Children ResourceChildren `json:"children,omitempty"`
}

// A map of alias to child object.
type ResourceChildren struct {
}
Suggest a fix

Shouldn't it generate ResourceChildren as

type ResourceChildren *map[ResourceID]ResourceChild

and (this may be a different issue alltogether or maybe I'm misunderstanding something) provide a definition like below and use it in place for id everywhere
type ResourceID string

Related PR

#398
#2008
#2692
#1139
#2858

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions