Search in sources :

Example 1 with EnumDefinition

use of org.jsonschema2pojo.model.EnumDefinition in project jsonschema2pojo by joelittlejohn.

the class EnumRule method buildEnumDefinitionWithNoExtensions.

protected EnumDefinition buildEnumDefinitionWithNoExtensions(String nodeName, JsonNode parentNode, JsonNode enums, JType backingType) {
    ArrayList<EnumValueDefinition> enumValues = new ArrayList<>();
    Collection<String> existingConstantNames = new ArrayList<>();
    for (int i = 0; i < enums.size(); i++) {
        JsonNode value = enums.path(i);
        if (!value.isNull()) {
            String constantName = getConstantName(value.asText(), null);
            constantName = makeUnique(constantName, existingConstantNames);
            existingConstantNames.add(constantName);
            enumValues.add(new EnumValueDefinition(constantName, value.asText()));
        }
    }
    return new EnumDefinition(nodeName, parentNode, backingType, enumValues, EnumDefinitionExtensionType.NONE);
}
Also used : EnumValueDefinition(org.jsonschema2pojo.model.EnumValueDefinition) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) EnumDefinition(org.jsonschema2pojo.model.EnumDefinition)

Example 2 with EnumDefinition

use of org.jsonschema2pojo.model.EnumDefinition in project jsonschema2pojo by joelittlejohn.

the class EnumRule method buildEnumDefinitionWithJavaEnumNamesExtension.

protected EnumDefinition buildEnumDefinitionWithJavaEnumNamesExtension(String nodeName, JsonNode parentNode, JsonNode enums, JsonNode javaEnumNames, JType backingType) {
    ArrayList<EnumValueDefinition> enumValues = new ArrayList<>();
    Collection<String> existingConstantNames = new ArrayList<>();
    for (int i = 0; i < enums.size(); i++) {
        JsonNode value = enums.path(i);
        if (!value.isNull()) {
            String constantName = getConstantName(value.asText(), javaEnumNames.path(i).asText());
            constantName = makeUnique(constantName, existingConstantNames);
            existingConstantNames.add(constantName);
            enumValues.add(new EnumValueDefinition(constantName, value.asText(), javaEnumNames));
        }
    }
    return new EnumDefinition(nodeName, parentNode, backingType, enumValues, EnumDefinitionExtensionType.JAVA_ENUM_NAMES);
}
Also used : EnumValueDefinition(org.jsonschema2pojo.model.EnumValueDefinition) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) EnumDefinition(org.jsonschema2pojo.model.EnumDefinition)

Example 3 with EnumDefinition

use of org.jsonschema2pojo.model.EnumDefinition in project jsonschema2pojo by joelittlejohn.

the class EnumRule method buildEnumDefinition.

/**
 * Builds the effective definition of an enumeration is based on what schema elements are provided.
 * <p/>
 * This function determines which method it should delegate creating of the definition to:
 *
 * For "enum" handled by {@link #buildEnumDefinitionWithNoExtensions(String, JsonNode, JsonNode, JType)}
 * For "enum" and "javaEnums" handled by {@link #buildEnumDefinitionWithJavaEnumsExtension(String, JsonNode, JsonNode, JsonNode, JType)}
 * For "enum" and "javaEnumNames" handled by {@link #buildEnumDefinitionWithJavaEnumNamesExtension(String, JsonNode, JsonNode, JsonNode, JType)}
 *
 * @param nodeName
 *            the name of the property which is an "enum"
 * @param node
 *            the enum node
 * @param backingType
 *            the object backing the value of enum, most commonly this is a string
 *
 * @return the effective definition for enumeration
 */
protected EnumDefinition buildEnumDefinition(String nodeName, JsonNode node, JType backingType) {
    JsonNode enums = node.path("enum");
    JsonNode javaEnumNames = node.path("javaEnumNames");
    JsonNode javaEnums = node.path("javaEnums");
    RuleLogger logger = ruleFactory.getLogger();
    if (!javaEnums.isMissingNode() && !javaEnumNames.isMissingNode()) {
        logger.error("Both javaEnums and javaEnumNames provided; the property javaEnumNames will be ignored when both javaEnums and javaEnumNames are provided.");
    }
    if (!javaEnumNames.isMissingNode()) {
        logger.error("javaEnumNames is deprecated; please migrate to javaEnums.");
    }
    EnumDefinition enumDefinition;
    if (!javaEnums.isMissingNode()) {
        enumDefinition = buildEnumDefinitionWithJavaEnumsExtension(nodeName, node, enums, javaEnums, backingType);
    } else if (!javaEnumNames.isMissingNode()) {
        enumDefinition = buildEnumDefinitionWithJavaEnumNamesExtension(nodeName, node, enums, javaEnumNames, backingType);
    } else {
        enumDefinition = buildEnumDefinitionWithNoExtensions(nodeName, node, enums, backingType);
    }
    return enumDefinition;
}
Also used : RuleLogger(org.jsonschema2pojo.RuleLogger) JsonNode(com.fasterxml.jackson.databind.JsonNode) EnumDefinition(org.jsonschema2pojo.model.EnumDefinition)

Example 4 with EnumDefinition

use of org.jsonschema2pojo.model.EnumDefinition in project jsonschema2pojo by joelittlejohn.

the class EnumRule method apply.

