Skip to content

Commit da55136

Browse files
committed
added pattern, mixLength, maxLength from "non object" model to a codegen property.
1 parent a4d6d7b commit da55136

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,7 +1442,7 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
14421442
addProperties(allProperties, allRequired, child, allDefinitions);
14431443
}
14441444
}
1445-
addVars(m, properties, required, allProperties, allRequired);
1445+
addVars(m, properties, required, allDefinitions, allProperties, allRequired);
14461446
} else {
14471447
ModelImpl impl = (ModelImpl) model;
14481448
if (impl.getType() != null) {
@@ -1458,7 +1458,7 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
14581458
if (impl.getAdditionalProperties() != null) {
14591459
addAdditionPropertiesToCodeGenModel(m, impl);
14601460
}
1461-
addVars(m, impl.getProperties(), impl.getRequired());
1461+
addVars(m, impl.getProperties(), impl.getRequired(), allDefinitions);
14621462
}
14631463

14641464
if (m.vars != null) {
@@ -3092,11 +3092,11 @@ protected void addImport(CodegenModel m, String type) {
30923092
}
30933093
}
30943094

3095-
private void addVars(CodegenModel m, Map<String, Property> properties, List<String> required) {
3096-
addVars(m, properties, required, null, null);
3095+
private void addVars(CodegenModel m, Map<String, Property> properties, List<String> required, Map<String, Model> allDefinitions) {
3096+
addVars(m, properties, required, allDefinitions, null, null);
30973097
}
30983098

3099-
private void addVars(CodegenModel m, Map<String, Property> properties, List<String> required,
3099+
private void addVars(CodegenModel m, Map<String, Property> properties, List<String> required, Map<String, Model> allDefinitions,
31003100
Map<String, Property> allProperties, List<String> allRequired) {
31013101

31023102
m.hasRequired = false;
@@ -3107,7 +3107,7 @@ private void addVars(CodegenModel m, Map<String, Property> properties, List<Stri
31073107

31083108
Set<String> mandatory = required == null ? Collections.<String> emptySet()
31093109
: new TreeSet<String>(required);
3110-
addVars(m, m.vars, properties, mandatory);
3110+
addVars(m, m.vars, properties, mandatory, allDefinitions);
31113111
m.allMandatory = m.mandatory = mandatory;
31123112
} else {
31133113
m.emptyVars = true;
@@ -3118,12 +3118,12 @@ private void addVars(CodegenModel m, Map<String, Property> properties, List<Stri
31183118
if (allProperties != null) {
31193119
Set<String> allMandatory = allRequired == null ? Collections.<String> emptySet()
31203120
: new TreeSet<String>(allRequired);
3121-
addVars(m, m.allVars, allProperties, allMandatory);
3121+
addVars(m, m.allVars, allProperties, allMandatory, allDefinitions);
31223122
m.allMandatory = allMandatory;
31233123
}
31243124
}
31253125

3126-
private void addVars(CodegenModel m, List<CodegenProperty> vars, Map<String, Property> properties, Set<String> mandatory) {
3126+
private void addVars(CodegenModel m, List<CodegenProperty> vars, Map<String, Property> properties, Set<String> mandatory, Map<String, Model> allDefinitions) {
31273127
// convert set to list so that we can access the next entry in the loop
31283128
List<Map.Entry<String, Property>> propertyList = new ArrayList<Map.Entry<String, Property>>(properties.entrySet());
31293129
final int totalCount = propertyList.size();
@@ -3146,6 +3146,17 @@ private void addVars(CodegenModel m, List<CodegenProperty> vars, Map<String, Pro
31463146
m.hasEnums = true;
31473147
}
31483148

3149+
if (prop instanceof RefProperty) {
3150+
RefProperty refProperty = (RefProperty) prop;
3151+
Model model = allDefinitions.get(refProperty.getSimpleRef());
3152+
if (model instanceof ModelImpl) {
3153+
ModelImpl modelImpl = (ModelImpl) model;
3154+
cp.pattern = modelImpl.getPattern();
3155+
cp.minLength = modelImpl.getMinLength();
3156+
cp.maxLength = modelImpl.getMaxLength();
3157+
}
3158+
}
3159+
31493160
// set model's hasOnlyReadOnly to false if the property is read-only
31503161
if (!Boolean.TRUE.equals(cp.isReadOnly)) {
31513162
m.hasOnlyReadOnly = false;

modules/swagger-codegen/src/test/java/io/swagger/codegen/CodegenTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,5 +439,18 @@ public void testPattern() throws Exception {
439439
ModelImpl currency = (ModelImpl) swagger.getDefinitions().get("Currency");
440440
Assert.assertNotNull(currency);
441441
Assert.assertEquals(currency.getPattern(), "^[A-Z]{3,3}$");
442+
443+
ModelImpl amount = (ModelImpl) swagger.getDefinitions().get("Amount");
444+
445+
final DefaultCodegen codegen = new DefaultCodegen();
446+
447+
final CodegenModel codegenModel = codegen.fromModel("Amount", amount, swagger.getDefinitions());
448+
for (CodegenProperty codegenProperty : codegenModel.vars) {
449+
if ("currency".equalsIgnoreCase(codegenProperty.name)) {
450+
Assert.assertEquals(codegenProperty.pattern, "^[A-Z]{3,3}$");
451+
break;
452+
}
453+
}
454+
442455
}
443456
}

0 commit comments

Comments
 (0)