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

fix enum type generators template #959

Closed
wants to merge 8 commits into from

Conversation

sbilello
Copy link
Contributor

This is necessary to solve swagger-api/swagger-codegen#11166
Please take a look @HugoMario

pom.xml Outdated
@@ -12,7 +12,7 @@

<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-generators</artifactId>
<version>1.0.28-SNAPSHOT</version>
<version>3.0.29-SNAPSHOT</version>
Copy link
Contributor

Choose a reason for hiding this comment

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

you don't need to make this change. It will create a mess on version releases

@HugoMario
Copy link
Contributor

I reviewed and tested changes on this PR, there is a build error on generated code. The error is related to Adapter code

public static class Adapter extends TypeAdapter<{{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}> {
@Override
public void write(final JsonWriter jsonWriter, final {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} enumeration) throws IOException {
jsonWriter.value(enumeration.getValue());
}
@Override
public {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} read(final JsonReader jsonReader) throws IOException {
Object value = {{#isNumber}}new BigDecimal(jsonReader.nextDouble()){{/isNumber}}{{^isNumber}}jsonReader.{{#isInteger}}nextInt(){{/isInteger}}{{^isInteger}}nextString(){{/isInteger}}{{/isNumber}};
return {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.fromValue(String.valueOf(value));
}
}

@sbilello
Copy link
Contributor Author

sbilello commented Sep 21, 2021 via email

Sergio Bilello added 2 commits September 21, 2021 13:48
* master:
  added option gitRepoBaseURL for PHP generator
@sbilello
Copy link
Contributor Author

@HugoMario

mvn test
[INFO] Results:
[INFO] 
[INFO] Tests run: 225, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  12.531 s
[INFO] Finished at: 2021-09-21T14:04:17-07:00

I didn't touch the adapter code. Could you please point out the problem?

@HugoMario
Copy link
Contributor

Hey @sbilello,

The adapter code makes a call to the enum value() method, and it's expecting to be string. Since you changed that to received an integer or any other data type, that creates a build error when we use mvn package command to the generated code.

@HugoMario
Copy link
Contributor

this is an eample i use to reproduce the build problem:

openapi: 3.0.3
servers: []
info:
  description: Nested Objct Sample.
  version: "1.0.0"
  title: Nested Object
  termsOfService: 'http://swagger.io/terms/'
  contact:
    email: apiteam@swagger.io
  license:
    name: Apache 2.0
    url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
tags:
  - name: nestedObject
    description: Everything about your Pets
    externalDocs:
      description: Find out more
      url: 'http://swagger.io'
  - name: store
    description: Access to Petstore orders
  - name: user
    description: Operations about user
    externalDocs:
      description: Find out more about our store
      url: 'http://swagger.io'
paths:
  /nestedObject:
    get:
      operationId: checkNestedObject
      parameters:
        - name: nestedObject
          in: query
          description: nested object.
          required: true
          style: deepObject
          schema:
            $ref: '#/components/schemas/NestedObject'
      responses:
        200:
          description: good

externalDocs:
  description: Find out more about Swagger
  url: 'http://swagger.io'
components:
  schemas:
    ObjectWithEnum:
      type: object
      description: The base meeting object.
      properties:
        topic:
          type: string
          description: The meeting's topic.
        type:
          type: integer
          description: |-
            The type of meeting:
            * `1` — An instant meeting.
            * `2` — A scheduled meeting.
            * `3` — A recurring meeting with no fixed time.
            * `8` — A recurring meeting with fixed time.
          default: 2
          #### THIS CAUSES PROBLEM IN THE SERIALIZATION BECAUSE THE ENUMS VALUES ARE NUMBERS AND NOT STRINGS
          enum:
            - 1
            - 2
            - 3
            - 8
          x-enum-descriptions:
            - Instant meeting.
            - Scheduled meeting.
            - Recurring meeting with no fixed time.
            - Recurring meeting with fixed time.
    Parakeet:
      type: object
      properties:
        color:
          type: string
        soundRepeater:
          type: boolean

    NestedObject:
      type: object
      properties:
        name:
          type: string
        types:
          type: array
          items:
            type: string
        parakeet:
          $ref: '#/components/schemas/Parakeet'
        count:
          type: integer
        enable:
          type: boolean

@sbilello
Copy link
Contributor Author

@HugoMario Thanks for your reply!

I added the fix but I was not able to replicate the bug with the example provided in the above message.

Please check this project that uses your example and generates the corresponding client:
https://github.com/sbilello/swagger-gen-bug-enum-parameters/blob/java8EnumCompilingTest/bug-client/src/main/java/us/test/openapi/graphql/model/ObjectWithEnum.java

I don't see "Adapter" code for the ObjectWithEnum.

Please provide steps to reproduce the problem or eventually open a PR on that test project if the fix provided does not solve the problem.

Thank you again!

@HugoMario
Copy link
Contributor

Hey @sbilello

I just generated code with your changes using the spec i shared here.
then i run:

mvn package

and i got the buid errors. I'll try again later with your changes and let you know.

@sbilello
Copy link
Contributor Author

sbilello commented Sep 27, 2021

@HugoMario I used your spec in that example project. I don't have any build errors. Did you try to clone https://github.com/sbilello/swagger-gen-bug-enum-parameters/tree/java8EnumCompilingTest that contains your spec?
Please check this out: https://github.com/sbilello/swagger-gen-bug-enum-parameters/blob/java8EnumCompilingTest/bug-client/src/main/resources/reproduceBug.yaml
I don't find any error with mvn package on java 8.
If you still encounter problems please list the steps to replicate the errors and the environment that you are working on.
I am not able to replicate it on my local.
Thanks!

@sbilello
Copy link
Contributor Author

sbilello commented Sep 28, 2021

@frantuma @HugoMario @gracekarina Could you please take a look?

@sbilello sbilello closed this Oct 11, 2021
@sbilello sbilello deleted the fix-enum-type-generator branch October 11, 2021 17:07
@sbilello
Copy link
Contributor Author

This PR has been rebased and the commits have been squashed.
I opened this PR #978 upon @frantuma request

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.

2 participants