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

[Go] Fix an issue causing int array reference translated to an invalid type []Integer #18785

Closed

Conversation

ChihweiLHBird
Copy link
Contributor

@ChihweiLHBird ChihweiLHBird commented May 29, 2024

CC: @antihax @grokify @kemokemo @jirikuncar @ph4r5h4d @lwj5

Fix an issue causing integer array with items referenced by $ref being generated to an invalid type []Integer.

Sample problematic spce:

openapi: 3.0.3
info:
  version: 1.0.0
  title: My API
  description: |
    some API spec
paths:
  /some-endpoint:
    post:
      servers:
      - url: https://example.com/
      operationId: someOperation
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
              - ids
              properties:
                MyIntegerArray:
                  type: array
                  items:
                    $ref: '#/components/schemas/ExampleObject/properties/id'

      responses:
        '200':
          description: success
          content:
            application/json:
              schema:
                type: object
components:
  schemas:
    ExampleObject:
      type: object
      properties:
        id:
          readOnly: true
          type: integer

This PR reorder of the if... else if... else... expression to prioritize existing primitive types defined in typeMapping to try to address the issue.

This PR double checks to ensure the openAPIType is in the ref before proceeding to generating a type with the referenced model name to avoid integer type with a reference being converted to Integer, rather than int32.

Further investigation might be needed to look into how ref and get$ref() work because them seems to be null when $ref is '#/components/schemas/SomeID' in the spec, but contains a non-null string value "#/components/schemas/ExampleObject/properties/id" when $ref is the corresponding value. This might be root cause of this issue and may have a larger impact.

Let me know any thoughts, feedback, or idea. Still working on finalizing things with the check list below. Done now.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (For Windows users, please run the script in Git BASH)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.6.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@ChihweiLHBird
Copy link
Contributor Author

@ChihweiLHBird ChihweiLHBird marked this pull request as ready for review May 30, 2024 06:40
@wing328
Copy link
Member

wing328 commented May 30, 2024

thanks for the PR.

can you please add a test in modules/openapi-generator/src/test/resources/3_0/go/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml and update the samples?

@ChihweiLHBird
Copy link
Contributor Author

ChihweiLHBird commented May 30, 2024

Hi @wing328, sure! Would it be fine to add a new path in that sample? I feel this fix is hard to be tested in the existing endpoint specifications in that doc.

@wing328
Copy link
Member

wing328 commented May 30, 2024

yes, you can add another fake (tag) endpoint

@wing328
Copy link
Member

wing328 commented Jun 1, 2024

                $ref: '#/components/schemas/ExampleObject/properties/id'

which version of openapi generator are you using?

I remember adding better support for referencing properties in recent versions (maybe starting in v7.5.0).

@ChihweiLHBird
Copy link
Contributor Author

Hi @wing328, I was using the master branch, so it should include the changes made in v7.5.0

@ChihweiLHBird
Copy link
Contributor Author

Hi @wing328, I just added the sample. Does it look good?

@ChihweiLHBird
Copy link
Contributor Author

Any thought, guys?

@@ -425,17 +424,19 @@ public String getSchemaType(Schema p) {
String ref = p.get$ref();
String type;

if (ref != null && !ref.isEmpty()) {
if (ref != null && !ref.isEmpty() && ref.endsWith(openAPIType)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from what i understand, this fix only works if the schema name happens to be openAPIType (e.g. string), right?

Copy link
Contributor Author

@ChihweiLHBird ChihweiLHBird Jun 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has been a while. If I recall correctly (I will double check tomorrow), openAPIType would be a primitive type name, (e.g., "integer"), for a reference to an integer, like this example below.

And openAPIType would be s schema name (e.g., "SomeSchema") for a reference to a schema object.

So, checking whether the ref is ended with the openAPIType can determine whether it's a Schema object, which needs toModelName() to capitalize its first letter, rather than a primitive type, which will be handled elsewhere.

@wing328
Copy link
Member

wing328 commented Jun 25, 2024

@ChihweiLHBird i've filed #19013 based on this PR.

It fixes the issue differently by checking if the ref is a ref to another schema in a property.

can you please test it to ensure it works for your use cases?

thanks

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

Successfully merging this pull request may close these issues.

None yet

2 participants