Consider minLength, maxLength and pattern in referenced schema (#45)

This commit is contained in:
Jérémie Bresson 2018-05-16 09:19:16 +02:00 committed by GitHub
parent 9d7feaaeba
commit 6b8079808b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 9 deletions

View File

@ -1519,7 +1519,7 @@ public class DefaultCodegen implements CodegenConfig {
addProperties(allProperties, allRequired, child, allDefinitions); addProperties(allProperties, allRequired, child, allDefinitions);
} }
} }
addVars(m, properties, required, allProperties, allRequired); addVars(m, properties, required, allProperties, allRequired, allDefinitions);
// TODO // TODO
//} else if (schema instanceof RefModel) { //} else if (schema instanceof RefModel) {
} else { } else {
@ -1533,7 +1533,7 @@ public class DefaultCodegen implements CodegenConfig {
if (ModelUtils.isMapSchema(schema)) { if (ModelUtils.isMapSchema(schema)) {
addAdditionPropertiesToCodeGenModel(m, schema); addAdditionPropertiesToCodeGenModel(m, schema);
} }
addVars(m, schema.getProperties(), schema.getRequired()); addVars(m, schema.getProperties(), schema.getRequired(), allDefinitions);
} }
if (m.vars != null) { if (m.vars != null) {
@ -3095,12 +3095,12 @@ public class DefaultCodegen implements CodegenConfig {
} }
} }
private void addVars(CodegenModel model, Map<String, Schema> properties, List<String> required) { private void addVars(CodegenModel model, Map<String, Schema> properties, List<String> required, Map<String, Schema> allDefinitions) {
addVars(model, properties, required, null, null); addVars(model, properties, required, null, null, allDefinitions);
} }
private void addVars(CodegenModel m, Map<String, Schema> properties, List<String> required, private void addVars(CodegenModel m, Map<String, Schema> properties, List<String> required,
Map<String, Schema> allProperties, List<String> allRequired) { Map<String, Schema> allProperties, List<String> allRequired, Map<String, Schema> allDefinitions) {
m.hasRequired = false; m.hasRequired = false;
if (properties != null && !properties.isEmpty()) { if (properties != null && !properties.isEmpty()) {
@ -3109,7 +3109,7 @@ public class DefaultCodegen implements CodegenConfig {
Set<String> mandatory = required == null ? Collections.<String>emptySet() Set<String> mandatory = required == null ? Collections.<String>emptySet()
: new TreeSet<String>(required); : new TreeSet<String>(required);
addVars(m, m.vars, properties, mandatory); addVars(m, m.vars, properties, mandatory, allDefinitions);
m.allMandatory = m.mandatory = mandatory; m.allMandatory = m.mandatory = mandatory;
} else { } else {
m.emptyVars = true; m.emptyVars = true;
@ -3120,12 +3120,12 @@ public class DefaultCodegen implements CodegenConfig {
if (allProperties != null) { if (allProperties != null) {
Set<String> allMandatory = allRequired == null ? Collections.<String>emptySet() Set<String> allMandatory = allRequired == null ? Collections.<String>emptySet()
: new TreeSet<String>(allRequired); : new TreeSet<String>(allRequired);
addVars(m, m.allVars, allProperties, allMandatory); addVars(m, m.allVars, allProperties, allMandatory, allDefinitions);
m.allMandatory = allMandatory; m.allMandatory = allMandatory;
} }
} }
private void addVars(CodegenModel m, List<CodegenProperty> vars, Map<String, Schema> properties, Set<String> mandatory) { private void addVars(CodegenModel m, List<CodegenProperty> vars, Map<String, Schema> properties, Set<String> mandatory, Map<String, Schema> allDefinitions) {
// convert set to list so that we can access the next entry in the loop // convert set to list so that we can access the next entry in the loop
List<Map.Entry<String, Schema>> propertyList = new ArrayList<Map.Entry<String, Schema>>(properties.entrySet()); List<Map.Entry<String, Schema>> propertyList = new ArrayList<Map.Entry<String, Schema>>(properties.entrySet());
final int totalCount = propertyList.size(); final int totalCount = propertyList.size();
@ -3133,7 +3133,11 @@ public class DefaultCodegen implements CodegenConfig {
Map.Entry<String, Schema> entry = propertyList.get(i); Map.Entry<String, Schema> entry = propertyList.get(i);
final String key = entry.getKey(); 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) { if (prop == null) {
LOGGER.warn("null property for " + key); LOGGER.warn("null property for " + key);

View File

@ -58,6 +58,7 @@ import org.testng.annotations.Test;
import java.io.File; import java.io.File;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -866,6 +867,83 @@ public class JavaModelTest {
Assert.assertEquals(cp2.getter, "getLong2"); 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<String, Schema> 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") @Test(description = "convert an array schema")
public void arraySchemaTest() { public void arraySchemaTest() {
final Schema testSchema = new ObjectSchema() final Schema testSchema = new ObjectSchema()