Repository: glennjones/hapi-swagger
Pull request #743 (2022-04-12 12:06:30)

FIX to support OpenApi 2.0 XML properties for schemas
PR for this issue: https://github.com/glennjones/hapi-swagger/issues/735


Patch:
diff --git a/lib/properties.js b/lib/properties.js
index 01613e5..739bc7f 100644
--- a/lib/properties.js
+++ b/lib/properties.js
@@ -260,6 +260,11 @@ internals.properties.prototype.parsePropertyMetadata = function (property, name,
       const meta = Hoek.clone(xMeta);
       delete meta.swaggerLabel;
       if (Object.keys(meta).length) {
+        if ('xml' in meta) {
+          property.xml = meta.xml;
+          delete meta.xml;
+        }
+
         property['x-meta'] = meta;
       }
     }
diff --git a/usageguide.md b/usageguide.md
index 878d8b1..f9c4ec8 100644
--- a/usageguide.md
+++ b/usageguide.md
@@ -15,6 +15,7 @@
 -   [Caching](#caching)
 -   [File upload](#file-upload)
 -   [Prevent JOI properties from being included in the Swagger schema](#prevent-joi-properties-from-being-included-in-the-swagger-schema)
+-   [Name JOI properties for XML formats](#name-joi-properties-for-xml-formats)
 -   [Headers and .unknown()](#headers-and-unknown)
 -   [Additional Hapi data using x-\*](#additional-hapi-data-using-x-)
 -   [JSON without UI](#json-without-ui)
@@ -475,6 +476,44 @@ You can prevent specific JOI properties from being included in the generated Swa
 
 `.meta({ swaggerHidden: true })`
 
+## Name JOI properties for XML formats
+
+You can rename JOI properties via meta for XML included in the generated Swagger schema by using:
+
+### Objects and properties
+
+#### Schema
+
+```js
+Joi.object().meta({ xml: { name: 'ObjectXML' } });
+```
+
+#### Result
+
+```xml
+<ObjectXML> </ObjectXML>
+```
+
+### Arrays
+
+#### Schema
+
+```js
+Joi.array()
+    .items(xmlObject)
+    .meta({ xml: { name: 'ArrayXML', wrapped: true } });
+```
+
+#### Result
+
+```xml
+<ArrayXML>
+   <ObjectXML> </ObjectXML>
+</ArrayXML>
+```
+
+See this link for more info: [OpenAPI 2.0 XML object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#xmlObject)
+
 ## Default values and examples
 
 You can add both default values and examples to your JOI objects which are displayed within the Swagger interface.


Test patch:
diff --git a/test/unit/property-test.js b/test/unit/property-test.js
index 9a48956..164ec0f 100644
--- a/test/unit/property-test.js
+++ b/test/unit/property-test.js
@@ -182,6 +182,30 @@ lab.experiment('property - ', () => {
 
     expect(nonNegativeWithDropdown).to.include({ enum: [0] });
     expect(nonNegativeWithoutDropdown).to.not.include({ enum: [0] });
+    
+    const xmlObject = Joi.object().meta({ xml: { name: 'ObjectXML' } });
+    expect(propertiesAlt.parseProperty('x', xmlObject, null, 'body', false, false)).to.equal({
+      type: 'object',
+      xml: { name: 'ObjectXML' }
+    });
+
+    const xmlArrayOfObjects = Joi.array()
+      .items(xmlObject)
+      .meta({
+        xml: {
+          name: 'ArrayXML',
+          wrapped: true
+        }
+      });
+    expect(propertiesAlt.parseProperty('x', xmlArrayOfObjects, null, 'body', false, false)).to.equal({
+      type: 'array',
+      xml: { name: 'ArrayXML', wrapped: true },
+      items: {
+        type: 'object',
+        xml: { name: 'ObjectXML' }
+      },
+      name: 'x'
+    });
   });
 
   lab.test('parse hidden', () => {