swagger-api / swagger-api/swagger-codegen

[JAVA][JAXRS] DecimalMin annotation does not respect exclusiveMinimum

Open
#6,718 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Java Issue: Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Specifying a number property with exclusiveMin set to true does not create the corresponding inclusive = false on a DecimalMin annotation.

Expected
public class User   {
...
  /**
   * Get id
   * minimum: 10
   * @return id
   **/
  @JsonProperty("id")
  @ApiModelProperty(value = "")
 @DecimalMin("10", inclusive = false)  public BigDecimal getId() {
    return id;
  }
...
Actual
public class User   {
...
  /**
   * Get id
   * minimum: 10
   * @return id
   **/
  @JsonProperty("id")
  @ApiModelProperty(value = "")
 @DecimalMin("10")  public BigDecimal getId() {
    return id;
  }
Swagger-codegen version

Tried in 2.2.3, 2.2.1, and on the head of master

Swagger declaration file content or url

Modifying the pet store example, picking any definition.

...
definitions:
  User:
    type: "object"
    properties:
      id:
        type: "number"
        minimum: 10
        exclusiveMinimum: true
...
Command line used for generation

With the following configuration:

{
  "library":"jersey2"
}

java -jar swagger-codegen-cli-2.2.1.jar generate -c config.json -l java -i "D:/swaggerFile.json"

Steps to reproduce
  1. Take the pet store swagger file
  2. Change User id property to be a number with exclusiveMinimum: true
  3. Generate the jaxrs server files
Suggested changes

Presently Swagger makes use of the Min annotation for integers and DecimalMin for numbers. Min does not support the inclusive parameter. If all else is equal, it seems like DecimalMin is preferable. When exclusiveMinimum is set, the annotation should have the inclusive = false parameter. Likewise for maximums.

Trying this out locally, changing d:\swagger-codegen\modules\swagger-codegen\src\main\resources\JavaJaxRS\beanValidationCore.mustache to something like:

...@DecimalMin("{{minimum}}"{{#isExclusiveMinimum}}, inclusive = false{{/isExclusiveMinimum}}){{/minimum}}...

Does not seem to work immediately, as isExclusiveMinimum doesn't appear to be set on the property. From a very cursory look, it seems like this issue is beyond the template.


Looking at DefaultCodegen.fromModel, the property returns the appropriate value for getExclusiveMinimum()

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with DefaultCodegen.fromModel to trace exclusiveMinimum into the model property, then inspect modules/swagger-codegen/src/main/resources/JavaJaxRS/beanValidationCore.mustache. Reproduce with the modified Petstore definition and the shown Java CLI command. Done means generated JAX-RS models include inclusive = false for exclusive decimal minimums, with corresponding maximum behavior considered.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.