@Pattern is not generated for scalar types
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.
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 https://github.com/swagger-api/swagger-codegen/issues/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.
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.
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();
}