Swagger-codegen: [JavaSpring] Bug generating @Pattern regex

Created on 20 Jun 2019  路  5Comments  路  Source: swagger-api/swagger-codegen

Description

When generating from this yaml file:
https://raw.githubusercontent.com/OpenBankingUK/account-info-api-spec/master/dist/v2.0.0/account-info-swagger.yaml

Regex pattern like this '^\d{1,13}.\d{1,5}$' are not escape property and the generated java code does not compile.

To discuss: Should the code generation be responsible for escaping the regex properly for every language? or Should I modify the YAML to provide the appropriated escape?

Swagger-codegen version

3.0.8

Swagger declaration file content or url
definitions:
  Amount:
    type: string
    pattern: '^\d{1,13}\.\d{1,5}$'
Command line used for generation

swagger-codegen generate -i https://raw.githubusercontent.com/OpenBankingUK/account-info-api-spec/master/dist/v2.0.0/account-info-swagger.yaml -l spring -o api-server

Steps to reproduce

swagger-codegen generate -i https://raw.githubusercontent.com/OpenBankingUK/account-info-api-spec/master/dist/v2.0.0/account-info-swagger.yaml -l spring -o api-account
mvn install

Related issues/PRs
Suggest a fix/enhancement

Wrong java code generated:

@Pattern(regexp="^\d{1,13}\.\d{1,5}$")   

Right code generated should be: \

@Pattern(regexp="^\\d{1,13}\\.\\d{1,5}$")   

Most helpful comment

Still a problem with 3.0.21

All 5 comments

I'm having the same problem now ... we have just updated to the version from 2.3.1 to 2.4.5

Facing the same issue, while working with https://github.com/jdegre/5GC_APIs/raw/master/TS29571_CommonData.yaml

``yaml Mnc: type: string pattern: '^\d{2,3}$'

Generated output includes below error: java: illegal escape character

```java
@Pattern(regexp="^\d{2,3}$") public String getMnc() {
return mnc;
}

`

Still a problem with 3.0.21

and also in 3.0.23. This is open since nearly 2 years. Does anyone have a workaround?

Hi @volkerrichert ,

Yes, we're working on this, and any PR is always welcome

Was this page helpful?
0 / 5 - 0 ratings