Skip to content

@Pattern is not generated for scalar types #10989

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

Open
cdprete opened this issue Apr 26, 2021 · 3 comments
Open

@Pattern is not generated for scalar types #10989

cdprete opened this issue Apr 26, 2021 · 3 comments

Comments

@cdprete
Copy link

cdprete commented Apr 26, 2021

Hi.
Given the following definitions:

swagger: '2.0'
info:
  title: MyDomainObject
  version: "1.0"
definitions:
  MyDomainObject:
      type: object
      required:
        - currency
      properties:
        currency:
          $ref: '#/definitions/CurrencyIso'
        riskCurrency:
          $ref: '#/definitions/CurrencyIso'

  CurrencyIso:
    description: |
      The ISO currency definition.
    type: string
    pattern: ^[A-Z]{3}|[0-9]{3}$
    example: CHF
paths: {}

when the class is generated, no @Pattern annotation is inserted on the getter of the fields referencing the CurrencyIso.
I'm actually using the swagger-codegen-maven-plugin with version 2.3.1.
It seems that version 2.4.x fixes this issue, but then the patterns are not escaped when generated, leading to not compilable code in certain cases (e.g., if it contains \d+). There is already this issue about it.

@cdprete
Copy link
Author

cdprete commented Apr 26, 2021

More on this. In version 2.4.19 the pattern is "correctly" added but only if the property is directly specified and with an example.
If the example is missing, then the issue in #9509 occurs; on the other end, if the scalar property is referenced through a $ref the pattern is copied without the example leading to the same compilation issue.

@cyChop
Copy link

cyChop commented Aug 5, 2022

Just made a test with version 3.0.34. The issue is still present, though it seems to work when directly specified even when no example is present.

@cyChop
Copy link

cyChop commented Aug 8, 2022

Hi, I had limited time to investigate the issue and wanted to try a fix, but a full day wasn't enough for me to grasp the subtleties of the project structure (codegen v3 depends on codegen-generators, which depends on codegen v3?) and my attempts at building a maven plugin v3 never included the changes I made in codegen v2.

If someone with a better knowledge of the project were willing to have a look at whether my attempt makes sense and make the PR if it does, I'd be delighted and thankful.

I detected only one occurrence in DefaultCodegen where the pattern was not escaped and wanted to test the behaviour if the escape was added there:

@@ -3322,7 +3324,8 @@ public class DefaultCodegen {
                     Model model =  allDefinitions.get(refProperty.getSimpleRef());
                     if (model instanceof ModelImpl) {
                         ModelImpl modelImpl = (ModelImpl) model;
-                        cp.pattern = modelImpl.getPattern();
+                        cp.pattern = toRegularExpression(modelImpl.getPattern());
                         cp.minLength = modelImpl.getMinLength();
                         cp.maxLength = modelImpl.getMaxLength();
                     }

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

No branches or pull requests

2 participants