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] Migrating from openapi-generator-maven-plugin 5.0.0 to 5.0.1+/6.x+ doesnt apply @DecimalMin and @DecimalMax anymore to string field #16312

Closed
dhananjay12 opened this issue Aug 12, 2023 · 9 comments

Comments

@dhananjay12
Copy link

dhananjay12 commented Aug 12, 2023

Description

We had a schema with type string that contained decimal value.

components:
  schemas:
    Item:
      type: object
      properties:
        id:
          type: string
          format: uuid
          description: The unique identifier for the item
        name:
          type: string
          description: The name of the item
        price:
          type: string
          minimum: -999999999999999999.99999
          maximum: 999999999999999999.99999
          description: Price

With 4.3.1 or 5.0.0 it use to @DecimalMax, @DecimalMin anootation based on maximum/minimum
image

Starting from 5.0.1, it didnt added those

image
openapi-generator version

5.0.1 version (used to work in 5.0.0 and 4.3.1)
Also reproducible with the lates 6.6.0 (with spring boot 3)

OpenAPI declaration file content or url

Minimum example here -https://github.com/dhananjay12/learn-open-api/blob/string-decimal/sample-service/src/main/openapi/test-service-api-v1.yaml

Generation Details

https://github.com/dhananjay12/learn-open-api/blob/string-decimal/sample-service/pom.xml#L110

Steps to reproduce

In the pom.xml , with openapi-generator-maven-plugin version 5.0.1 generated the code to see the reproduce the issue. With 5.0.0 or 4.3.1 its fine.

Suggest a fix

Any workaround to achieve the same? (Add @DecimalMin and @DecimalMax annotation to string type)

@wing328
Copy link
Member

wing328 commented Aug 12, 2023

please try 7.0.0-beta release instead.

5.x release was a while ago

@dhananjay12 dhananjay12 changed the title [BUG] Migrating from openapi-generator-maven-plugin 5.0.0 to 5.0.1+ doesnt apply doesn't apply @DecimalMin and @DecimalMax anymore to string field [BUG] Migrating from openapi-generator-maven-plugin 5.0.0 to 5.0.1+/6.x+ doesnt apply doesn't apply @DecimalMin and @DecimalMax anymore to string field Aug 12, 2023
@dhananjay12
Copy link
Author

7.0.0-beta version also results the same.

5.0.1 is just the version where we started seeing the issue. Checking the release notes, not sure what introduce this change. We are actually at 6.6.0.

@dhananjay12 dhananjay12 changed the title [BUG] Migrating from openapi-generator-maven-plugin 5.0.0 to 5.0.1+/6.x+ doesnt apply doesn't apply @DecimalMin and @DecimalMax anymore to string field [BUG] Migrating from openapi-generator-maven-plugin 5.0.0 to 5.0.1+/6.x+ doesnt apply @DecimalMin and @DecimalMax anymore to string field Aug 12, 2023
@wing328
Copy link
Member

wing328 commented Aug 15, 2023

do you mind performing a git bisect to identify the commit causing the issue?

@dhananjay12
Copy link
Author

My analysis leads to this merge - 2331432

Checking the commits,

Not Working - Adds hasValidation to all java core Schema classes (#8474) - 2331432

Working - [PowerShell] minor fix to API doc - 3d23b99

Checked by building the project locally on these commit and testing with the minimal project provided earlier.

@wing328
Copy link
Member

wing328 commented Aug 17, 2023

thanks for identifying the issue. let me try to come up with a fix later.

@wing328
Copy link
Member

wing328 commented Aug 17, 2023

to fix the issue, use number instead of string,

        price:
          type: number
          minimum: -999999999999999999.99999
          maximum: 999999999999999999.99999
          description: Price

@dhananjay12
Copy link
Author

dhananjay12 commented Aug 17, 2023

Yes, it does add the annotations
image

But this change the datatype from string to BigDecimal which will be a breaking change wrt to APIs forcing us to make a major release.

Can you suggest any other workaround? or a way to override the template on our side?

@martin-mfg
Copy link
Contributor

You can specify the input like this:

price:
  type: string
  format: number
  minimum: -999999999999999999.99999
  maximum: 999999999999999999.99999
  description: Price

This way price will still be a string for users of your API. In the generated code, it will be represented as BigDecimal however and have the appropriate @DecimalMin and @DecimalMax annotations.

If you also want to change the internal representation in the generated code to String, on top of the above change you can add a type mapping to your pom.xml like this:

<generatorName>spring</generatorName>
<typeMappings>
	<typeMapping>BigDecimal=String</typeMapping>
</typeMappings>
<!-- ... -->

This will generate

  @Valid @DecimalMin("-1.0E+18") @DecimalMax("1.0E+18") 
  @Schema(name = "price", description = "Price", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  @JsonProperty("price")
  public String getPrice() {

@dhananjay12
Copy link
Author

Thanks @wing328 @martin-mfg for the workarounds.

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

No branches or pull requests

3 participants