Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Consider minLength, maxLength and pattern in referenced schema #45

Merged
merged 1 commit into from
May 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1519,7 +1519,7 @@ public CodegenModel fromModel(String name, Schema schema, Map<String, Schema> 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 {
Expand All @@ -1533,7 +1533,7 @@ public CodegenModel fromModel(String name, Schema schema, Map<String, Schema> 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) {
Expand Down Expand Up @@ -3095,12 +3095,12 @@ protected void addImport(CodegenModel m, String type) {
}
}

private void addVars(CodegenModel model, Map<String, Schema> properties, List<String> required) {
addVars(model, properties, required, null, null);
private void addVars(CodegenModel model, Map<String, Schema> properties, List<String> required, Map<String, Schema> allDefinitions) {
addVars(model, properties, required, null, null, allDefinitions);
}

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;
if (properties != null && !properties.isEmpty()) {
Expand All @@ -3109,7 +3109,7 @@ private void addVars(CodegenModel m, Map<String, Schema> properties, List<String

Set<String> mandatory = required == null ? Collections.<String>emptySet()
: new TreeSet<String>(required);
addVars(m, m.vars, properties, mandatory);
addVars(m, m.vars, properties, mandatory, allDefinitions);
m.allMandatory = m.mandatory = mandatory;
} else {
m.emptyVars = true;
Expand All @@ -3120,20 +3120,24 @@ private void addVars(CodegenModel m, Map<String, Schema> properties, List<String
if (allProperties != null) {
Set<String> allMandatory = allRequired == null ? Collections.<String>emptySet()
: new TreeSet<String>(allRequired);
addVars(m, m.allVars, allProperties, allMandatory);
addVars(m, m.allVars, allProperties, allMandatory, allDefinitions);
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
List<Map.Entry<String, Schema>> propertyList = new ArrayList<Map.Entry<String, Schema>>(properties.entrySet());
final int totalCount = propertyList.size();
for (int i = 0; i < totalCount; i++) {
Map.Entry<String, Schema> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@

import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -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<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")
public void arraySchemaTest() {
final Schema testSchema = new ObjectSchema()
Expand Down