Skip to content

Commit 00f6b62

Browse files
author
alexperez
committed
feat(W-17309546): add RAML definition and example data for user API
1 parent a1a0aaf commit 00f6b62

File tree

6 files changed

+145
-25
lines changed

6 files changed

+145
-25
lines changed

demo/W-17309546/W-17309546.raml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#%RAML 1.0
2+
title: test_for_support
3+
mediaType: application/json
4+
5+
6+
7+
types:
8+
User:
9+
type: object
10+
properties:
11+
id: integer
12+
name: string
13+
email: string
14+
postal_code: string
15+
companyDateEntry: date-only
16+
17+
/test:
18+
get:
19+
description: Obtenir la liste des utilisateurs
20+
responses:
21+
200:
22+
body:
23+
type: User[]
24+
example: !include /examples/employees/user-examples.raml
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#%RAML 1.0 NamedExample
2+
value:
3+
- id: 1
4+
name: Alice Dupont
5+
6+
companyDateEntry: 2003-05-14
7+
postal_code: "075001"
8+
- id: 2
9+
name: Bob Martin
10+
11+
companyDateEntry: 2004-05-04
12+
postal_code: "169002"
13+
- id: 3
14+
name: Charlie Durand
15+
16+
companyDateEntry: 2003-08-11
17+
postal_code: "013003"

