use of org.jsonschema2pojo.Schema in project jsonschema2pojo by joelittlejohn.
the class ArrayRuleTest method arrayWithNonUniqueItemsProducesList.
@Test
public void arrayWithNonUniqueItemsProducesList() {
JCodeModel codeModel = new JCodeModel();
JPackage jpackage = codeModel._package(getClass().getPackage().getName());
ObjectMapper mapper = new ObjectMapper();
ObjectNode itemsNode = mapper.createObjectNode();
itemsNode.put("type", "number");
ObjectNode propertyNode = mapper.createObjectNode();
propertyNode.set("uniqueItems", BooleanNode.FALSE);
propertyNode.set("items", itemsNode);
Schema schema = mock(Schema.class);
when(schema.getId()).thenReturn(URI.create("http://example/nonUniqueArray"));
when(config.isUseDoubleNumbers()).thenReturn(true);
JClass propertyType = rule.apply("fooBars", propertyNode, jpackage, schema);
assertThat(propertyType, notNullValue());
assertThat(propertyType.erasure(), is(codeModel.ref(List.class)));
assertThat(propertyType.getTypeParameters().get(0).fullName(), is(Double.class.getName()));
}
use of org.jsonschema2pojo.Schema in project jsonschema2pojo by joelittlejohn.
the class PropertyRule method resolveRefs.
private JsonNode resolveRefs(JsonNode node, Schema parent) {
if (node.has("$ref")) {
Schema refSchema = ruleFactory.getSchemaStore().create(parent, node.get("$ref").asText(), ruleFactory.getGenerationConfig().getRefFragmentPathDelimiters());
JsonNode refNode = refSchema.getContent();
return resolveRefs(refNode, refSchema);
} else {
return node;
}
}
use of org.jsonschema2pojo.Schema in project jsonschema2pojo by joelittlejohn.
the class PropertyRule method apply.
/**
* Applies this schema rule to take the required code generation steps.
* <p>
* This rule adds a property to a given Java class according to the Java
* Bean spec. A private field is added to the class, along with accompanying
* accessor methods.
* <p>
* If this rule's schema mapper is configured to include builder methods
* (see {@link GenerationConfig#isGenerateBuilders()} ),
* then a builder method of the form <code>withFoo(Foo foo);</code> is also
* added.
*
* @param nodeName the name of the property to be applied
* @param node the node describing the characteristics of this property
* @param parent the parent node
* @param jclass the Java class which should have this property added
* @return the given jclass
*/
@Override
public JDefinedClass apply(String nodeName, JsonNode node, JsonNode parent, JDefinedClass jclass, Schema schema) {
String propertyName = ruleFactory.getNameHelper().getPropertyName(nodeName, node);
String pathToProperty;
if (schema.getId() == null || schema.getId().getFragment() == null) {
pathToProperty = "#/properties/" + nodeName;
} else {
pathToProperty = "#" + schema.getId().getFragment() + "/properties/" + nodeName;
}
Schema propertySchema = ruleFactory.getSchemaStore().create(schema, pathToProperty, ruleFactory.getGenerationConfig().getRefFragmentPathDelimiters());
JType propertyType = ruleFactory.getSchemaRule().apply(nodeName, node, parent, jclass, propertySchema);
propertySchema.setJavaTypeIfEmpty(propertyType);
boolean isIncludeGetters = ruleFactory.getGenerationConfig().isIncludeGetters();
boolean isIncludeSetters = ruleFactory.getGenerationConfig().isIncludeSetters();
node = resolveRefs(node, schema);
int accessModifier = isIncludeGetters || isIncludeSetters ? JMod.PRIVATE : JMod.PUBLIC;
JFieldVar field = jclass.field(accessModifier, propertyType, propertyName);
propertyAnnotations(nodeName, node, schema, field);
formatAnnotation(field, jclass, node);
ruleFactory.getAnnotator().propertyField(field, jclass, nodeName, node);
if (isIncludeGetters) {
JMethod getter = addGetter(jclass, field, nodeName, node, isRequired(nodeName, node, schema), useOptional(nodeName, node, schema));
ruleFactory.getAnnotator().propertyGetter(getter, jclass, nodeName);
propertyAnnotations(nodeName, node, schema, getter);
}
if (isIncludeSetters) {
JMethod setter = addSetter(jclass, field, nodeName, node);
ruleFactory.getAnnotator().propertySetter(setter, jclass, nodeName);
propertyAnnotations(nodeName, node, schema, setter);
}
if (ruleFactory.getGenerationConfig().isGenerateBuilders()) {
addBuilderMethod(jclass, field, nodeName, node);
}
if (node.has("pattern")) {
ruleFactory.getPatternRule().apply(nodeName, node.get("pattern"), node, field, schema);
}
ruleFactory.getDefaultRule().apply(nodeName, node.get("default"), node, field, schema);
ruleFactory.getMinimumMaximumRule().apply(nodeName, node, parent, field, schema);
ruleFactory.getMinItemsMaxItemsRule().apply(nodeName, node, parent, field, schema);
ruleFactory.getMinLengthMaxLengthRule().apply(nodeName, node, parent, field, schema);
ruleFactory.getDigitsRule().apply(nodeName, node, parent, field, schema);
if (isObject(node) || isArray(node)) {
ruleFactory.getValidRule().apply(nodeName, node, parent, field, schema);
}
return jclass;
}
use of org.jsonschema2pojo.Schema in project jsonschema2pojo by joelittlejohn.
the class ArrayRule method apply.
/**
* <p>Applies this schema rule to take the required code generation steps.</p>
*
* <p>When constructs of type "array" appear in the schema, these are mapped to
* Java collections in the generated POJO. If the array is marked as having
* "uniqueItems" then the resulting Java type is {@link Set}, if not, then
* the resulting Java type is {@link List}. The schema given by "items" will
* decide the generic type of the collection.</p>
*
* <p>If the "items" property requires newly generated types, then the type
* name will be the singular version of the nodeName (unless overridden by
* the javaType property) e.g.
* <pre>
* "fooBars" : {"type":"array", "uniqueItems":"true", "items":{type:"object"}}
* ==>
* {@code Set<FooBar> getFooBars(); }
* </pre>
* </p>
*
* @param nodeName
* the name of the property which has type "array"
* @param node
* the schema "type" node
* @param parent
* the parent node
* @param jpackage
* the package into which newly generated types should be added
* @return the Java type associated with this array rule, either {@link Set}
* or {@link List}, narrowed by the "items" type
*/
@Override
public JClass apply(String nodeName, JsonNode node, JsonNode parent, JPackage jpackage, Schema schema) {
boolean uniqueItems = node.has("uniqueItems") && node.get("uniqueItems").asBoolean();
boolean rootSchemaIsArray = !schema.isGenerated();
JType itemType;
if (node.has("items")) {
String pathToItems;
if (schema.getId() == null || schema.getId().getFragment() == null) {
pathToItems = "#/items";
} else {
pathToItems = "#" + schema.getId().getFragment() + "/items";
}
Schema itemsSchema = ruleFactory.getSchemaStore().create(schema, pathToItems, ruleFactory.getGenerationConfig().getRefFragmentPathDelimiters());
itemType = ruleFactory.getSchemaRule().apply(makeSingular(nodeName), node.get("items"), node, jpackage, itemsSchema);
itemsSchema.setJavaTypeIfEmpty(itemType);
} else {
itemType = jpackage.owner().ref(Object.class);
}
JClass arrayType;
if (uniqueItems) {
arrayType = jpackage.owner().ref(Set.class).narrow(itemType);
} else {
arrayType = jpackage.owner().ref(List.class).narrow(itemType);
}
if (rootSchemaIsArray) {
schema.setJavaType(arrayType);
}
return arrayType;
}
use of org.jsonschema2pojo.Schema in project jsonschema2pojo by joelittlejohn.
the class ReflectionHelper method getSuperType.
public JType getSuperType(String nodeName, JsonNode node, JPackage jPackage, Schema schema) {
if (node.has("extends") && node.has("extendsJavaClass")) {
throw new IllegalStateException("'extends' and 'extendsJavaClass' defined simultaneously");
}
JType superType = jPackage.owner().ref(Object.class);
Schema superTypeSchema = getSuperSchema(node, schema, false);
if (superTypeSchema != null) {
superType = ruleFactory.getSchemaRule().apply(nodeName + "Parent", node.get("extends"), node, jPackage, superTypeSchema);
} else if (node.has("extendsJavaClass")) {
superType = resolveType(jPackage, node.get("extendsJavaClass").asText());
}
return superType;
}
Aggregations