OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.64k stars 6.53k forks source link

[BUG] [Go] Go SDK breaks for parent schema names with special character(._) #18632

Closed mohamuni closed 5 months ago

mohamuni commented 5 months ago

Bug Report Checklist

Description

While generating Go SDK with OpenAPI Generator, if a composite schema's parent schema contains names with special characters (. _) and a discriminator is defined on the parent schema, Go SDK fails because the sanitized name of the parent model is not used in the composite schema model.

openapi-generator version

Latest version(7.5.0)

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Test
  version: 1.0.0
paths: {}
components:
  schemas:
    FinalItem:
      type: object
      allOf:
        - $ref: '#/components/schemas/Base.Item'
        - $ref: '#/components/schemas/AdditionalData'
    Base.Item:
      type: object
      properties:
        title:
          type: string
        type:
          type: string
          enum:
            - FINAL
          example: FINAL
      discriminator:
        propertyName: type
        mapping:
          FINAL: '#/components/schemas/FinalItem'
      required:
        - title
        - type
    AdditionalData:
      type: object
      properties:
        prop1:
          type: string
        quantity:
          type: integer
          format: int32
          example: 1
          minimum: 1
        unitPrice:
          type: number
          format: double
          example: 9.99
          minimum: 0.0
        totalPrice:
          type: number
          format: double
          example: 9.99
          minimum: 0.0
      required:
        - sku
        - quantity
        - unitPrice
        - totalPrice
Generation Details
mvn clean install
java -jar openapi-generator-cli-7.5.0.jar generate -i example.yaml -g go -o gosdk
Steps to reproduce

Generate the Go SDK with the specified spec file and observe that the parent schema model name in the composite schema model is not sanitized.

Actual Parent model file

type BaseItem struct {
    Title string `json:"title"`
    Type string `json:"type"`
}

Actual Composite model file when referring to the parent schema Base.Item which is not sanitized name, it leads to failure of generated SDK, Instead of referring to sanitized name(BaseItem) of parent model

type FinalItem struct {
    Base.Item
    Prop1 *string `json:"prop1,omitempty"`
    Quantity int32 `json:"quantity"`
    UnitPrice float64 `json:"unitPrice"`
    TotalPrice float64 `json:"totalPrice"`
}

Expected model definition in Composite schema model file.

type FinalItem struct {
    BaseItem
    Prop1 *string `json:"prop1,omitempty"`
    Quantity int32 `json:"quantity"`
    UnitPrice float64 `json:"unitPrice"`
    TotalPrice float64 `json:"totalPrice"`
}
Suggest a fix

In following file, when checking for parent model for composite schema, specify to use parentmodel.classname instead of parentmodel.name https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/go/model_simple.mustache

Following changes are suggested along with line numbers

6   {{#parentModel.name}}
7   {{^isArray}}
8       {{{parentModel.classname}}}
9   {{/isArray}}
10  {{#isArray}}
11      Items {{{parentModel.classname}}}
12  {{/isArray}}
13  {{/parentModel.name}}
vvb commented 5 months ago

cc @wing328 @rikotsev I think this regression was introduced by the changes made in https://github.com/OpenAPITools/openapi-generator/pull/18390

We would appreciate your views on this.

wing328 commented 5 months ago

In following file, when checking for parent model for composite schema, specify to use parentmodel.classname instead of parentmodel.name

please file a PR with the suggested fix when you've time.

vvb commented 5 months ago

Done