demo/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ class ComponentDemo extends ApiDemoPage {
124124
['APIC-655', 'APIC-655'],
125125
['xml-api', 'xml-api'],
126126
['W-11843862', 'W-11843862'],
127+
['W-17309546', 'W-17309546'],
127128
['v4_0_0_api_specs', 'v4_0_0_api_specs']
128129
].map(
129130
([file, label]) => html`

demo/model.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ files.set('SE-22063/SE-22063.raml', { type: 'RAML 1.0' });
2222
files.set('APIC-332/APIC-332.raml', { type: 'RAML 1.0' });
2323
files.set('APIC-690/APIC-690.raml', { type: 'RAML 1.0' });
2424
files.set('10732397/10732397.raml', { type: 'RAML 1.0' });
25+
files.set('W-17309546/W-17309546.raml', { type: 'RAML 1.0' });
2526
files.set('oas-3-api/oas-3-api.yaml', { type: 'OAS 3.0', mime: 'application/yaml' });
2627
files.set('allof-types/allof-types.yaml', { type: 'OAS 3.0', mime: 'application/yaml' });
2728
files.set('APIC-679/APIC-679.yaml', { type: 'OAS 3.0', mime: 'application/yaml' });

src/ExampleGenerator.js

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -653,19 +653,19 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
653653

654654
// Map raw to external fragment
655655
if (!raw) {
656-
// It first retrieves the @id property from the first element
656+
// It first retrieves the @id property from the first element
657657
// of referenceIdData array and assigns it to referenceId.
658658
const referenceId = referenceIdData[0]['@id']
659659

660-
// It calls the _computeReferences method with this.amf as an
661-
// argument to get the root references and assigns
660+
// It calls the _computeReferences method with this.amf as an
661+
// argument to get the root references and assigns
662662
// the result to rootReferences.
663663
const rootReferences = this._computeReferences(this.amf)
664664

665665
// It maps over each item in rootReferences,
666-
// and for each item, it computes references twice in a nested manner.
666+
// and for each item, it computes references twice in a nested manner.
667667
// It then gets the second element from externalFragments and computes its encodes.
668-
// The result of this map operation is an array of
668+
// The result of this map operation is an array of
669669
// encoded external fragments, which is assigned to encodesOfExternalFragments.
670670
const encodesOfExternalFragments = rootReferences.map((item) => {
671671
const shapeFragment = this._computeReferences(item)
@@ -675,16 +675,16 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
675675
return this._computeEncodes(externalFragmentExample)
676676
})
677677

678-
// It finds an element in encodesOfExternalFragments where
679-
// the @id property matches referenceId and assigns
678+
// It finds an element in encodesOfExternalFragments where
679+
// the @id property matches referenceId and assigns
680680
// it to exmapleExternalFragmentByReferenceId.
681681
const exmapleExternalFragmentByReferenceId = encodesOfExternalFragments.find(externalFrament => (
682682
externalFrament['@id'] === referenceId
683683
))
684684

685685
const rawKey = this._getAmfKey(this.ns.aml.vocabularies.document.raw)
686686
// Finally, it calls the _getValue method with
687-
// exmapleExternalFragmentByReferenceId and rawKey
687+
// exmapleExternalFragmentByReferenceId and rawKey
688688
// as arguments and assigns the result to raw.
689689
raw = this._getValue(exmapleExternalFragmentByReferenceId, rawKey)
690690
}
@@ -801,7 +801,7 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
801801
}
802802
});
803803
if (isJson) {
804-
// if raw (original example) exists try to parse it to JSON
804+
// if raw (original example) exists try to parse it to JSON
805805
// if the parse process fails then use parts to build example value
806806
if (result.raw) {
807807
try {
@@ -847,27 +847,44 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
847847
return undefined;
848848
}
849849

850+
parseToJSON (arr) {
851+
return arr
852+
.filter(item => item.trim() !== "") // Remove empty strings
853+
.map(item => {
854+
if (item.startsWith("- ")) {
855+
// Handle case where item is a list of numbers
856+
return item.split("- ")
857+
.filter(subItem => subItem.trim() !== "")
858+
.map(subItem => Number(subItem.trim()));
859+
}
860+
// Handle case where item is a key-value pair
861+
const entries = item.trim().split("\n").map(line => {
862+
const index = line.indexOf(":");
863+
const key = line.slice(0, index).trim();
864+
const value = line.slice(index + 1).trim();
865+
if (value.startsWith("\"") && value.endsWith("\"")) {
866+
return [key, value.slice(1, -1)]; // Preserve leading zeros
867+
}
868+
if (!Number.isNaN(Number(value))) {
869+
return [key, Number(value)];
870+
}
871+
return [key, value];
872+
});
873+
return Object.fromEntries(entries);
874+
875+
});
876+
};
877+
850878
/**
851-
* @param {String} raw
879+
* @param {String} raw
852880
* @returns string JSON formatted
853881
*/
854882
computeRaw(raw) {
855883
const accountEntries = raw.split('-\n');
856-
const accounts = [];
857-
for (const entry of accountEntries) {
858-
if (entry !== '') {
859-
const lines = entry.split('\n');
860-
const account = {};
861-
for (const line of lines) {
862-
if (line !== '') {
863-
const [key, value] = line.split(': ');
864-
account[key.trim()] = Number(value) ? Number(value) : value.trim()
865-
}
866-
}
867-
accounts.push(account);
868-
}
869-
}
870-
return JSON.stringify(accounts, null, 2);
884+
const parsed = this.parseToJSON(accountEntries)
885+
// Ensure the parsed result is always an array
886+
const result = Array.isArray(parsed[0]) ? parsed.flat() : parsed;
887+
return JSON.stringify(result, null, 2);
871888
}
872889

873890

test/W-17309546.test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { fixture, assert, html } from '@open-wc/testing';
2+
import { AmfLoader } from './amf-loader.js';
3+
import '../api-example-generator.js';
4+
5+
describe('W-17309546', () => {
6+
async function basicFixture(amf) {
7+
return (await fixture(html`<api-example-generator
8+
.amf="${amf}"></api-example-generator>`));
9+
}
10+
11+
const apiFile = 'W-17309546';
12+
13+
[
14+
['json+ld data model', false],
15+
['Compact data model', true]
16+
].forEach(([label, compact]) => {
17+
describe(label, () => {
18+
let element;
19+
let amf;
20+
21+
before(async () => {
22+
amf = await AmfLoader.load(compact, apiFile);
23+
});
24+
25+
beforeEach(async () => {
26+
element = await basicFixture(amf);
27+
});
28+
29+
it('renders examples right', () => {
30+
const payloads = AmfLoader.lookupReturnsPayload(amf, '/test', 'get', 200);
31+
const result = element.generatePayloadsExamples(payloads, 'application/json');
32+
assert.typeOf(result, 'array');
33+
const item = result[0];
34+
assert.equal(item.value, `[
35+
{
36+
"id": 1,
37+
"name": "Alice Dupont",
38+
"email": "[email protected]",
39+
"companyDateEntry": "2003-05-14",
40+
"postal_code": "075001"
41+
},
42+
{
43+
"id": 2,
44+
"name": "Bob Martin",
45+
"email": "[email protected]",
46+
"companyDateEntry": "2004-05-04",
47+
"postal_code": "169002"
48+
},
49+
{
50+
"id": 3,
51+
"name": "Charlie Durand",
52+
"email": "[email protected]",
53+
"companyDateEntry": "2003-08-11",
54+
"postal_code": "013003"
55+
}
56+
]`);
57+
});
58+
});
59+
});
60+
});

0 commit comments

Comments
 (0)