Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
5 of 6 tasks
mohamuni opened this issue May 10, 2024 · 3 comments · Fixed by #18644
Closed
5 of 6 tasks

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

mohamuni opened this issue May 10, 2024 · 3 comments · Fixed by #18644

Comments

@mohamuni
Copy link

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
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}}
@mohamuni mohamuni changed the title [BUG] [Go] Go SDK breaks for schema names with special character(._) [BUG] [Go] Go SDK breaks for parent schema names with special character(._) May 10, 2024
@vvb
Copy link
Contributor

vvb commented May 10, 2024

cc @wing328 @rikotsev I think this regression was introduced by the changes made in #18390

We would appreciate your views on this.

@wing328
Copy link
Member

wing328 commented May 11, 2024

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
Copy link
Contributor

vvb commented May 12, 2024

Done

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

Successfully merging a pull request may close this issue.

3 participants