protected XML resolveXml(Annotated a, Annotation[] annotations, io.swagger.v3.oas.annotations.media.Schema schema) { // if XmlRootElement annotation, construct an Xml object and attach it to the model XmlRootElement rootAnnotation = null; if (a != null) { rootAnnotation = a.getAnnotation(XmlRootElement.class); } if (rootAnnotation == null) { if (annotations != null) { for (Annotation ann: annotations) { if (ann instanceof XmlRootElement) { rootAnnotation = (XmlRootElement)ann; break; } } } } if (rootAnnotation != null && !"".equals(rootAnnotation.name()) && !"##default".equals(rootAnnotation.name())) { XML xml = new XML().name(rootAnnotation.name()); if (rootAnnotation.namespace() != null && !"".equals(rootAnnotation.namespace()) && !"##default".equals(rootAnnotation.namespace())) { xml.namespace(rootAnnotation.namespace()); } return xml; } return null; }
assertEquals(rootXml.getName(), "rootName"); final XML xml = property.getXml(); assertNotNull(xml); assertNull(xml.getName()); assertTrue(xml.getAttribute()); assertNull(xml.getWrapped()); } else if ("name".equals(name)) { final XML xml = property.getXml(); assertNotNull(xml); assertEquals(xml.getName(), "renamed"); assertNull(xml.getAttribute()); assertNull(xml.getWrapped()); } else if (Arrays.asList("list", "forcedElement").contains(name)) { assertNull(property.getXml()); final XML xml = property.getXml(); assertNotNull(xml); assertEquals(xml.getName(), "wrappedListItems"); assertNull(xml.getAttribute()); assertTrue(xml.getWrapped()); } else { fail(String.format("Unexpected property: %s", name));
/** * Puts definitions for XML attribute. * * @param attribute XmlAttribute * @param property property instance to be updated */ private static void applyAttribute(XmlAttribute attribute, Schema property) { if (attribute != null) { final XML xml = getXml(property); xml.setAttribute(true); setName(attribute.namespace(), attribute.name(), property); } }
/** * Puts definitions for XML wrapper. * * @param wrapper XmlElementWrapper * @param property property instance to be updated */ private static void applyElement(XmlElementWrapper wrapper, Schema property) { if (wrapper != null) { final XML xml = getXml(property); xml.setWrapped(true); // No need to set the xml name if the name provided by xmlelementwrapper annotation is ##default or equal to the property name | https://github.com/swagger-api/swagger-core/pull/2050 if (!"##default".equals(wrapper.name()) && !wrapper.name().isEmpty() && !wrapper.name().equals(property.getName())) { xml.setName(wrapper.name()); } } }
@Test(description = "it should process an XML model attribute") public void processXMLModelAttribute() { final Map<String, Schema> schemas = ModelConverters.getInstance().readAll(Monster.class); final Schema model = schemas.get("Monster"); assertNotNull(model); assertTrue(model instanceof Schema); XML xml = model.getXml(); assertNotNull(xml); assertEquals(xml.getName(), "monster"); final Schema property = (Schema) model.getProperties().get("children"); assertNotNull(property); xml = property.getXml(); assertTrue(xml.getWrapped()); assertNull(xml.getName()); }
return null; XML xml = new XML(); xml.setName(value); xml.setNamespace(value); xml.setPrefix(value); xml.setAttribute(attribute); xml.setWrapped(wrapped); xml.setExtensions(extensions);
if (propertySchema.getXml().getAttribute() != null) { codegenProperty.getVendorExtensions().put(CodegenConstants.IS_XML_ATTRIBUTE_EXT_NAME, propertySchema.getXml().getAttribute()); codegenProperty.xmlPrefix = propertySchema.getXml().getPrefix(); codegenProperty.xmlName = propertySchema.getXml().getName(); codegenProperty.xmlNamespace = propertySchema.getXml().getNamespace(); if (propertySchema.getXml() != null) { codegenProperty.getVendorExtensions().put(CodegenConstants.IS_XML_WRAPPED_EXT_NAME, propertySchema.getXml().getWrapped() == null ? false : propertySchema.getXml().getWrapped()); codegenProperty.xmlPrefix= propertySchema.getXml().getPrefix(); codegenProperty.xmlNamespace = propertySchema.getXml().getNamespace(); codegenProperty.xmlName = propertySchema.getXml().getName();
codegenModel.xmlPrefix = schema.getXml().getPrefix(); codegenModel.xmlNamespace = schema.getXml().getNamespace(); codegenModel.xmlName = schema.getXml().getName(); .getPrefix(); codegenModel.xmlNamespace = innerModel.getXml() .getNamespace(); codegenModel.xmlName = innerModel.getXml() .getName();
@Override public Optional<Schema> filterSchema(Schema schema, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) { if (schema == null || StringUtils.isBlank(schema.getXml().getName())) { return Optional.of(schema); } if (schema.getXml().getName().equals(MODEL)) { return Optional.empty(); } return Optional.of(schema); } }
XML xml = schema.getXml(); if (xml != null) { if (xml.getName() != null) { name = xml.getName(); for (String pName : properties.keySet()) { Schema property = properties.get(pName); if (property != null && property.getXml() != null && property.getXml().getAttribute() != null && property.getXml().getAttribute()) { attributes.put(pName, property); } else {
private static XML getXml(Schema property) { final XML existing = property.getXml(); if (existing != null) { return existing; } final XML created = new XML(); property.setXml(created); return created; }
private boolean paramHasXmlNamespace(CodegenParameter param, Map<String, Schema> definitions) { Object refName = param.vendorExtensions.get("refName"); if ((refName != null) && (refName instanceof String)) { String name = (String) refName; Schema model = definitions.get(ModelUtils.getSimpleRef(name)); if (model != null) { XML xml = model.getXml(); if ((xml != null) && (xml.getNamespace() != null)) { return true; } } } return false; }
Schema inner = as.getItems(); boolean wrapped = false; if (schema.getXml() != null && schema.getXml().getWrapped() != null && schema.getXml().getWrapped()) { wrapped = true;
return null; XML xml = new XML(); xml.setName(value); xml.setNamespace(value); xml.setPrefix(value); xml.setAttribute(attribute); xml.setWrapped(wrapped); xml.setExtensions(extensions);
if (propertySchema.getXml().getAttribute() != null) { codegenProperty.getVendorExtensions().put(CodegenConstants.IS_XML_ATTRIBUTE_EXT_NAME, propertySchema.getXml().getAttribute()); codegenProperty.xmlPrefix = propertySchema.getXml().getPrefix(); codegenProperty.xmlName = propertySchema.getXml().getName(); codegenProperty.xmlNamespace = propertySchema.getXml().getNamespace(); if (propertySchema.getXml() != null) { codegenProperty.getVendorExtensions().put(CodegenConstants.IS_XML_WRAPPED_EXT_NAME, propertySchema.getXml().getWrapped() == null ? false : propertySchema.getXml().getWrapped()); codegenProperty.xmlPrefix= propertySchema.getXml().getPrefix(); codegenProperty.xmlNamespace = propertySchema.getXml().getNamespace(); codegenProperty.xmlName = propertySchema.getXml().getName();
codegenModel.xmlPrefix = schema.getXml().getPrefix(); codegenModel.xmlNamespace = schema.getXml().getNamespace(); codegenModel.xmlName = schema.getXml().getName(); .getPrefix(); codegenModel.xmlNamespace = innerModel.getXml() .getNamespace(); codegenModel.xmlName = innerModel.getXml() .getName();
@Test public void testSimple() throws Exception { final ModelConverter mr = modelResolver(); ModelConverterContextImpl ctx = new ModelConverterContextImpl(mr); final Schema model = mr.resolve(new AnnotatedType(XmlDecoratedBean.class), ctx, null); final XML xml = model.getXml(); assertNotNull(xml); assertEquals(xml.getName(), "xmlDecoratedBean"); // Cast it to an array property final ArraySchema property = (ArraySchema) model.getProperties().get("elements"); assertNotNull(property); final XML propertyXml = property.getXml(); assertNotNull(propertyXml); assertNull(propertyXml.getName()); assertTrue(propertyXml.getWrapped()); // Get the xml for items for the array property final XML itemsXml = property.getItems().getXml(); assertNotNull(itemsXml); // Check the name of item name assertEquals(itemsXml.getName(), "element"); assertNotNull(model.getProperties().get("elementC")); }
@Test(description = "it should serialize a parameterized type per 606") public void serializeParameterizedType() { final Map<String, Schema> schemas = readAll(Employee.class); final Schema employee = (Schema) schemas.get("employee"); final Map<String, Schema> props = employee.getProperties(); final Iterator<String> et = props.keySet().iterator(); final Schema id = props.get(et.next()); assertTrue(id instanceof IntegerSchema); final Schema firstName = props.get(et.next()); assertTrue(firstName instanceof StringSchema); final Schema lastName = props.get(et.next()); assertTrue(lastName instanceof StringSchema); final Schema department = props.get(et.next()); assertNotNull(department.get$ref()); final Schema manager = props.get(et.next()); assertNotNull(manager.get$ref()); final Schema team = props.get(et.next()); assertTrue(team instanceof ArraySchema); final ArraySchema ap = (ArraySchema) team; assertTrue(ap.getUniqueItems()); assertNotNull(employee.getXml()); assertEquals(employee.getXml().getName(), "employee"); }