Skip to content

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

Closed

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

Activity

  1. auto-labeler commented on May 23, 2019

    @auto-labeler

    👍 Thanks for opening this issue!
    🏷 I have applied any labels matching special text in your issue.

    The team will review the labels and make any necessary changes.

  2. iafilius commented on Mar 26, 2021

    @iafilius

    Hi, ran in same issue with openapi-generator-cli 4.2.2 and 5.1.0
    specfile used:
    https://code.vmware.com/apis/1115/velocloud-sdwan-vco-api

    // ConfigurationGetIdentifiableApplicationsResult struct for ConfigurationGetIdentifiableApplicationsResult
    type ConfigurationGetIdentifiableApplicationsResult struct {
            // Maps application class IDs (strings) to application IDs (integers)
            ApplicationClassToApplication ModelMap `json:"applicationClassToApplication"`
            // Maps application IDs (strings) to class IDs (integers)
            ApplicationToApplicationClass ModelMap `json:"applicationToApplicationClass"`
            // Map of application class IDs to class names
            ApplicationClasses ModelMap       `json:"applicationClasses"`
            Applications       *[]Application `json:"applications"`
    }
    

    is generator but ModelMap is not defined

    source:

        "configuration_get_identifiable_applications_result": {
          "properties": {
            "applicationClassToApplication": {
              "allOf": [
                {
                  "$ref": "#/definitions/id_map_one_to_many"
                }
              ],
              "description": "Maps application class IDs (strings) to application IDs (integers)"
            },
            "applicationToApplicationClass": {
              "allOf": [
                {
                  "$ref": "#/definitions/id_map_one_to_many"
                }
              ],
              "description": "Maps application IDs (strings) to class IDs (integers)"
            },
            "applicationClasses": {
              "allOf": [
                {
                  "$ref": "#/definitions/id_map_one_to_one"
                }
              ],
              "description": "Map of application class IDs to class names"
            },
            "applications": {
              "items": {
                "$ref": "#/definitions/application"
              },
              "type": "array"
            }
          },
          "required": [
            "applications",
            "applicationClasses",
            "applicationClassToApplication",--generate-alias-as-model
            "applicationToApplicationClass"
          ],
          "type": "object"
        },
    

    and

        "id_map_one_to_many": {
          "additionalProperties": {
            "type": "integer"
          },
          "type": "object"
        },
        "id_map_one_to_one": {
          "additionalProperties": {
            "type": "integer"
          },
          "type": "string"
        },
    

    using the suggested --generate-alias-as-model results in an empty type (but go code at least compiles)

  3. JigarJoshi04 commented on Feb 13, 2024

    @JigarJoshi04

    I am still facing the same issue, any possible solution?

  4. wing328 commented on Feb 13, 2024

    @wing328
    Member

    did a test with the latest master and the output works (no compilation error)

    please give it a try with the latest master or stable version (v7.3.0)

    closing this one for the time being

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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