@api-client/core 0.6.13 → 0.6.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/browser.d.ts +9 -2
- package/build/browser.js +12 -2
- package/build/browser.js.map +1 -1
- package/build/index.d.ts +9 -2
- package/build/index.js +12 -2
- package/build/index.js.map +1 -1
- package/build/src/amf/AmfMixin.d.ts +395 -0
- package/build/src/amf/AmfMixin.js +1146 -0
- package/build/src/amf/AmfMixin.js.map +1 -0
- package/build/src/amf/AmfSerializer.d.ts +136 -0
- package/build/src/amf/AmfSerializer.js +1913 -0
- package/build/src/amf/AmfSerializer.js.map +1 -0
- package/build/src/amf/AmfShapeGenerator.d.ts +85 -0
- package/build/src/amf/AmfShapeGenerator.js +385 -0
- package/build/src/amf/AmfShapeGenerator.js.map +1 -0
- package/build/src/amf/AmfTypes.d.ts +24 -0
- package/build/src/amf/AmfTypes.js +122 -0
- package/build/src/amf/AmfTypes.js.map +1 -0
- package/build/src/amf/ApiExampleGenerator.d.ts +36 -0
- package/build/src/amf/ApiExampleGenerator.js +109 -0
- package/build/src/amf/ApiExampleGenerator.js.map +1 -0
- package/build/src/amf/ApiMonacoSchemaGenerator.d.ts +67 -0
- package/build/src/amf/ApiMonacoSchemaGenerator.js +243 -0
- package/build/src/amf/ApiMonacoSchemaGenerator.js.map +1 -0
- package/build/src/amf/ApiSchemaGenerator.d.ts +55 -0
- package/build/src/amf/ApiSchemaGenerator.js +94 -0
- package/build/src/amf/ApiSchemaGenerator.js.map +1 -0
- package/build/src/amf/ApiSchemaValues.d.ts +98 -0
- package/build/src/amf/ApiSchemaValues.js +382 -0
- package/build/src/amf/ApiSchemaValues.js.map +1 -0
- package/build/src/amf/Utils.d.ts +41 -0
- package/build/src/amf/Utils.js +176 -0
- package/build/src/amf/Utils.js.map +1 -0
- package/build/src/amf/data-node/DataNodeBase.d.ts +31 -0
- package/build/src/amf/data-node/DataNodeBase.js +77 -0
- package/build/src/amf/data-node/DataNodeBase.js.map +1 -0
- package/build/src/amf/data-node/JsonDataNodeGenerator.d.ts +15 -0
- package/build/src/amf/data-node/JsonDataNodeGenerator.js +24 -0
- package/build/src/amf/data-node/JsonDataNodeGenerator.js.map +1 -0
- package/build/src/amf/data-node/UrlEncodedDataNodeGenerator.d.ts +13 -0
- package/build/src/amf/data-node/UrlEncodedDataNodeGenerator.js +42 -0
- package/build/src/amf/data-node/UrlEncodedDataNodeGenerator.js.map +1 -0
- package/build/src/amf/data-node/XmlDataNodeGenerator.d.ts +21 -0
- package/build/src/amf/data-node/XmlDataNodeGenerator.js +30 -0
- package/build/src/amf/data-node/XmlDataNodeGenerator.js.map +1 -0
- package/build/src/amf/definitions/Amf.d.ts +362 -0
- package/build/src/amf/definitions/Amf.js +2 -0
- package/build/src/amf/definitions/Amf.js.map +1 -0
- package/build/src/amf/definitions/Api.d.ts +381 -0
- package/build/src/amf/definitions/Api.js +2 -0
- package/build/src/amf/definitions/Api.js.map +1 -0
- package/build/src/amf/definitions/Base.d.ts +11 -0
- package/build/src/amf/definitions/Base.js +2 -0
- package/build/src/amf/definitions/Base.js.map +1 -0
- package/build/src/amf/definitions/Namespace.d.ts +311 -0
- package/build/src/amf/definitions/Namespace.js +330 -0
- package/build/src/amf/definitions/Namespace.js.map +1 -0
- package/build/src/amf/definitions/Shapes.d.ts +309 -0
- package/build/src/amf/definitions/Shapes.js +87 -0
- package/build/src/amf/definitions/Shapes.js.map +1 -0
- package/build/src/amf/models/AmfDataNode.d.ts +68 -0
- package/build/src/amf/models/AmfDataNode.js +188 -0
- package/build/src/amf/models/AmfDataNode.js.map +1 -0
- package/build/src/amf/shape/ShapeBase.d.ts +75 -0
- package/build/src/amf/shape/ShapeBase.js +90 -0
- package/build/src/amf/shape/ShapeBase.js.map +1 -0
- package/build/src/amf/shape/ShapeJsonSchemaGenerator.d.ts +46 -0
- package/build/src/amf/shape/ShapeJsonSchemaGenerator.js +406 -0
- package/build/src/amf/shape/ShapeJsonSchemaGenerator.js.map +1 -0
- package/build/src/amf/shape/ShapeXmlSchemaGenerator.d.ts +84 -0
- package/build/src/amf/shape/ShapeXmlSchemaGenerator.js +820 -0
- package/build/src/amf/shape/ShapeXmlSchemaGenerator.js.map +1 -0
- package/build/src/models/Thing.d.ts +17 -0
- package/build/src/models/Thing.js +19 -1
- package/build/src/models/Thing.js.map +1 -1
- package/build/src/models/data/Bindings.d.ts +161 -0
- package/build/src/models/data/Bindings.js +2 -0
- package/build/src/models/data/Bindings.js.map +1 -0
- package/build/src/models/data/DataAssociation.d.ts +135 -14
- package/build/src/models/data/DataAssociation.js +154 -21
- package/build/src/models/data/DataAssociation.js.map +1 -1
- package/build/src/models/data/DataAssociationSchema.d.ts +36 -0
- package/build/src/models/data/DataEntity.d.ts +76 -4
- package/build/src/models/data/DataEntity.js +136 -9
- package/build/src/models/data/DataEntity.js.map +1 -1
- package/build/src/models/data/DataFile.d.ts +3 -0
- package/build/src/models/data/DataFile.js +3 -0
- package/build/src/models/data/DataFile.js.map +1 -1
- package/build/src/models/data/DataModel.d.ts +1 -1
- package/build/src/models/data/DataModel.js.map +1 -1
- package/build/src/models/data/DataNamespace.d.ts +6 -0
- package/build/src/models/data/DataNamespace.js +11 -1
- package/build/src/models/data/DataNamespace.js.map +1 -1
- package/build/src/models/data/DataProperty.d.ts +131 -36
- package/build/src/models/data/DataProperty.js +200 -17
- package/build/src/models/data/DataProperty.js.map +1 -1
- package/data/apis/oas-date/oas-date.yaml +28 -0
- package/data/apis/oas-types/oas-types.yaml +159 -0
- package/data/apis/oas-unions/oas-unions.yaml +75 -0
- package/data/apis/raml-date/raml-date.raml +28 -0
- package/data/apis/recursive/recursive.raml +14 -0
- package/data/apis/schema-api/examples/person.json +14 -0
- package/data/apis/schema-api/examples/person.raml +14 -0
- package/data/apis/schema-api/examples/person.url.encoded +1 -0
- package/data/apis/schema-api/examples/person.xml +14 -0
- package/data/apis/schema-api/library/demo-types.raml +43 -0
- package/data/apis/schema-api/schema-api.raml +644 -0
- package/data/apis/schema-api/schemas/person.json +104 -0
- package/data/apis/schema-api/schemas/person.xsd +26 -0
- package/data/apis/schema-api/types/DemoPerson.raml +67 -0
- package/data/model.js +106 -0
- package/data/models/oas-date.json +637 -0
- package/data/models/oas-types.json +5352 -0
- package/data/models/oas-unions.json +1881 -0
- package/data/models/raml-date.json +1096 -0
- package/data/models/recursive.json +610 -0
- package/data/models/schema-api.json +37319 -0
- package/package.json +9 -6
- package/src/amf/AmfMixin.ts +1623 -0
- package/src/amf/AmfSerializer.ts +2028 -0
- package/src/amf/AmfShapeGenerator.ts +400 -0
- package/src/amf/AmfTypes.ts +126 -0
- package/src/amf/ApiExampleGenerator.ts +112 -0
- package/src/amf/ApiMonacoSchemaGenerator.ts +296 -0
- package/src/amf/ApiSchemaGenerator.ts +108 -0
- package/src/amf/ApiSchemaValues.ts +411 -0
- package/src/amf/Utils.ts +182 -0
- package/src/amf/data-node/DataNodeBase.ts +81 -0
- package/src/amf/data-node/JsonDataNodeGenerator.ts +26 -0
- package/src/amf/data-node/README.md +3 -0
- package/src/amf/data-node/UrlEncodedDataNodeGenerator.ts +43 -0
- package/src/amf/data-node/XmlDataNodeGenerator.ts +38 -0
- package/src/amf/definitions/Amf.ts +443 -0
- package/src/amf/definitions/Api.ts +427 -0
- package/src/amf/definitions/Base.ts +13 -0
- package/src/amf/definitions/Namespace.ts +341 -0
- package/src/amf/definitions/Shapes.ts +414 -0
- package/src/amf/models/AmfDataNode.ts +200 -0
- package/src/amf/shape/README.md +4 -0
- package/src/amf/shape/ShapeBase.ts +160 -0
- package/src/amf/shape/ShapeJsonSchemaGenerator.ts +422 -0
- package/src/amf/shape/ShapeXmlSchemaGenerator.ts +876 -0
- package/src/models/Thing.ts +25 -1
- package/src/models/data/Bindings.ts +186 -0
- package/src/models/data/DataAssociation.ts +226 -29
- package/src/models/data/DataAssociationSchema.ts +38 -0
- package/src/models/data/DataEntity.ts +170 -13
- package/src/models/data/DataFile.ts +3 -0
- package/src/models/data/DataModel.ts +1 -1
- package/src/models/data/DataNamespace.ts +16 -1
- package/src/models/data/DataProperty.ts +250 -47
- package/build/src/models/data/DataPropertySchema.d.ts +0 -125
- package/build/src/models/data/DataPropertySchema.js +0 -33
- package/build/src/models/data/DataPropertySchema.js.map +0 -1
- package/src/models/data/DataPropertySchema.ts +0 -156
|
@@ -0,0 +1,820 @@
|
|
|
1
|
+
import { Core as JsonCore } from '@api-client/json';
|
|
2
|
+
import { AmfNamespace as ns } from "../definitions/Namespace.js";
|
|
3
|
+
import { ShapeBase } from './ShapeBase.js';
|
|
4
|
+
import { XmlDataNodeGenerator } from '../data-node/XmlDataNodeGenerator.js';
|
|
5
|
+
import { collectNodeProperties, formatXmlValue, getUnionMember } from '../Utils.js';
|
|
6
|
+
/**
|
|
7
|
+
* Normalizes given name to a value that can be accepted by `createElement`
|
|
8
|
+
* function on a document object.
|
|
9
|
+
* @param name A name to process
|
|
10
|
+
* @returns The normalized name
|
|
11
|
+
*/
|
|
12
|
+
export const normalizeXmlTagName = (name) => name.replace(/[^a-zA-Z0-9-_.]/g, '');
|
|
13
|
+
const UNKNOWN_TYPE = 'unknown-type';
|
|
14
|
+
export function shapeToXmlTagName(shape) {
|
|
15
|
+
const { name, inherits = [], xmlSerialization } = shape;
|
|
16
|
+
let label = xmlSerialization && xmlSerialization.name ? xmlSerialization.name : name || UNKNOWN_TYPE;
|
|
17
|
+
if (label === 'schema' && inherits.length) {
|
|
18
|
+
const n = inherits.find(i => i.name && i.name !== 'schema');
|
|
19
|
+
if (n) {
|
|
20
|
+
label = n.name === 'type' ? n.displayName || n.name : n.name;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return normalizeXmlTagName(String(label));
|
|
24
|
+
}
|
|
25
|
+
export class ShapeXmlSchemaGenerator extends ShapeBase {
|
|
26
|
+
/**
|
|
27
|
+
* Generates a XML example from the structured value.
|
|
28
|
+
*
|
|
29
|
+
* @param schema The Shape definition
|
|
30
|
+
*/
|
|
31
|
+
generate(schema) {
|
|
32
|
+
const value = this.processNode(schema);
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Processes the Shape definition and returns a JavaScript object or array.
|
|
37
|
+
*/
|
|
38
|
+
processNode(schema, options = {}) {
|
|
39
|
+
const { types } = schema;
|
|
40
|
+
if (types.includes(ns.aml.vocabularies.shapes.ScalarShape)) {
|
|
41
|
+
return this._scalarShapeObject(schema, options);
|
|
42
|
+
}
|
|
43
|
+
if (types.includes(ns.w3.shacl.NodeShape)) {
|
|
44
|
+
return this._nodeShapeObject(schema, options);
|
|
45
|
+
}
|
|
46
|
+
if (types.includes(ns.aml.vocabularies.shapes.UnionShape)) {
|
|
47
|
+
return this._unionShapeObject(schema);
|
|
48
|
+
}
|
|
49
|
+
if (types.includes(ns.aml.vocabularies.shapes.FileShape)) {
|
|
50
|
+
return this._fileShapeObject();
|
|
51
|
+
}
|
|
52
|
+
if (types.includes(ns.aml.vocabularies.shapes.SchemaShape)) {
|
|
53
|
+
return this._schemaShapeObject();
|
|
54
|
+
}
|
|
55
|
+
if (types.includes(ns.aml.vocabularies.shapes.TupleShape)) {
|
|
56
|
+
return this._tupleShapeObject();
|
|
57
|
+
}
|
|
58
|
+
if (types.includes(ns.aml.vocabularies.shapes.ArrayShape) || types.includes(ns.aml.vocabularies.shapes.MatrixShape)) {
|
|
59
|
+
return this._arrayShapeObject(schema, options);
|
|
60
|
+
}
|
|
61
|
+
return this._anyShapeObject(schema);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Serializes generated JS value according to the mime type.
|
|
65
|
+
*/
|
|
66
|
+
serialize(value) {
|
|
67
|
+
return value;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Picks the union member to render.
|
|
71
|
+
*/
|
|
72
|
+
_readCurrentUnion(anyOf) {
|
|
73
|
+
const { selectedUnions } = this.opts;
|
|
74
|
+
return getUnionMember(anyOf, selectedUnions);
|
|
75
|
+
}
|
|
76
|
+
_collectProperties(schema) {
|
|
77
|
+
const { selectedUnions } = this.opts;
|
|
78
|
+
return collectNodeProperties(schema, selectedUnions);
|
|
79
|
+
}
|
|
80
|
+
_nodeShapeObject(schema, options = {}) {
|
|
81
|
+
const label = options.forceName || shapeToXmlTagName(schema);
|
|
82
|
+
const parts = [];
|
|
83
|
+
const currentIndent = (options.indent || 0);
|
|
84
|
+
const exampleValue = this._collectExamples(schema, {
|
|
85
|
+
tagFill: new Array(currentIndent * 2 + 0).fill(' ').join(''),
|
|
86
|
+
indent: currentIndent + 1,
|
|
87
|
+
nodeName: label,
|
|
88
|
+
});
|
|
89
|
+
if (exampleValue) {
|
|
90
|
+
return exampleValue;
|
|
91
|
+
}
|
|
92
|
+
const attributes = [];
|
|
93
|
+
const properties = this._collectProperties(schema);
|
|
94
|
+
properties.forEach((property) => {
|
|
95
|
+
const { range, minCount = 0 } = property;
|
|
96
|
+
if (minCount === 0 && !this.opts.renderOptional) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const anyRange = range;
|
|
100
|
+
if (anyRange.xmlSerialization) {
|
|
101
|
+
// Adds to the parent attributes list.
|
|
102
|
+
// When a non-scalar shape has `attribute` serialization this is an API spec error.
|
|
103
|
+
// Ignore such situation.
|
|
104
|
+
if (anyRange.xmlSerialization.attribute && anyRange.types.includes(ns.aml.vocabularies.shapes.ScalarShape)) {
|
|
105
|
+
let aLabel = normalizeXmlTagName(anyRange.xmlSerialization.name ? anyRange.xmlSerialization.name : property.name || anyRange.name || UNKNOWN_TYPE);
|
|
106
|
+
if (anyRange.xmlSerialization.prefix) {
|
|
107
|
+
aLabel = `${anyRange.xmlSerialization.prefix}:${aLabel}`;
|
|
108
|
+
}
|
|
109
|
+
const value = this._scalarValue(anyRange);
|
|
110
|
+
attributes.push(`${aLabel}="${value}"`);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const value = this._propertyShapeObject(property, { indent: currentIndent });
|
|
115
|
+
if (typeof value !== 'undefined') {
|
|
116
|
+
const fill = new Array(currentIndent * 2 + 2).fill(' ').join('');
|
|
117
|
+
parts.push(formatXmlValue(fill, value));
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
let opening = `<${label}`;
|
|
121
|
+
if (attributes.length) {
|
|
122
|
+
opening += ' ';
|
|
123
|
+
opening += attributes.join(' ');
|
|
124
|
+
}
|
|
125
|
+
parts.unshift(`${opening}>`);
|
|
126
|
+
const fill = new Array(currentIndent * 2).fill(' ').join('');
|
|
127
|
+
parts.push(`${fill}</${label}>`);
|
|
128
|
+
return parts.join('\n');
|
|
129
|
+
}
|
|
130
|
+
_scalarShapeObject(schema, options = {}) {
|
|
131
|
+
const { xmlSerialization, defaultValue } = schema;
|
|
132
|
+
let content;
|
|
133
|
+
if (defaultValue) {
|
|
134
|
+
const gen = new XmlDataNodeGenerator();
|
|
135
|
+
content = gen.processNode(defaultValue);
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
content = this._scalarValue(schema);
|
|
139
|
+
}
|
|
140
|
+
let label = options.forceName || shapeToXmlTagName(schema);
|
|
141
|
+
const attributes = [];
|
|
142
|
+
const parts = [];
|
|
143
|
+
if (xmlSerialization) {
|
|
144
|
+
const { namespace, prefix } = xmlSerialization;
|
|
145
|
+
if (namespace) {
|
|
146
|
+
const attrName = prefix ? `xmlns:${prefix}` : 'xmlns';
|
|
147
|
+
attributes.push(`${attrName}="${namespace}"`);
|
|
148
|
+
}
|
|
149
|
+
if (prefix) {
|
|
150
|
+
label = `${prefix}:${label}`;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
let opening = `<${label}`;
|
|
154
|
+
if (attributes.length) {
|
|
155
|
+
opening += ' ';
|
|
156
|
+
opening += attributes.join(' ');
|
|
157
|
+
}
|
|
158
|
+
opening += '>';
|
|
159
|
+
parts.push(opening);
|
|
160
|
+
parts.push(content);
|
|
161
|
+
parts.push(`</${label}>`);
|
|
162
|
+
return parts.join('');
|
|
163
|
+
}
|
|
164
|
+
_nilShapeObject(schema, options = {}) {
|
|
165
|
+
const { xmlSerialization } = schema;
|
|
166
|
+
const content = '';
|
|
167
|
+
let label = options.forceName || shapeToXmlTagName(schema);
|
|
168
|
+
const attributes = [];
|
|
169
|
+
const parts = [];
|
|
170
|
+
if (xmlSerialization) {
|
|
171
|
+
const { namespace, prefix } = xmlSerialization;
|
|
172
|
+
if (namespace) {
|
|
173
|
+
const attrName = prefix ? `xmlns:${prefix}` : 'xmlns';
|
|
174
|
+
attributes.push(`${attrName}="${namespace}"`);
|
|
175
|
+
}
|
|
176
|
+
if (prefix) {
|
|
177
|
+
label = `${prefix}:${label}`;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
let opening = `<${label}`;
|
|
181
|
+
if (attributes.length) {
|
|
182
|
+
opening += ' ';
|
|
183
|
+
opening += attributes.join(' ');
|
|
184
|
+
}
|
|
185
|
+
opening += '>';
|
|
186
|
+
parts.push(opening);
|
|
187
|
+
parts.push(content);
|
|
188
|
+
parts.push(`</${label}>`);
|
|
189
|
+
return parts.join('');
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* @returns The value for the property or undefined when cannot generate the value.
|
|
193
|
+
*/
|
|
194
|
+
_propertyShapeObject(schema, options = {}) {
|
|
195
|
+
const { range, minCount = 0 } = schema;
|
|
196
|
+
if (minCount === 0 && !this.opts.renderOptional || !range) {
|
|
197
|
+
return undefined;
|
|
198
|
+
}
|
|
199
|
+
const { types } = range;
|
|
200
|
+
const name = shapeToXmlTagName(schema);
|
|
201
|
+
if (types.includes(ns.aml.vocabularies.shapes.ScalarShape)) {
|
|
202
|
+
return this._scalarShapeObject(range, { ...options });
|
|
203
|
+
}
|
|
204
|
+
if (types.includes(ns.aml.vocabularies.shapes.NilShape)) {
|
|
205
|
+
return this._nilShapeObject(range, { ...options, forceName: name });
|
|
206
|
+
}
|
|
207
|
+
if (types.includes(ns.aml.vocabularies.shapes.RecursiveShape)) {
|
|
208
|
+
return undefined;
|
|
209
|
+
}
|
|
210
|
+
if (types.includes(ns.w3.shacl.NodeShape)) {
|
|
211
|
+
return this._nodePropertyObject(schema, options);
|
|
212
|
+
}
|
|
213
|
+
if (types.includes(ns.aml.vocabularies.shapes.ArrayShape) || types.includes(ns.aml.vocabularies.shapes.MatrixShape)) {
|
|
214
|
+
return this._nodePropertyArray(schema, options);
|
|
215
|
+
}
|
|
216
|
+
return this.processNode(range, { ...options, forceName: name });
|
|
217
|
+
}
|
|
218
|
+
_collectExamples(schema, opts = {}) {
|
|
219
|
+
if (!this.opts.renderExamples) {
|
|
220
|
+
return undefined;
|
|
221
|
+
}
|
|
222
|
+
const { isWrapped, nodeName, tagFill = '', indent = 0 } = opts;
|
|
223
|
+
let { examples = [] } = schema;
|
|
224
|
+
const { inherits } = schema;
|
|
225
|
+
if (Array.isArray(inherits) && inherits.length) {
|
|
226
|
+
inherits.forEach((parent) => {
|
|
227
|
+
let node = parent;
|
|
228
|
+
if (node.types.includes(ns.aml.vocabularies.shapes.UnionShape)) {
|
|
229
|
+
const union = node;
|
|
230
|
+
const { anyOf = [] } = union;
|
|
231
|
+
node = this._readCurrentUnion(anyOf);
|
|
232
|
+
}
|
|
233
|
+
const anyShape = node;
|
|
234
|
+
if (Array.isArray(anyShape.examples) && anyShape.examples.length) {
|
|
235
|
+
examples = examples.concat(anyShape.examples);
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
const validExamples = examples.filter(item => !!item.structuredValue);
|
|
240
|
+
if (!validExamples.length) {
|
|
241
|
+
return undefined;
|
|
242
|
+
}
|
|
243
|
+
const parts = [];
|
|
244
|
+
if (isWrapped) {
|
|
245
|
+
parts.push(`${tagFill}<${nodeName}>`);
|
|
246
|
+
}
|
|
247
|
+
const generator = new XmlDataNodeGenerator();
|
|
248
|
+
validExamples.forEach((item) => {
|
|
249
|
+
const value = generator.generate(item.structuredValue, nodeName, {
|
|
250
|
+
indent: indent + 1,
|
|
251
|
+
});
|
|
252
|
+
if (value !== undefined) {
|
|
253
|
+
parts.push(value);
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
if (isWrapped) {
|
|
257
|
+
parts.push(`${tagFill}</${nodeName}>`);
|
|
258
|
+
}
|
|
259
|
+
return parts.join('\n');
|
|
260
|
+
}
|
|
261
|
+
_createTabs(indent = 0, offset = 0) {
|
|
262
|
+
return new Array(indent * 2 + offset).fill(' ').join('');
|
|
263
|
+
}
|
|
264
|
+
_nodePropertyObject(schema, options = {}) {
|
|
265
|
+
const parts = [];
|
|
266
|
+
const name = normalizeXmlTagName(String(schema.name));
|
|
267
|
+
const { indent = 0 } = options;
|
|
268
|
+
const baseTabs = this._createTabs(indent);
|
|
269
|
+
let value;
|
|
270
|
+
const range = schema.range;
|
|
271
|
+
const exampleValue = this._collectExamples(range, {
|
|
272
|
+
tagFill: baseTabs,
|
|
273
|
+
indent: indent + 1,
|
|
274
|
+
nodeName: name,
|
|
275
|
+
});
|
|
276
|
+
const attributes = [];
|
|
277
|
+
if (exampleValue) {
|
|
278
|
+
value = exampleValue;
|
|
279
|
+
}
|
|
280
|
+
else {
|
|
281
|
+
const propertyParts = [];
|
|
282
|
+
const properties = this._collectProperties(range);
|
|
283
|
+
properties.forEach((property) => {
|
|
284
|
+
const { range, minCount = 0 } = property;
|
|
285
|
+
if (minCount === 0 && !this.opts.renderOptional) {
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
const anyRange = range;
|
|
289
|
+
if (anyRange.xmlSerialization) {
|
|
290
|
+
const { prefix, attribute } = anyRange.xmlSerialization;
|
|
291
|
+
if (attribute && anyRange.types.includes(ns.aml.vocabularies.shapes.ScalarShape)) {
|
|
292
|
+
let aLabel = normalizeXmlTagName(anyRange.xmlSerialization.name ? anyRange.xmlSerialization.name : property.name || anyRange.name || UNKNOWN_TYPE);
|
|
293
|
+
if (prefix) {
|
|
294
|
+
aLabel = `${prefix}:${aLabel}`;
|
|
295
|
+
}
|
|
296
|
+
const value = this._scalarValue(anyRange);
|
|
297
|
+
attributes.push(`${aLabel}="${value}"`);
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
const value = this._propertyShapeObject(property, { indent });
|
|
302
|
+
if (value !== undefined) {
|
|
303
|
+
const fill = this._createTabs(indent, 2);
|
|
304
|
+
propertyParts.push(formatXmlValue(fill, value));
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
value = propertyParts.join('\n');
|
|
308
|
+
}
|
|
309
|
+
let opening = `${baseTabs}<${name}`;
|
|
310
|
+
if (attributes.length) {
|
|
311
|
+
opening += ' ';
|
|
312
|
+
opening += attributes.join(' ');
|
|
313
|
+
}
|
|
314
|
+
parts.unshift(`${opening}>`);
|
|
315
|
+
parts.push(value);
|
|
316
|
+
parts.push(`${baseTabs}</${name}>`);
|
|
317
|
+
return parts.join('\n');
|
|
318
|
+
}
|
|
319
|
+
_nodePropertyArray(schema, options = {}) {
|
|
320
|
+
const range = schema.range;
|
|
321
|
+
const parts = [];
|
|
322
|
+
let name;
|
|
323
|
+
if (range.xmlSerialization && range.xmlSerialization.name) {
|
|
324
|
+
name = normalizeXmlTagName(range.xmlSerialization.name);
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
name = normalizeXmlTagName(String(schema.name));
|
|
328
|
+
}
|
|
329
|
+
const { indent = 0 } = options;
|
|
330
|
+
const isScalarItems = !!range.items && range.items.types.includes(ns.aml.vocabularies.shapes.ScalarShape);
|
|
331
|
+
const baseTabs = this._createTabs(indent);
|
|
332
|
+
if (!isScalarItems) {
|
|
333
|
+
parts.push(`${baseTabs}<${name}>`);
|
|
334
|
+
}
|
|
335
|
+
const result = this._arrayShapeObject(range, { ...options, indent });
|
|
336
|
+
parts.push(result);
|
|
337
|
+
if (!isScalarItems) {
|
|
338
|
+
parts.push(`${baseTabs}</${name}>`);
|
|
339
|
+
}
|
|
340
|
+
return parts.join('\n');
|
|
341
|
+
}
|
|
342
|
+
_arrayShapeObject(schema, options = {}) {
|
|
343
|
+
const { items } = schema;
|
|
344
|
+
const isScalarItems = !!items && items.types.includes(ns.aml.vocabularies.shapes.ScalarShape);
|
|
345
|
+
// the name is either from the XML serialization info, or the parent property.
|
|
346
|
+
// IT IS NOT THE `item`'s name unless its a scalar.
|
|
347
|
+
let nodeName;
|
|
348
|
+
// if (isScalarItems && schema.name) {
|
|
349
|
+
// nodeName = normalizeXmlTagName(schema.name);
|
|
350
|
+
// } else if (!isScalarItems && items?.name) {
|
|
351
|
+
// nodeName = normalizeXmlTagName(items.name);
|
|
352
|
+
// }
|
|
353
|
+
if (!isScalarItems && items?.name) {
|
|
354
|
+
nodeName = normalizeXmlTagName(items.name);
|
|
355
|
+
}
|
|
356
|
+
else if (schema.name) {
|
|
357
|
+
nodeName = normalizeXmlTagName(schema.name);
|
|
358
|
+
}
|
|
359
|
+
// wrapping can only be defined on the array shape and it "wraps" the generated
|
|
360
|
+
// content into an element that has the same name as the items
|
|
361
|
+
const isWrapped = !!schema.xmlSerialization && !!schema.xmlSerialization.wrapped;
|
|
362
|
+
const currentIndent = (options.indent || 0);
|
|
363
|
+
const tagFill = new Array((currentIndent) * 2 + 0).fill(' ').join('');
|
|
364
|
+
const valueFill = new Array((currentIndent) * 2 + (isWrapped ? 2 : 0)).fill(' ').join('');
|
|
365
|
+
// let's start with examples
|
|
366
|
+
if (this.opts.renderExamples) {
|
|
367
|
+
let { examples = [] } = schema;
|
|
368
|
+
const validExamples = examples.filter(item => !!item.structuredValue);
|
|
369
|
+
if (validExamples.length) {
|
|
370
|
+
const parts = [];
|
|
371
|
+
const generator = new XmlDataNodeGenerator();
|
|
372
|
+
// scalar items are always wrapped with it's own range
|
|
373
|
+
if (isScalarItems && !isWrapped) {
|
|
374
|
+
const [example] = validExamples;
|
|
375
|
+
if (!nodeName && items?.name) {
|
|
376
|
+
nodeName = normalizeXmlTagName(items.name);
|
|
377
|
+
}
|
|
378
|
+
const value = generator.generate(example.structuredValue, nodeName, {
|
|
379
|
+
indent: currentIndent,
|
|
380
|
+
});
|
|
381
|
+
if (value !== undefined) {
|
|
382
|
+
parts.push(value);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
else if (isScalarItems && isWrapped) {
|
|
386
|
+
const [example] = validExamples;
|
|
387
|
+
const value = generator.generate(example.structuredValue, nodeName, {
|
|
388
|
+
indent: currentIndent,
|
|
389
|
+
});
|
|
390
|
+
parts.push(`${tagFill}<${nodeName}>`);
|
|
391
|
+
if (value !== undefined) {
|
|
392
|
+
parts.push(value);
|
|
393
|
+
}
|
|
394
|
+
parts.push(`${tagFill}</${nodeName}>`);
|
|
395
|
+
}
|
|
396
|
+
else if (isWrapped) {
|
|
397
|
+
// when wrapped we wrap each example into the "name", else we render all examples under the "name".
|
|
398
|
+
validExamples.forEach((item) => {
|
|
399
|
+
parts.push(`${tagFill}<${nodeName}>`);
|
|
400
|
+
const value = generator.generate(item.structuredValue, /* nodeName */ undefined, {
|
|
401
|
+
indent: currentIndent + 2,
|
|
402
|
+
});
|
|
403
|
+
if (value !== undefined) {
|
|
404
|
+
parts.push(value);
|
|
405
|
+
}
|
|
406
|
+
parts.push(`${tagFill}</${nodeName}>`);
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
else {
|
|
410
|
+
validExamples.forEach((item) => {
|
|
411
|
+
const value = generator.generate(item.structuredValue, /* nodeName */ undefined, {
|
|
412
|
+
indent: currentIndent,
|
|
413
|
+
});
|
|
414
|
+
if (value !== undefined) {
|
|
415
|
+
parts.push(value);
|
|
416
|
+
}
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
return parts.join('\n');
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
if (isScalarItems && schema.defaultValue) {
|
|
423
|
+
const gen = new XmlDataNodeGenerator();
|
|
424
|
+
const value = gen.generate(schema.defaultValue);
|
|
425
|
+
if (!nodeName && items?.name) {
|
|
426
|
+
nodeName = normalizeXmlTagName(items.name);
|
|
427
|
+
}
|
|
428
|
+
return `<${nodeName}>${value?.trim()}</${nodeName}>`;
|
|
429
|
+
}
|
|
430
|
+
if (items) {
|
|
431
|
+
const parts = [];
|
|
432
|
+
if (isWrapped && isScalarItems) {
|
|
433
|
+
parts.push(`${tagFill}<${nodeName}>`);
|
|
434
|
+
}
|
|
435
|
+
const init = {
|
|
436
|
+
indent: currentIndent + 1,
|
|
437
|
+
};
|
|
438
|
+
if (nodeName) {
|
|
439
|
+
init.forceName = nodeName;
|
|
440
|
+
}
|
|
441
|
+
const value = this.processNode(items, init);
|
|
442
|
+
if (value !== undefined) {
|
|
443
|
+
parts.push(`${valueFill}${value}`);
|
|
444
|
+
}
|
|
445
|
+
if (isWrapped && isScalarItems) {
|
|
446
|
+
parts.push(`${tagFill}</${nodeName}>`);
|
|
447
|
+
}
|
|
448
|
+
return parts.join('\n');
|
|
449
|
+
}
|
|
450
|
+
return '';
|
|
451
|
+
// const isScalarItems = !!items && items.types.includes(ns.aml.vocabularies.shapes.ScalarShape);
|
|
452
|
+
// if (items) {
|
|
453
|
+
// if (items.types.includes(ns.aml.vocabularies.shapes.ScalarShape)) {
|
|
454
|
+
// return this._scalarItems(schema as IArrayShape<IScalarShape>, options);
|
|
455
|
+
// }
|
|
456
|
+
// if (items.types.includes(ns.w3.shacl.NodeShape)) {
|
|
457
|
+
// return this._nodeItems(schema as IArrayShape<INodeShape>, options);
|
|
458
|
+
// }
|
|
459
|
+
// }
|
|
460
|
+
// let label = shapeToXmlTagName(schema);
|
|
461
|
+
// if (label === UNKNOWN_TYPE && isScalarItems) {
|
|
462
|
+
// // label = shapeToXmlTagName(items as IScalarShape);
|
|
463
|
+
// }
|
|
464
|
+
// const currentIndent = (options.indent || 0);
|
|
465
|
+
// const rootFill = new Array(currentIndent*2).fill(' ').join('');
|
|
466
|
+
// const parts = [
|
|
467
|
+
// `${rootFill}<${label}>`
|
|
468
|
+
// ];
|
|
469
|
+
// let nodeName = label;
|
|
470
|
+
// const anyItems = items as IAnyShape;
|
|
471
|
+
// if (anyItems.xmlSerialization && anyItems.xmlSerialization.name) {
|
|
472
|
+
// nodeName = normalizeXmlTagName(anyItems.xmlSerialization.name);
|
|
473
|
+
// }
|
|
474
|
+
// // Note about wrapping.
|
|
475
|
+
// // XML array values are not wrapped by default. This means that by default
|
|
476
|
+
// // it produces a value like this:
|
|
477
|
+
// // <ParentArray>
|
|
478
|
+
// // <arrayMemberProperty></arrayMemberProperty>
|
|
479
|
+
// // </ParentArray>
|
|
480
|
+
// //
|
|
481
|
+
// // When the object is marked as wrapped then the object is rendered as follows
|
|
482
|
+
// //
|
|
483
|
+
// // <ParentArray>
|
|
484
|
+
// // <MemberObject>
|
|
485
|
+
// // <arrayMemberProperty></arrayMemberProperty>
|
|
486
|
+
// // <MemberObject>
|
|
487
|
+
// // </ParentArray>
|
|
488
|
+
// const isWrapped = xmlSerialization && !!xmlSerialization.wrapped;
|
|
489
|
+
// const defaultValue = schema.defaultValue || items && items.defaultValue;
|
|
490
|
+
// let itemName;
|
|
491
|
+
// if (isWrapped) {
|
|
492
|
+
// try {
|
|
493
|
+
// itemName = shapeToXmlTagName(items as IAnyShape);
|
|
494
|
+
// } catch (e) {
|
|
495
|
+
// itemName = 'UNKNOWN-NAME'
|
|
496
|
+
// }
|
|
497
|
+
// }
|
|
498
|
+
// let { examples=[] } = schema;
|
|
499
|
+
// if (Array.isArray(anyItems.examples)) {
|
|
500
|
+
// examples = examples.concat(anyItems.examples);
|
|
501
|
+
// }
|
|
502
|
+
// if (this.opts.renderExamples && examples && examples.length) {
|
|
503
|
+
// const example = examples.find((item) => !!item.structuredValue);
|
|
504
|
+
// const value = this._exampleToObject(example);
|
|
505
|
+
// if (typeof value !== 'undefined') {
|
|
506
|
+
// const tagFill = new Array(currentIndent * 2 + 2).fill(' ').join('');
|
|
507
|
+
// const valueFill = new Array(currentIndent * 2 + 4).fill(' ').join('');
|
|
508
|
+
// parts.push(`${tagFill}<${nodeName}>`);
|
|
509
|
+
// parts.push(`${valueFill}${value}`);
|
|
510
|
+
// parts.push(`${tagFill}</${nodeName}>`);
|
|
511
|
+
// }
|
|
512
|
+
// } else if (defaultValue) {
|
|
513
|
+
// const gen = new XmlDataNodeGenerator();
|
|
514
|
+
// const value = gen.generate(defaultValue);
|
|
515
|
+
// if (value) {
|
|
516
|
+
// const tagFill = new Array(currentIndent * 2 + 2).fill(' ').join('');
|
|
517
|
+
// const valueFill = new Array(currentIndent * 2 + 4).fill(' ').join('');
|
|
518
|
+
// parts.push(`${tagFill}<${nodeName}>`);
|
|
519
|
+
// parts.push(`${valueFill}${value.trim()}`);
|
|
520
|
+
// parts.push(`${tagFill}</${nodeName}>`);
|
|
521
|
+
// }
|
|
522
|
+
// } else if (items && items.types.includes(ns.w3.shacl.NodeShape)) {
|
|
523
|
+
// const typed = items as INodeShape;
|
|
524
|
+
// const tagFill = new Array(currentIndent * 2 + 2).fill(' ').join('');
|
|
525
|
+
// const valueFill = isWrapped ? new Array(currentIndent * 2 + 4).fill(' ').join('') : tagFill;
|
|
526
|
+
// if (isWrapped) {
|
|
527
|
+
// parts.push(`${tagFill}<${itemName}>`);
|
|
528
|
+
// }
|
|
529
|
+
// const properties = this._collectProperties(typed);
|
|
530
|
+
// properties.forEach((prop) => {
|
|
531
|
+
// const value = this._propertyShapeObject(prop);
|
|
532
|
+
// if (value) {
|
|
533
|
+
// parts.push(`${valueFill}${value}`);
|
|
534
|
+
// }
|
|
535
|
+
// });
|
|
536
|
+
// if (isWrapped) {
|
|
537
|
+
// parts.push(`${tagFill}</${itemName}>`);
|
|
538
|
+
// }
|
|
539
|
+
// } else if (items) {
|
|
540
|
+
// let name = shapeToXmlTagName(items as IAnyShape);
|
|
541
|
+
// if (name === UNKNOWN_TYPE) {
|
|
542
|
+
// name = label;
|
|
543
|
+
// }
|
|
544
|
+
// const opts = {
|
|
545
|
+
// forceName: name,
|
|
546
|
+
// indent: currentIndent + 1,
|
|
547
|
+
// };
|
|
548
|
+
// const value = items && this.processNode(items, opts);
|
|
549
|
+
// if (typeof value !== 'undefined') {
|
|
550
|
+
// const fill = new Array(currentIndent * 2 + 2).fill(' ').join('');
|
|
551
|
+
// parts.push(`${fill}${value}`);
|
|
552
|
+
// }
|
|
553
|
+
// }
|
|
554
|
+
// parts.push(`${rootFill}</${label}>`);
|
|
555
|
+
// return parts.join('\n');
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* Creates an example from an array shape when the items is the scalar shape.
|
|
559
|
+
* Note, it assumes the previous step tested whether the `items` is scalar.
|
|
560
|
+
*
|
|
561
|
+
* @param schema The array schema
|
|
562
|
+
* @param options Processing options.
|
|
563
|
+
*/
|
|
564
|
+
_scalarItems(schema, options = {}) {
|
|
565
|
+
const items = schema.items;
|
|
566
|
+
const currentIndent = (options.indent || 0);
|
|
567
|
+
const tagFill = new Array(currentIndent * 2).fill(' ').join('');
|
|
568
|
+
let nodeName = '';
|
|
569
|
+
if (items.xmlSerialization && items.xmlSerialization.name) {
|
|
570
|
+
nodeName = normalizeXmlTagName(items.xmlSerialization.name);
|
|
571
|
+
}
|
|
572
|
+
else {
|
|
573
|
+
nodeName = shapeToXmlTagName(schema.name ? schema : items);
|
|
574
|
+
}
|
|
575
|
+
// let's start with examples
|
|
576
|
+
if (this.opts.renderExamples) {
|
|
577
|
+
let { examples = [] } = schema;
|
|
578
|
+
if (Array.isArray(items.examples)) {
|
|
579
|
+
examples = examples.concat(items.examples);
|
|
580
|
+
}
|
|
581
|
+
const validExamples = examples.filter(item => !!item.structuredValue);
|
|
582
|
+
if (validExamples.length) {
|
|
583
|
+
const parts = [];
|
|
584
|
+
const generator = new XmlDataNodeGenerator();
|
|
585
|
+
validExamples.forEach((item) => {
|
|
586
|
+
const value = generator.generate(item.structuredValue, nodeName);
|
|
587
|
+
if (value !== undefined) {
|
|
588
|
+
parts.push(`${tagFill}${value}`);
|
|
589
|
+
}
|
|
590
|
+
});
|
|
591
|
+
return parts.join('\n');
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
// then the default value
|
|
595
|
+
if (schema.defaultValue) {
|
|
596
|
+
const generator = new XmlDataNodeGenerator();
|
|
597
|
+
const value = generator.generate(schema.defaultValue, nodeName);
|
|
598
|
+
if (value !== undefined) {
|
|
599
|
+
return `${tagFill}${value}`;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
// finally we generate stuff.
|
|
603
|
+
const opts = {
|
|
604
|
+
indent: currentIndent + 1,
|
|
605
|
+
forceName: nodeName,
|
|
606
|
+
};
|
|
607
|
+
const value = this.processNode(items, opts);
|
|
608
|
+
return `${tagFill}${value}`;
|
|
609
|
+
}
|
|
610
|
+
/**
|
|
611
|
+
* Renders a NodeShape as an array item.
|
|
612
|
+
*
|
|
613
|
+
* @param schema The array shape with item that is NodeShape.
|
|
614
|
+
* @param options Rendering options.
|
|
615
|
+
*/
|
|
616
|
+
_nodeItems(schema, options = {}) {
|
|
617
|
+
const items = schema.items;
|
|
618
|
+
const currentIndent = (options.indent || 0);
|
|
619
|
+
const tagFill = new Array(currentIndent * 2).fill(' ').join('');
|
|
620
|
+
const isWrapped = schema.xmlSerialization && !!schema.xmlSerialization.wrapped;
|
|
621
|
+
const defaultValue = schema.defaultValue || items.defaultValue;
|
|
622
|
+
let nodeName = '';
|
|
623
|
+
if (items.xmlSerialization && items.xmlSerialization.name) {
|
|
624
|
+
nodeName = normalizeXmlTagName(items.xmlSerialization.name);
|
|
625
|
+
}
|
|
626
|
+
else {
|
|
627
|
+
nodeName = shapeToXmlTagName(schema.name ? schema : items);
|
|
628
|
+
}
|
|
629
|
+
const valueFill = isWrapped ? new Array(currentIndent * 2 + 2).fill(' ').join('') : tagFill;
|
|
630
|
+
const parts = [];
|
|
631
|
+
if (isWrapped) {
|
|
632
|
+
parts.push(`${tagFill}<${nodeName}>`);
|
|
633
|
+
}
|
|
634
|
+
let rendered = false;
|
|
635
|
+
// let's start with examples
|
|
636
|
+
if (this.opts.renderExamples) {
|
|
637
|
+
let { examples = [] } = schema;
|
|
638
|
+
if (Array.isArray(items.examples)) {
|
|
639
|
+
examples = examples.concat(items.examples);
|
|
640
|
+
}
|
|
641
|
+
const validExamples = examples.filter(item => !!item.structuredValue);
|
|
642
|
+
if (validExamples.length) {
|
|
643
|
+
const parts = [];
|
|
644
|
+
const generator = new XmlDataNodeGenerator();
|
|
645
|
+
validExamples.forEach((item) => {
|
|
646
|
+
const value = generator.generate(item.structuredValue, nodeName);
|
|
647
|
+
if (value !== undefined) {
|
|
648
|
+
parts.push(`${tagFill}${value}`);
|
|
649
|
+
}
|
|
650
|
+
});
|
|
651
|
+
rendered = true;
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
if (!rendered && defaultValue) {
|
|
655
|
+
const generator = new XmlDataNodeGenerator();
|
|
656
|
+
const value = generator.generate(defaultValue, nodeName);
|
|
657
|
+
if (value !== undefined) {
|
|
658
|
+
parts.push(`${tagFill}${value}`);
|
|
659
|
+
rendered = true;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
if (!rendered) {
|
|
663
|
+
const properties = this._collectProperties(items);
|
|
664
|
+
properties.forEach((prop) => {
|
|
665
|
+
const value = this._propertyShapeObject(prop);
|
|
666
|
+
if (value) {
|
|
667
|
+
parts.push(`${valueFill}${value}`);
|
|
668
|
+
}
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
if (isWrapped) {
|
|
672
|
+
parts.push(`${tagFill}</${nodeName}>`);
|
|
673
|
+
}
|
|
674
|
+
return parts.join('\n');
|
|
675
|
+
}
|
|
676
|
+
/**
|
|
677
|
+
* @param example The example to turn into a JS object
|
|
678
|
+
*/
|
|
679
|
+
_exampleToObject(example) {
|
|
680
|
+
if (example && example.structuredValue) {
|
|
681
|
+
const generator = new XmlDataNodeGenerator();
|
|
682
|
+
return generator.generate(example.structuredValue);
|
|
683
|
+
}
|
|
684
|
+
return undefined;
|
|
685
|
+
}
|
|
686
|
+
_unionShapeObject(schema, options = {}) {
|
|
687
|
+
let { anyOf = [], examples = [] } = schema;
|
|
688
|
+
if (Array.isArray(schema.inherits) && schema.inherits) {
|
|
689
|
+
schema.inherits.forEach((parent) => {
|
|
690
|
+
const anyParent = parent;
|
|
691
|
+
if (Array.isArray(anyParent.examples) && anyParent.examples.length) {
|
|
692
|
+
examples = examples.concat(anyParent.examples);
|
|
693
|
+
}
|
|
694
|
+
const typed = parent;
|
|
695
|
+
if (Array.isArray(typed.anyOf) && typed.anyOf.length) {
|
|
696
|
+
anyOf = anyOf.concat(typed.anyOf);
|
|
697
|
+
}
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
if (Array.isArray(anyOf) && anyOf.length) {
|
|
701
|
+
if (this._isNotRequiredUnion(anyOf)) {
|
|
702
|
+
// This generates schema for required values.
|
|
703
|
+
// This implicitly mean that the property is not required therefore the value should
|
|
704
|
+
// not be generated.
|
|
705
|
+
return undefined;
|
|
706
|
+
}
|
|
707
|
+
const example = examples.find((item) => !!item.structuredValue);
|
|
708
|
+
const value = this._exampleToObject(example);
|
|
709
|
+
if (value !== undefined) {
|
|
710
|
+
const label = shapeToXmlTagName(schema);
|
|
711
|
+
const currentIndent = (options.indent || 0);
|
|
712
|
+
const rootFill = new Array(currentIndent * 2).fill(' ').join('');
|
|
713
|
+
const valueFill = new Array(currentIndent * 2 + 2).fill(' ').join('');
|
|
714
|
+
const parts = [];
|
|
715
|
+
parts.push(`${rootFill}<${label}>`);
|
|
716
|
+
const formatted = String(value).split('\n').filter(i => !!i).map(i => `${valueFill}${i}`).join('\n');
|
|
717
|
+
parts.push(`${formatted}`);
|
|
718
|
+
parts.push(`${rootFill}</${label}>`);
|
|
719
|
+
return parts.join('\n');
|
|
720
|
+
}
|
|
721
|
+
if (schema.defaultValue) {
|
|
722
|
+
return this._unionDefaultValue(schema, schema.defaultValue);
|
|
723
|
+
}
|
|
724
|
+
const member = this._readCurrentUnion(anyOf);
|
|
725
|
+
if (member) {
|
|
726
|
+
return this.processNode(member, { ...options, forceName: schema.name });
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
return undefined;
|
|
730
|
+
}
|
|
731
|
+
/**
|
|
732
|
+
* @param schema The schema with unions
|
|
733
|
+
* @param defaultValue The definition of a default value.
|
|
734
|
+
*/
|
|
735
|
+
_unionDefaultValue(schema, defaultValue, options = {}) {
|
|
736
|
+
const gen = new XmlDataNodeGenerator();
|
|
737
|
+
const value = gen.generate(defaultValue);
|
|
738
|
+
const anySchema = schema;
|
|
739
|
+
const label = shapeToXmlTagName(anySchema);
|
|
740
|
+
const currentIndent = (options.indent || 0);
|
|
741
|
+
const rootFill = new Array(currentIndent * 2).fill(' ').join('');
|
|
742
|
+
const parts = [
|
|
743
|
+
`${rootFill}<${label}>`
|
|
744
|
+
];
|
|
745
|
+
const valueFill = new Array(currentIndent * 2 + 2).fill(' ').join('');
|
|
746
|
+
parts.push(`${valueFill}${String(value).trim()}`);
|
|
747
|
+
parts.push(`${rootFill}</${label}>`);
|
|
748
|
+
return parts.join('\n');
|
|
749
|
+
}
|
|
750
|
+
_fileShapeObject() {
|
|
751
|
+
return undefined;
|
|
752
|
+
}
|
|
753
|
+
_schemaShapeObject() {
|
|
754
|
+
return undefined;
|
|
755
|
+
}
|
|
756
|
+
_tupleShapeObject() {
|
|
757
|
+
return undefined;
|
|
758
|
+
}
|
|
759
|
+
_anyShapeObject(schema) {
|
|
760
|
+
const { and = [], xone = [], or = [] } = schema;
|
|
761
|
+
if (and.length) {
|
|
762
|
+
// we combine all properties together under `schema` with changed properties
|
|
763
|
+
const copy = JsonCore.clone(schema);
|
|
764
|
+
copy.and = [];
|
|
765
|
+
copy.properties = [];
|
|
766
|
+
and.forEach((item) => {
|
|
767
|
+
const { types } = item;
|
|
768
|
+
if (types.includes(ns.aml.vocabularies.shapes.ScalarShape)) {
|
|
769
|
+
const typed = item;
|
|
770
|
+
copy.properties.push(typed);
|
|
771
|
+
}
|
|
772
|
+
else if (types.includes(ns.w3.shacl.NodeShape)) {
|
|
773
|
+
const typed = item;
|
|
774
|
+
typed.properties.forEach(i => copy.properties.push(i));
|
|
775
|
+
}
|
|
776
|
+
else if (types.includes(ns.aml.vocabularies.shapes.ArrayShape) || types.includes(ns.aml.vocabularies.shapes.MatrixShape)) {
|
|
777
|
+
const typed = item;
|
|
778
|
+
if (typed.items) {
|
|
779
|
+
copy.properties.push(typed.items);
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
});
|
|
783
|
+
return this._nodeShapeObject(copy);
|
|
784
|
+
}
|
|
785
|
+
if (xone.length) {
|
|
786
|
+
const { selectedUnions = [] } = this.opts;
|
|
787
|
+
let selected = xone.find(i => selectedUnions.includes(i.id));
|
|
788
|
+
if (!selected) {
|
|
789
|
+
// select firs available
|
|
790
|
+
selected = xone[0];
|
|
791
|
+
}
|
|
792
|
+
return this.processNode(selected);
|
|
793
|
+
}
|
|
794
|
+
if (or.length) {
|
|
795
|
+
const { selectedUnions = [] } = this.opts;
|
|
796
|
+
let selected = or.find(i => selectedUnions.includes(i.id));
|
|
797
|
+
if (!selected) {
|
|
798
|
+
// select firs available
|
|
799
|
+
selected = or[0];
|
|
800
|
+
}
|
|
801
|
+
return this.processNode(selected);
|
|
802
|
+
}
|
|
803
|
+
const { examples = [] } = schema;
|
|
804
|
+
const label = shapeToXmlTagName(schema);
|
|
805
|
+
if (this.opts.renderExamples && examples && examples.length) {
|
|
806
|
+
const example = examples.find((item) => !!item.structuredValue);
|
|
807
|
+
const value = this._exampleToObject(example);
|
|
808
|
+
const parts = [];
|
|
809
|
+
if (typeof value !== 'undefined') {
|
|
810
|
+
const valueFill = ` `;
|
|
811
|
+
parts.push(`<${label}>`);
|
|
812
|
+
parts.push(`${valueFill}${value}`);
|
|
813
|
+
parts.push(`</${label}>`);
|
|
814
|
+
}
|
|
815
|
+
return parts.join('\n');
|
|
816
|
+
}
|
|
817
|
+
return this._scalarShapeObject(schema);
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
//# sourceMappingURL=ShapeXmlSchemaGenerator.js.map
|