Search in sources :

Example 1 with JsonSchema

use of org.eclipse.persistence.internal.jaxb.json.schema.model.JsonSchema in project eclipselink by eclipse-ee4j.

the class JsonSchemaGenerator method generateSchema.

public JsonSchema generateSchema(Class<?> rootClass) {
    this.rootClass = rootClass;
    schema = new JsonSchema();
    schema.setTitle(rootClass.getName());
    if (rootClass.isEnum()) {
        Class<?> generatedWrapper = jaxbContext.getClassToGeneratedClasses().get(rootClass.getName());
        if (generatedWrapper != null) {
            rootClass = generatedWrapper;
        } else {
            schema.setType(JsonType.STRING);
            return schema;
        }
    }
    // Check for simple type
    JsonType rootType = getJsonTypeForJavaType(rootClass);
    if (rootType != JsonType.OBJECT) {
        if (rootType == JsonType.BINARYTYPE) {
            schema.setAnyOf(getXopIncludeProperties());
            return schema;
        } else {
            schema.setType(rootType);
            return schema;
        }
    }
    Map<String, Property> properties = null;
    // Check for root level list or array
    if (rootClass.isArray() || isCollection(rootClass)) {
        schema.setType(JsonType.ARRAY);
        schema.setItems(new Property());
        Class<?> itemType = Object.class;
        if (rootClass.isArray()) {
            itemType = rootClass.getComponentType();
        } else {
            Type pType = rootClass.getGenericSuperclass();
            if (pType instanceof ParameterizedType) {
                itemType = (Class<?>) ((ParameterizedType) pType).getActualTypeArguments()[0];
            }
        }
        rootType = getJsonTypeForJavaType(itemType);
        schema.getItems().setType(rootType);
        if (rootType != JsonType.OBJECT) {
            return schema;
        }
        rootClass = itemType;
        properties = schema.getItems().getProperties();
    } else {
        schema.setType(JsonType.OBJECT);
        properties = schema.getProperties();
    }
    this.project = this.xmlContext.getSession(rootClass).getProject();
    XMLDescriptor descriptor = (XMLDescriptor) project.getDescriptor(rootClass);
    Boolean includeRoot = Boolean.TRUE;
    if (contextProperties != null) {
        includeRoot = (Boolean) this.contextProperties.get(JAXBContextProperties.JSON_INCLUDE_ROOT);
        if (includeRoot == null) {
            includeRoot = Boolean.TRUE;
        }
    }
    if (Boolean.TRUE.equals(includeRoot)) {
        XMLField field = descriptor.getDefaultRootElementField();
        if (field != null) {
            rootProperty = new Property();
            rootProperty.setType(JsonType.OBJECT);
            rootProperty.setName(getNameForFragment(field.getXPathFragment()));
            properties.put(rootProperty.getName(), rootProperty);
            properties = rootProperty.getProperties();
        }
    }
    boolean allowsAdditionalProperties = hasAnyMappings(descriptor);
    if (descriptor.hasInheritance()) {
        // handle inheritence
        // schema.setAnyOf(new Property[descriptor.getInheritancePolicy().getAllChildDescriptors().size()]);
        List<ClassDescriptor> descriptors = this.getAllDescriptorsForInheritance(descriptor);
        Property[] props = new Property[descriptors.size()];
        for (int i = 0; i < props.length; i++) {
            XMLDescriptor nextDescriptor = (XMLDescriptor) descriptors.get(i);
            Property ref = new Property();
            ref.setRef(getReferenceForDescriptor(nextDescriptor, true));
            props[i] = ref;
        }
        if (rootProperty != null) {
            rootProperty.setAnyOf(props);
            rootProperty.setProperties(null);
            rootProperty.setType(null);
            rootProperty.setAdditionalProperties(null);
            rootProperty.setAdditionalProperties(null);
        } else {
            this.schema.setAnyOf(props);
            this.schema.setProperties(null);
            this.schema.setType(null);
            this.schema.setAdditionalProperties(null);
        }
    } else {
        JsonType type = populateProperties(properties, descriptor);
        if (type != null) {
            if (type == JsonType.BINARYTYPE) {
                if (rootProperty != null) {
                    rootProperty.setAnyOf(getXopIncludeProperties());
                    rootProperty.setProperties(null);
                    rootProperty.setAdditionalProperties(null);
                    rootProperty.setType(null);
                } else {
                    this.schema.setAnyOf(getXopIncludeProperties());
                    this.schema.setProperties(null);
                    this.schema.setType(null);
                    this.schema.setAdditionalProperties(null);
                }
            } else if (type == JsonType.ENUMTYPE) {
                if (rootProperty != null) {
                    rootProperty.setType(JsonType.STRING);
                    rootProperty.setProperties(null);
                    rootProperty.setEnumeration(getEnumeration(descriptor));
                } else {
                    this.schema.setType(JsonType.STRING);
                    this.schema.setProperties(null);
                    this.schema.setEnumeration(getEnumeration(descriptor));
                }
            } else {
                if (rootProperty != null) {
                    rootProperty.setType(type);
                } else {
                    schema.setType(type);
                }
            }
        } else {
            if (rootProperty != null) {
                rootProperty.setAdditionalProperties(allowsAdditionalProperties);
            } else {
                this.schema.setAdditionalProperties(allowsAdditionalProperties);
            }
        }
    }
    return schema;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) JsonType(org.eclipse.persistence.internal.jaxb.json.schema.model.JsonType) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) JsonSchema(org.eclipse.persistence.internal.jaxb.json.schema.model.JsonSchema) ParameterizedType(java.lang.reflect.ParameterizedType) JsonType(org.eclipse.persistence.internal.jaxb.json.schema.model.JsonType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) Property(org.eclipse.persistence.internal.jaxb.json.schema.model.Property)