/**
 * Applies this schema rule to take the required code generation steps.
 * <p>
 * A Java {@link Enum} is created, with constants for each of the enum
 * values present in the schema. The enum name is derived from the nodeName,
 * and the enum type itself is created as an inner class of the owning type.
 * In the rare case that no owning type exists (the enum is the root of the
 * schema), then the enum becomes a public class in its own right.
 * <p>
 * The actual JSON value for each enum constant is held in a property called
 * "value" in the generated type. A static factory method
 * <code>fromValue(String)</code> is added to the generated enum, and the
 * methods are annotated to allow Jackson to marshal/unmarshal values
 * correctly.
 *
 * @param nodeName
 *            the name of the property which is an "enum"
 * @param node
 *            the enum node
 * @param parent
 *            the parent node
 * @param container
 *            the class container (class or package) to which this enum
 *            should be added
 * @return the newly generated Java type that was created to represent the
 *         given enum
 */
@Override
public JType apply(String nodeName, JsonNode node, JsonNode parent, JClassContainer container, Schema schema) {
    if (node.has("existingJavaType")) {
        JType type = ruleFactory.getTypeRule().apply(nodeName, node, parent, container.getPackage(), schema);
        schema.setJavaTypeIfEmpty(type);
        return type;
    }
    JDefinedClass _enum;
    try {
        _enum = createEnum(node, nodeName, container);
    } catch (ClassAlreadyExistsException e) {
        ruleFactory.getLogger().error("Could not create enum.", e);
        return e.getExistingClass();
    }
    schema.setJavaTypeIfEmpty(_enum);
    // Add JavaDocs
    if (node.has("title")) {
        ruleFactory.getTitleRule().apply(nodeName, node.get("title"), node, _enum, schema);
    }
    if (node.has("description")) {
        ruleFactory.getDescriptionRule().apply(nodeName, node.get("description"), node, _enum, schema);
    }
    if (node.has("$comment")) {
        ruleFactory.getCommentRule().apply(nodeName, node.get("$comment"), node, _enum, schema);
    }
    if (node.has("javaInterfaces")) {
        addInterfaces(_enum, node.get("javaInterfaces"));
    }
    // copy our node; remove the javaType as it will throw off the TypeRule for our case
    ObjectNode typeNode = (ObjectNode) node.deepCopy();
    typeNode.remove("javaType");
    // If type is specified on the enum, get a type rule for it.  Otherwise, we're a string.
    // (This is different from the default of Object, which is why we don't do this for every case.)
    JType backingType = node.has("type") ? ruleFactory.getTypeRule().apply(nodeName, typeNode, parent, container, schema) : container.owner().ref(String.class);
    EnumDefinition enumDefinition = buildEnumDefinition(nodeName, node, backingType);
    if (ruleFactory.getGenerationConfig() != null && ruleFactory.getGenerationConfig().isIncludeGeneratedAnnotation()) {
        AnnotationHelper.addGeneratedAnnotation(_enum);
    }
    JFieldVar valueField = addConstructorAndFields(enumDefinition, _enum);
    // override toString only if we have a sensible string to return
    if (isString(backingType)) {
        addToString(_enum, valueField);
    }
    addFieldAccessors(_enum, valueField);
    addEnumConstants(enumDefinition, _enum, schema);
    addFactoryMethod(enumDefinition, _enum);
    applyCustomizations(enumDefinition, _enum);
    return _enum;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JDefinedClass(com.sun.codemodel.JDefinedClass) JFieldVar(com.sun.codemodel.JFieldVar) EnumDefinition(org.jsonschema2pojo.model.EnumDefinition) JType(com.sun.codemodel.JType) JClassAlreadyExistsException(com.sun.codemodel.JClassAlreadyExistsException) ClassAlreadyExistsException(org.jsonschema2pojo.exception.ClassAlreadyExistsException)

Example 5 with EnumDefinition

use of org.jsonschema2pojo.model.EnumDefinition in project jsonschema2pojo by joelittlejohn.

the class EnumRule method buildEnumDefinitionWithJavaEnumsExtension.

protected EnumDefinition buildEnumDefinitionWithJavaEnumsExtension(String nodeName, JsonNode enumNode, JsonNode enums, JsonNode javaEnums, JType type) {
    ArrayList<EnumValueDefinition> enumValues = new ArrayList<>();
    Collection<String> existingConstantNames = new ArrayList<>();
    for (int i = 0; i < enums.size(); i++) {
        JsonNode value = enums.path(i);
        if (!value.isNull()) {
            JsonNode javaEnumNode = javaEnums.path(i);
            if (javaEnumNode.isMissingNode()) {
                ruleFactory.getLogger().error("javaEnum entry for " + value.asText() + " was not found.");
            }
            String constantName = getConstantName(value.asText(), javaEnumNode.path("name").asText());
            constantName = makeUnique(constantName, existingConstantNames);
            existingConstantNames.add(constantName);
            JsonNode titleNode = javaEnumNode.path("title");
            JsonNode descriptionNode = javaEnumNode.path("description");
            enumValues.add(new EnumValueDefinition(constantName, value.asText(), javaEnumNode, titleNode, descriptionNode));
        }
    }
    return new EnumDefinition(nodeName, enumNode, type, enumValues, EnumDefinitionExtensionType.JAVA_ENUMS);
}
Also used : EnumValueDefinition(org.jsonschema2pojo.model.EnumValueDefinition) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) EnumDefinition(org.jsonschema2pojo.model.EnumDefinition)

Aggregations

EnumDefinition (org.jsonschema2pojo.model.EnumDefinition)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ArrayList (java.util.ArrayList)3 EnumValueDefinition (org.jsonschema2pojo.model.EnumValueDefinition)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 JClassAlreadyExistsException (com.sun.codemodel.JClassAlreadyExistsException)1 JDefinedClass (com.sun.codemodel.JDefinedClass)1 JFieldVar (com.sun.codemodel.JFieldVar)1 JType (com.sun.codemodel.JType)1 RuleLogger (org.jsonschema2pojo.RuleLogger)1 ClassAlreadyExistsException (org.jsonschema2pojo.exception.ClassAlreadyExistsException)1