From 5d49b916aab6ebcc9c5a399a9a7fbafc2ad9c406 Mon Sep 17 00:00:00 2001 From: Jeremie Bresson Date: Tue, 15 May 2018 13:22:19 +0200 Subject: [PATCH] Consider minLength, maxLength and pattern in referenced schema --- .../openapitools/codegen/DefaultCodegen.java | 22 +++--- .../codegen/java/JavaModelTest.java | 78 +++++++++++++++++++ 2 files changed, 91 insertions(+), 9 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index 79cd8db2780e..b56b03e63f13 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -1519,7 +1519,7 @@ public CodegenModel fromModel(String name, Schema schema, Map al addProperties(allProperties, allRequired, child, allDefinitions); } } - addVars(m, properties, required, allProperties, allRequired); + addVars(m, properties, required, allProperties, allRequired, allDefinitions); // TODO //} else if (schema instanceof RefModel) { } else { @@ -1533,7 +1533,7 @@ public CodegenModel fromModel(String name, Schema schema, Map al if (ModelUtils.isMapSchema(schema)) { addAdditionPropertiesToCodeGenModel(m, schema); } - addVars(m, schema.getProperties(), schema.getRequired()); + addVars(m, schema.getProperties(), schema.getRequired(), allDefinitions); } if (m.vars != null) { @@ -3095,12 +3095,12 @@ protected void addImport(CodegenModel m, String type) { } } - private void addVars(CodegenModel model, Map properties, List required) { - addVars(model, properties, required, null, null); + private void addVars(CodegenModel model, Map properties, List required, Map allDefinitions) { + addVars(model, properties, required, null, null, allDefinitions); } private void addVars(CodegenModel m, Map properties, List required, - Map allProperties, List allRequired) { + Map allProperties, List allRequired, Map allDefinitions) { m.hasRequired = false; if (properties != null && !properties.isEmpty()) { @@ -3109,7 +3109,7 @@ private void addVars(CodegenModel m, Map properties, List mandatory = required == null ? Collections.emptySet() : new TreeSet(required); - addVars(m, m.vars, properties, mandatory); + addVars(m, m.vars, properties, mandatory, allDefinitions); m.allMandatory = m.mandatory = mandatory; } else { m.emptyVars = true; @@ -3120,12 +3120,12 @@ private void addVars(CodegenModel m, Map properties, List allMandatory = allRequired == null ? Collections.emptySet() : new TreeSet(allRequired); - addVars(m, m.allVars, allProperties, allMandatory); + addVars(m, m.allVars, allProperties, allMandatory, allDefinitions); m.allMandatory = allMandatory; } } - private void addVars(CodegenModel m, List vars, Map properties, Set mandatory) { + private void addVars(CodegenModel m, List vars, Map properties, Set mandatory, Map allDefinitions) { // convert set to list so that we can access the next entry in the loop List> propertyList = new ArrayList>(properties.entrySet()); final int totalCount = propertyList.size(); @@ -3133,7 +3133,11 @@ private void addVars(CodegenModel m, List vars, Map entry = propertyList.get(i); final String key = entry.getKey(); - final Schema prop = entry.getValue(); + Schema prop = entry.getValue(); + if (allDefinitions != null && prop != null && StringUtils.isNotEmpty(prop.get$ref())) { + String refName = ModelUtils.getSimpleRef(prop.get$ref()); + prop = allDefinitions.get(refName); + } if (prop == null) { LOGGER.warn("null property for " + key); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaModelTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaModelTest.java index 957efb3739ae..482651ce6611 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaModelTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaModelTest.java @@ -58,6 +58,7 @@ import java.io.File; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -866,6 +867,83 @@ public void longPropertyInReferencedSchemaTest() { Assert.assertEquals(cp2.getter, "getLong2"); } + @Test(description = "convert string property") + public void stringPropertyTest() { + final Schema property = new StringSchema().maxLength(10).minLength(3).pattern("^[A-Z]+$"); + final DefaultCodegen codegen = new JavaClientCodegen(); + final CodegenProperty cp = codegen.fromProperty("somePropertyWithMinMaxAndPattern", property); + + Assert.assertEquals(cp.baseName, "somePropertyWithMinMaxAndPattern"); + Assert.assertEquals(cp.nameInCamelCase, "SomePropertyWithMinMaxAndPattern"); + Assert.assertEquals(cp.nameInSnakeCase, "SOME_PROPERTY_WITH_MIN_MAX_AND_PATTERN"); + Assert.assertEquals(cp.datatype, "String"); + Assert.assertEquals(cp.name, "somePropertyWithMinMaxAndPattern"); + Assert.assertEquals(cp.baseType, "String"); + Assert.assertTrue(cp.isNotContainer); + Assert.assertFalse(cp.isLong); + Assert.assertFalse(cp.isInteger); + Assert.assertTrue(cp.isString); + Assert.assertEquals(cp.getter, "getSomePropertyWithMinMaxAndPattern"); + Assert.assertEquals(cp.minLength, Integer.valueOf(3)); + Assert.assertEquals(cp.maxLength, Integer.valueOf(10)); + Assert.assertEquals(cp.pattern, "^[A-Z]+$"); + } + + @Test(description = "convert string property in an object") + public void stringPropertyInObjectTest() { + final Schema property = new StringSchema().maxLength(10).minLength(3).pattern("^[A-Z]+$"); + final Schema myObject = new ObjectSchema().addProperties("somePropertyWithMinMaxAndPattern", property); + + final DefaultCodegen codegen = new JavaClientCodegen(); + CodegenModel cm = codegen.fromModel("myObject", myObject, Collections.singletonMap("myObject", myObject)); + + Assert.assertEquals(cm.getVars().size(), 1); + CodegenProperty cp = cm.getVars().get(0); + Assert.assertEquals(cp.baseName, "somePropertyWithMinMaxAndPattern"); + Assert.assertEquals(cp.nameInCamelCase, "SomePropertyWithMinMaxAndPattern"); + Assert.assertEquals(cp.nameInSnakeCase, "SOME_PROPERTY_WITH_MIN_MAX_AND_PATTERN"); + Assert.assertEquals(cp.datatype, "String"); + Assert.assertEquals(cp.name, "somePropertyWithMinMaxAndPattern"); + Assert.assertEquals(cp.baseType, "String"); + Assert.assertTrue(cp.isNotContainer); + Assert.assertFalse(cp.isLong); + Assert.assertFalse(cp.isInteger); + Assert.assertTrue(cp.isString); + Assert.assertEquals(cp.getter, "getSomePropertyWithMinMaxAndPattern"); + Assert.assertEquals(cp.minLength, Integer.valueOf(3)); + Assert.assertEquals(cp.maxLength, Integer.valueOf(10)); + Assert.assertEquals(cp.pattern, "^[A-Z]+$"); + } + + @Test(description = "convert referenced string property in an object") + public void stringPropertyReferencedInObjectTest() { + final Schema property = new StringSchema().maxLength(10).minLength(3).pattern("^[A-Z]+$"); + final Schema myObject = new ObjectSchema().addProperties("somePropertyWithMinMaxAndPattern", new ObjectSchema().$ref("refObj")); + + final DefaultCodegen codegen = new JavaClientCodegen(); + Map schemaMap = new HashMap<>(); + schemaMap.put("myObject", myObject); + schemaMap.put("refObj", property); + CodegenModel cm = codegen.fromModel("myObject", myObject, schemaMap); + + Assert.assertEquals(cm.getVars().size(), 1); + CodegenProperty cp = cm.getVars().get(0); + Assert.assertEquals(cp.baseName, "somePropertyWithMinMaxAndPattern"); + Assert.assertEquals(cp.nameInCamelCase, "SomePropertyWithMinMaxAndPattern"); + Assert.assertEquals(cp.nameInSnakeCase, "SOME_PROPERTY_WITH_MIN_MAX_AND_PATTERN"); + Assert.assertEquals(cp.datatype, "String"); + Assert.assertEquals(cp.name, "somePropertyWithMinMaxAndPattern"); + Assert.assertEquals(cp.baseType, "String"); + Assert.assertTrue(cp.isNotContainer); + Assert.assertFalse(cp.isLong); + Assert.assertFalse(cp.isInteger); + Assert.assertTrue(cp.isString); + Assert.assertEquals(cp.getter, "getSomePropertyWithMinMaxAndPattern"); + Assert.assertEquals(cp.minLength, Integer.valueOf(3)); + Assert.assertEquals(cp.maxLength, Integer.valueOf(10)); + Assert.assertEquals(cp.pattern, "^[A-Z]+$"); + } + @Test(description = "convert an array schema") public void arraySchemaTest() { final Schema testSchema = new ObjectSchema()