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

Validation Contraints of $ref (JSON Reference) components are not generated #8772

Closed
wrssmi opened this issue Oct 3, 2018 · 7 comments · Fixed by swagger-api/swagger-codegen-generators#232
Assignees
Milestone

Comments

@wrssmi
Copy link

wrssmi commented Oct 3, 2018

Description

When generating from an openapi 3.0.0 yaml the Validation-Contraints for Java Beans are not generated correctly if $ref ((JSON Reference) is used.

Swagger-codegen version

3.0.0
generatorName: jaxrs-spec

Swagger declaration file content or url
openapi: 3.0.1
paths:
  /accountReferenceIban:
    get:
    responses:
      '200':
      $ref: "#/components/schemas/accountReferenceIban"
components:
  schemas:
    accountReferenceIban:
      type: object
      required:
        - iban
      properties:
        iban:
          $ref: "#/components/schemas/iban"
        currency:
          $ref: "#/components/schemas/currencyCode"
    currencyCode:
      type: string
      pattern: "[A-Z]{3}"
    iban:
      type: string
      pattern: "[A-Z]{2,2}[0-9]{2,2}[a-zA-Z0-9]{1,30}"
Command line used for generation

java -jar swagger-codegen-cli-3.0.0.jar generate -i test.yaml -l jaxrs-spec --api-package=api --model-package=model

Steps to reproduce

Execute command line above. Beside 2 java empty JavaBeans (Iban and CurrrencyCode) the AccountReferenceIban Bean looks like this:

public class AccountReferenceIban  implements Serializable {
  private @Valid String iban = null;  private @Valid String currency = null;

  /**
   **/
  public AccountReferenceIban iban(String iban) {
    this.iban = iban;
    return this;
  }

  
  @ApiModelProperty(required = true, value = "")
  @JsonProperty("iban")
  @NotNull
  public String getIban() {
    return iban;
  }
  public void setIban(String iban) {
    this.iban = iban;
  }

  /**
   **/
  public AccountReferenceIban currency(String currency) {
    this.currency = currency;
    return this;
  }

  
  @ApiModelProperty(value = "")
  @JsonProperty("currency")
  public String getCurrency() {
    return currency;
  }
  public void setCurrency(String currency) {
    this.currency = currency;
  }

Expected Result
iban should have: @pattern(regexp="[A-Z]{2,2}[0-9]{2,2}[a-zA-Z0-9]{1,30}")
currencyCode should have: @pattern(regexp="[A-Z]{3}")

if input.yaml looks like the following the output is correct:

openapi: 3.0.1
paths:
  /accountReferenceIban:
    get:
    responses:
      '200':
      $ref: "#/components/schemas/accountReferenceIban"
components:
  schemas:
    accountReferenceIban:
      type: object
      required:
        - iban
      properties:
        iban:
          type: string
          pattern: "[A-Z]{2,2}[0-9]{2,2}[a-zA-Z0-9]{1,30}"
        currency:
          type: string
          pattern: "[A-Z]{3}"

java bean output:

public class AccountReferenceIban  implements Serializable {
  private @Valid String iban = null;  private @Valid String currency = null;

  /**
   **/
  public AccountReferenceIban iban(String iban) {
    this.iban = iban;
    return this;
  }

  
  @ApiModelProperty(required = true, value = "")
  @JsonProperty("iban")
  @NotNull
 @Pattern(regexp="[A-Z]{2,2}[0-9]{2,2}[a-zA-Z0-9]{1,30}")  public String getIban() {
    return iban;
  }
  public void setIban(String iban) {
    this.iban = iban;
  }

  /**
   **/
  public AccountReferenceIban currency(String currency) {
    this.currency = currency;
    return this;
  }

  
  @ApiModelProperty(value = "")
  @JsonProperty("currency")
 @Pattern(regexp="[A-Z]{3}")  public String getCurrency() {
    return currency;
  }
  public void setCurrency(String currency) {
    this.currency = currency;
  }
@reisi007
Copy link

Hi,

I see the same problem. However, the format seems to be ignored as well. Is there anything we can do to help you resoplve this?

@HugoMario
Copy link
Contributor

hey @wrssmi @reisi007 sorry for delay, i'm going to work to fix this

@HugoMario
Copy link
Contributor

@wrssmi @reisi007 can you please check this fix and let me know if issue is still present?

@HugoMario HugoMario self-assigned this Nov 8, 2018
@wrssmi
Copy link
Author

wrssmi commented Nov 8, 2018

thank you for your commit. but it is not working yet.
you missed to add the x-has-validation in case of a pattern/minLength/maxLength

if (propertySchema.get$ref() != null) {
	openAPIUtil.addPropertiesFromRef(propertySchema, cp);
}
//NEED TO BE ADDED
if (cp.pattern != null || cp.minLength != null || cp.maxLength != null) {
	codegenModel.getVendorExtensions().put(HAS_VALIDATION_EXT_NAME, Boolean.TRUE);
}

boolean hasRequired = getBooleanValue(codegenModel, HAS_REQUIRED_EXT_NAME) || cp.

@wrssmi
Copy link
Author

wrssmi commented Dec 7, 2018

not working yet!

@HugoMario
Copy link
Contributor

hey @wrssmi, i just added a new PR adding the validation. can you please check this version and let me know if it works as expected?

@HugoMario
Copy link
Contributor

going to close this issue but if there is something wrong, please let me know.

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

Successfully merging a pull request may close this issue.

3 participants