Example 2 with JsonSchema

use of org.eclipse.persistence.internal.jaxb.json.schema.model.JsonSchema in project eclipselink by eclipse-ee4j.

the class JAXBContext method generateJsonSchema.

public void generateJsonSchema(SchemaOutputResolver outputResolver, Class<?> rootClass) {
    JsonSchemaGenerator generator = new JsonSchemaGenerator(this, this.contextState.properties);
    JsonSchema schema = generator.generateSchema(rootClass);
    try {
        Marshaller m = getJsonSchemaMarshaller();
        m.marshal(schema, outputResolver.createOutput(null, rootClass.getName() + ".json"));
    } catch (Exception ex) {
        throw org.eclipse.persistence.exceptions.JAXBException.exceptionDuringSchemaGeneration(ex);
    }
}
Also used : XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) Marshaller(jakarta.xml.bind.Marshaller) JsonSchema(org.eclipse.persistence.internal.jaxb.json.schema.model.JsonSchema) JsonSchemaGenerator(org.eclipse.persistence.internal.jaxb.json.schema.JsonSchemaGenerator) JAXBException(org.eclipse.persistence.exceptions.JAXBException) ConversionException(org.eclipse.persistence.exceptions.ConversionException) PropertyException(jakarta.xml.bind.PropertyException)

Aggregations

JsonSchema (org.eclipse.persistence.internal.jaxb.json.schema.model.JsonSchema)2 Marshaller (jakarta.xml.bind.Marshaller)1 PropertyException (jakarta.xml.bind.PropertyException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 ConversionException (org.eclipse.persistence.exceptions.ConversionException)1 JAXBException (org.eclipse.persistence.exceptions.JAXBException)1 JsonSchemaGenerator (org.eclipse.persistence.internal.jaxb.json.schema.JsonSchemaGenerator)1 JsonType (org.eclipse.persistence.internal.jaxb.json.schema.model.JsonType)1 Property (org.eclipse.persistence.internal.jaxb.json.schema.model.Property)1 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)1 XMLField (org.eclipse.persistence.oxm.XMLField)1 XMLMarshaller (org.eclipse.persistence.oxm.XMLMarshaller)1