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

[JAVA] Missing @Pattern in generated code #7980

Closed
Michus001 opened this issue Apr 6, 2018 · 12 comments · Fixed by #8277
Closed

[JAVA] Missing @Pattern in generated code #7980

Michus001 opened this issue Apr 6, 2018 · 12 comments · Fixed by #8277
Assignees

Comments

@Michus001
Copy link

Description

I wanted to reuse some string fileds with regexp pattern (in the example below, it is currency field). I move them to definitions and tried to generate server. Unfortunatelly, there isn't any Pattern annotation in generated java code. If I move Currency directly to Amount, Pattern annotation is correctly generated.

Swagger-codegen version

swagger-codegen-maven-plugin 2.3.1
swagger: 2.0

Swagger declaration file content or url

If you post the code inline, please wrap it with

definitions:
  Amount:
    type: object
    description: >
      some description
    properties:
      value:
        format: double
        type: number
        minimum: 0.01
        maximum: 1000000000000000
        description: >
          some description
      currency:
        $ref: '#/definitions/Currency'
    required:
      - value
      - currency
  Currency:
    type: string
    pattern: '^[A-Z]{3,3}$'
    description: >
      some description

Generated Java code:

package generated.rest.model;

import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
import javax.validation.constraints.*;

/**
 * some description
 */
@ApiModel(description = "some description")
@Validated
@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2018-04-06T07:36:11.106+02:00")

public class AmountGen  implements Serializable {
  private static final long serialVersionUID = 1L;

  @JsonProperty("value")
  private Double value = null;

  @JsonProperty("currency")
  private String currency = null;

  public AmountGen value(Double value) {
    this.value = value;
    return this;
  }

  /**
   * some description 
   * minimum: 0.01
   * maximum: 1000000000000000
   * @return value
  **/
  @ApiModelProperty(required = true, value = "some descirption")
  @NotNull

@DecimalMin("0.01") @DecimalMax("1000000000000000") 
  public Double getValue() {
    return value;
  }

  public void setValue(Double value) {
    this.value = value;
  }

  public AmountGen currency(String currency) {
    this.currency = currency;
    return this;
  }

  /**
   * Get currency
   * @return currency
  **/
  @ApiModelProperty(required = true, value = "")
  @NotNull


  public String getCurrency() {
    return currency;
  }

  public void setCurrency(String currency) {
    this.currency = currency;
  }


  @Override
  public boolean equals(java.lang.Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    AmountGen amount = (AmountGen) o;
    return Objects.equals(this.value, amount.value) &&
        Objects.equals(this.currency, amount.currency);
  }

  @Override
  public int hashCode() {
    return Objects.hash(value, currency);
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class AmountGen {\n");
    
    sb.append("    value: ").append(toIndentedString(value)).append("\n");
    sb.append("    currency: ").append(toIndentedString(currency)).append("\n");
    sb.append("}");
    return sb.toString();
  }

  /**
   * Convert the given object to string with each line indented by 4 spaces
   * (except the first line).
   */
  private String toIndentedString(java.lang.Object o) {
    if (o == null) {
      return "null";
    }
    return o.toString().replace("\n", "\n    ");
  }
}
Command line used for generation
Steps to reproduce

Config options:

  • library: spring-boot
  • java8: true
  • useBeanValidation: true
  • useOptional: true
Related issues/PRs
Suggest a fix/enhancement
@Michus001 Michus001 changed the title Missing @Pattern in generated code [Java] Missing @Pattern in generated code Apr 6, 2018
@Michus001 Michus001 changed the title [Java] Missing @Pattern in generated code [JAVA] Missing @Pattern in generated code Apr 6, 2018
@FatCash
Copy link

FatCash commented Apr 13, 2018

I have the same issues, can someone help out please?
How else to reuse pattern and validation constraints??
I filed a similar issue but no help yet: #8001

@FatCash
Copy link

FatCash commented Apr 23, 2018

Hi @Michus001, have you figured out how to generate pattern when using $ref with a simple String?
Any findings? I'm stuck on this...

@Michus001
Copy link
Author

@FatCash Unfortunatelly, no progress. I moved validations to my internal model and I don't rely on Swagger validations anymore.
As workaround, you can copy referenced definitions. It's quite silly solution, but it works.

@FatCash
Copy link

FatCash commented May 4, 2018

Thats too bad. I guess I will have to do the same since I cannot edit the API from the vendor :(
@wing328 do you have any time to check on this? Is this going to be resolved in 3.0.x ?

@HugoMario
Copy link
Contributor

hey @Michus001 @FatCash sorry for delay, i was waiting for swagger core and parser releases in order to proceed to fix this issue.

Anyway, this was fixed with PR #8277 , can you please verify it when you have a chance?

crumpf added a commit to crumpf/swagger-codegen that referenced this issue Jul 19, 2018
* master:
  Update README.md
  fixed NPE on `getHasQueryParams` method.
  Fix getHasQueryParams to take authMethods into account
  added null check.
  added pattern, mixLength, maxLength from "non object" model to a codegen property.
  updated dependencies versions on CI pom files.
  bump to released versions
  Fix getter methods for Java model with additionalProperties
  set required on response property when schema is not null.
  updated swagger core and swagger parser versions, also added tests to confirm that issue swagger-api#7980 is solved with these changes.
  fixed index out of bound exception for inline empty example field.
  Fix for uncompilable java client if no model definitions i specified (swagger-api#7836)
  Add option modelPropertyNaming to javascript generator
  Update PetStore sample
  Configuration option to disable HTML escaping when using Gson
@lubinson
Copy link

@HugoMario Have this issue been fixed in 3.0.0 rc1? I have the same issue using the latest snapshot of rc1. Thanks.

@HugoMario
Copy link
Contributor

Hi @lubinson, can you please try with https://github.com/swagger-api/swagger-codegen/releases/tag/v3.0.0 ?
and let me know if there is something wrong?

@RexpecT
Copy link

RexpecT commented Nov 6, 2018

@HugoMario I have dowloaded the latest swagger code generator(3.0.0), but facing the same issue. The pattern is completely missing from the generated code.

@dskvr
Copy link

dskvr commented Feb 8, 2019

Same.

@HardikAnghan
Copy link

Unable to generate the annotation for pattern

@onmax
Copy link

onmax commented Oct 23, 2020

Yeah same here. I have in my components/schema in the .yml a schema like this. I am using openapi: 3.0.0

StringWithPattern:
      type: string
      pattern: '^[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+\/=]*$'

The annotation for the regex is missing in the generated code

@ThorisoMa1
Copy link

Hi , has this issue been resolved as i am currently using openapi generator 6 and still unable to generate the regex patterns for the fields in the contract that have patterns. are there any workarounds or is there a particular version that would allow the models to generate with the required regex patterns

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 a pull request may close this issue.

9 participants