private static void defineAttributeProperty(final String propertyName, final Property property, final Element parent) { final String type = property.getType(); final Element propertyElement = XmlSchemaHelper.addElement(parent, "attribute"); propertyElement.addAttribute("name", requireNonNull(propertyName, "missing property name")); propertyElement.addAttribute("type", XmlSchemaHelper.toXsdType(type)); }
private static void defineAttributePropertiesOf(final Element parent, final ModelImpl model) { final Map<String, Property> properties = model.getProperties(); for (final Entry<String, Property> propertyEntry : properties.entrySet()) { final String propertyName = propertyEntry.getKey(); final Property property = propertyEntry.getValue(); if (XmlSchemaHelper.isAttribute(property)) { defineAttributeProperty(propertyName, property, parent); } } }
private static void defineElementPropertiesOf(final Element parent, final Model model, final Swagger swagger, final Map<String, SchemaPrefixAndElement> moreSchemas) { final Map<String, Property> properties = model.getProperties(); for (final Entry<String, Property> propertyEntry : properties.entrySet()) { final String propertyName = propertyEntry.getKey(); final Property property = propertyEntry.getValue(); if (XmlSchemaHelper.isElement(property)) { defineElementProperty(propertyName, property, parent, swagger, moreSchemas); } } }
Element elementToDeclareIn; final String namespace = XmlSchemaHelper.xmlTargetNamespaceOrNull(model); final String name = XmlSchemaHelper.nameOf(model); return new SchemaPrefixAndElement("p" + moreSchemas.size(), XmlSchemaHelper.newXmlSchema(n)); }); elementToDeclareIn = XmlSchemaHelper.addElement(schemaPrefixAndElement.schema, "element"); elementToDeclareIn.addAttribute("name", name); ret = XmlSchemaHelper.addElement(parent, "element"); ret.addAttribute("ref", schemaPrefixAndElement.prefix + ":" + name); ret.addNamespace(schemaPrefixAndElement.prefix, namespace); if (parent == null) { ret = XmlSchemaHelper.newXmlSchema(namespace); elementToDeclareIn = XmlSchemaHelper.addElement(ret, "element"); elementToDeclareIn.addAttribute("name", name); } else { ret = XmlSchemaHelper.addElement(parent, "element"); ret.addAttribute("name", name); final Element complex = XmlSchemaHelper.addElement(elementToDeclareIn, "complexType"); final Element sequence = XmlSchemaHelper.addElement(complex, "sequence");
final Element schema = XmlSchemaHelper.newXmlSchema(SYNDESIS_PARAMETERS_NS); final Element parameters = XmlSchemaHelper.addElement(schema, "element"); parameters.addAttribute("name", "parameters"); final Element complex = XmlSchemaHelper.addElement(parameters, "complexType"); final Element sequence = XmlSchemaHelper.addElement(complex, "sequence"); final String type = XmlSchemaHelper.toXsdType(serializableParameter.getType()); final String name = trimToNull(serializableParameter.getName()); final Element element = XmlSchemaHelper.addElement(sequence, "element"); element.addAttribute("name", name); if (type != null) {
schemaSet.addNamespace(XmlSchemaHelper.XML_SCHEMA_PREFIX, XmlSchemaHelper.XML_SCHEMA_NS); final Element schema = XmlSchemaHelper.addElement(schemaSet, "schema"); schema.addAttribute("targetNamespace", SYNDESIS_REQUEST_NS); schema.addAttribute("elementFormDefault", "qualified"); final Element element = XmlSchemaHelper.addElement(schema, "element"); element.addAttribute("name", "request"); final Element sequence = XmlSchemaHelper.addElement(element, "complexType", "sequence"); final Element parameters = XmlSchemaHelper.addElement(sequence, "element"); parameters.addNamespace("p", SYNDESIS_PARAMETERS_NS); parameters.addAttribute("ref", "p:parameters"); final Element bodyElement = XmlSchemaHelper.addElement(sequence, "element"); bodyElement.addAttribute("name", "body"); final Element body = XmlSchemaHelper.addElement(bodyElement, "complexType", "sequence", "element"); final String bodyTargetNamespace = bodySchema.attributeValue("targetNamespace"); final String xmlSchemaSet = XmlSchemaHelper.serialize(document);
@Test public void shouldCreateArrayFromExamples() { final Map<String, ArrayProperty> namedPropertyMap = propertyFrom(jsonSchemaSnippet); final Entry<String, ArrayProperty> namedProperty = namedPropertyMap.entrySet().iterator().next(); final String propertyName = namedProperty.getKey(); final ArrayProperty array = namedProperty.getValue(); final Document document = DocumentHelper.createDocument(); final Element parent = document.addElement("xsd:sequence", XmlSchemaHelper.XML_SCHEMA_NS); assertThat(UnifiedXmlDataShapeGenerator.determineArrayItemName(propertyName, array)).isEqualTo(arrayItemName); assertThat(UnifiedXmlDataShapeGenerator.determineArrayElementName(propertyName, array)).isEqualTo(arrayElementName); UnifiedXmlDataShapeGenerator.defineArrayElement(array, propertyName, parent, NO_SWAGGER, NO_MORE_SCHEMAS); assertThat(XmlSchemaHelper.serialize(document)).isXmlEqualTo(schema(xmlSchemaSnippet)); }
@Test public void shouldConvertJsonSchemaToXsdTypes() { assertThat(XmlSchemaHelper.toXsdType("boolean")).isEqualTo(XmlSchemaHelper.XML_SCHEMA_PREFIX + ":boolean"); assertThat(XmlSchemaHelper.toXsdType("number")).isEqualTo(XmlSchemaHelper.XML_SCHEMA_PREFIX + ":decimal"); assertThat(XmlSchemaHelper.toXsdType("string")).isEqualTo(XmlSchemaHelper.XML_SCHEMA_PREFIX + ":string"); assertThat(XmlSchemaHelper.toXsdType("integer")).isEqualTo(XmlSchemaHelper.XML_SCHEMA_PREFIX + ":integer"); }
private static Element createResponseBodySchema(final Swagger swagger, final Operation operation, final Map<String, SchemaPrefixAndElement> moreSchemas) { final Optional<Response> maybeResponse = findResponse(operation); if (!maybeResponse.isPresent()) { return null; } final Response body = maybeResponse.get(); final Property bodySchema = body.getSchema(); if (bodySchema instanceof RefProperty) { return defineComplexElement((RefProperty) bodySchema, null, swagger, moreSchemas); } else if (bodySchema instanceof ArrayProperty) { final ArrayProperty array = (ArrayProperty) bodySchema; final String targetNamespace = XmlSchemaHelper.xmlTargetNamespaceOrNull(array); final Element schema = XmlSchemaHelper.newXmlSchema(targetNamespace); defineElementProperty(ofNullable(array.getName()).orElse("array"), array, schema, swagger, moreSchemas); return schema; } else { throw new IllegalArgumentException("Unsupported response schema type: " + bodySchema); } }
public static String nameOf(final Property property) { if (property instanceof RefProperty) { throw new IllegalArgumentException("Make sure that you dereference property, given: " + property); } return xmlNameOrDefault(property.getXml(), property.getName()); }
public static String nameOrDefault(final Property property, final String name) { final String determined = nameOf(property); if (determined == null) { return name; } return determined; }
private static void addEnumerationsTo(final Element element, final List<?> enums) { final Element simpleType = XmlSchemaHelper.addElement(element, "simpleType"); final Element restriction = XmlSchemaHelper.addElement(simpleType, "restriction"); restriction.addAttribute("base", element.attributeValue("type")); for (final Object enumValue : enums) { final Element enumeration = XmlSchemaHelper.addElement(restriction, "enumeration"); enumeration.addAttribute("value", String.valueOf(enumValue)); } }
} else { bodySchemaToUse = new ModelImpl(); final String name = XmlSchemaHelper.nameOrDefault(items, "array"); bodySchemaToUse.name(name); bodySchemaToUse.addProperty(name, items); final String targetNamespace = XmlSchemaHelper.xmlTargetNamespaceOrNull(bodySchemaToUse); final Element schema = XmlSchemaHelper.newXmlSchema(targetNamespace); final Element bodyElement = XmlSchemaHelper.addElement(schema, "element"); bodyElement.addAttribute("name", XmlSchemaHelper.nameOf(bodySchemaToUse)); final Element complexBody = XmlSchemaHelper.addElement(bodyElement, "complexType"); final Element bodySequence = XmlSchemaHelper.addElement(complexBody, "sequence");
@Override public DataShape createShapeFromResponse(final ObjectNode json, final Swagger swagger, final Operation operation) { final Optional<Response> maybeResponse = findResponse(operation); if (!maybeResponse.isPresent()) { return DATA_SHAPE_NONE; } final Document document = DocumentHelper.createDocument(); final Element schemaSet = document.addElement("d:SchemaSet", SCHEMA_SET_NS); schemaSet.addNamespace(XmlSchemaHelper.XML_SCHEMA_PREFIX, XmlSchemaHelper.XML_SCHEMA_NS); final Map<String, SchemaPrefixAndElement> moreSchemas = new HashMap<>(); final Element bodySchema = createResponseBodySchema(swagger, operation, moreSchemas); if (bodySchema == null) { return DATA_SHAPE_NONE; } schemaSet.add(bodySchema.detach()); if (!moreSchemas.isEmpty()) { final Element additionalSchemas = schemaSet.addElement("d:AdditionalSchemas"); moreSchemas.values().forEach(e -> additionalSchemas.add(e.schema.detach())); } final String xmlSchemaSet = XmlSchemaHelper.serialize(document); return new DataShape.Builder()// .name("Response")// .description("API response payload")// .kind(DataShapeKinds.XML_SCHEMA)// .specification(xmlSchemaSet)// .build(); }
public static String nameOf(final ModelImpl model) { return xmlNameOrDefault(model.getXml(), model.getName()); }
private static void defineElementProperty(final String propertyName, final Property property, final Element parent, final Swagger swagger, final Map<String, SchemaPrefixAndElement> moreSchemas) { final String type = property.getType(); switch (type) { case "ref": defineComplexElement((RefProperty) property, parent, swagger, moreSchemas); break; case "array": defineArrayElement(property, propertyName, parent, swagger, moreSchemas); break; default: final Element propertyElement = XmlSchemaHelper.addElement(parent, "element"); propertyElement.addAttribute("name", requireNonNull(propertyName, "missing property name")); propertyElement.addAttribute("type", XmlSchemaHelper.toXsdType(type)); break; } }
final String arrayElementName = determineArrayElementName(propertyName, array); final Element arrayElement = XmlSchemaHelper.addElement(parent, "element"); arrayElement.addAttribute("name", requireNonNull(arrayElementName, "missing array property name")); final Element arrayComplex = XmlSchemaHelper.addElement(arrayElement, "complexType"); sequence = XmlSchemaHelper.addElement(arrayComplex, "sequence"); } else { sequence = parent; itemsElement = defineComplexElement((RefProperty) items, sequence, swagger, moreSchemas); } else { itemsElement = XmlSchemaHelper.addElement(sequence, "element"); itemsElement.addAttribute("name", determineArrayItemName(propertyName, array)); itemsElement.addAttribute("type", XmlSchemaHelper.toXsdType(arrayItemsType));