Skip to content

Commit 54db28c

Browse files
authored
[W-14651333] difference in order of example payload in exchange and acm (#62)
* chore: add computeRaw method to ExampleGenerator class * chore: fix formatting issue in ExampleGenerator.js * chore: add comment
1 parent 598f6a2 commit 54db28c

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/ExampleGenerator.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,4 +533,11 @@ declare class ExampleGenerator extends AmfHelperMixin(Object) {
533533
* @return {XmlData}
534534
*/
535535
_computeXmlSerializationData(serialization): object;
536+
537+
/**
538+
* Generate JSON example string value from raw value definition.
539+
* @param {string} raw
540+
* @return {string}
541+
*/
542+
computeRaw(raw: string): string
536543
}

src/ExampleGenerator.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,17 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
757757
}
758758
});
759759
if (isJson) {
760+
// if raw (original example) exists try to parse it to JSON
761+
// if the parse process fails then use parts to build example value
762+
if (result.raw) {
763+
try {
764+
result.value = this.computeRaw(raw)
765+
return result
766+
} catch (_) {
767+
// ...
768+
}
769+
}
770+
760771
result.value = JSON.stringify(parts, null, 2);
761772
return result;
762773
}
@@ -792,6 +803,30 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
792803
return undefined;
793804
}
794805

806+
/**
807+
* @param {String} raw
808+
* @returns string JSON formatted
809+
*/
810+
computeRaw (raw) {
811+
const accountEntries = raw.split('-\n');
812+
const accounts = [];
813+
for (const entry of accountEntries) {
814+
if (entry !== '') {
815+
const lines = entry.split('\n');
816+
const account = {};
817+
for (const line of lines) {
818+
if (line !== '') {
819+
const [key, value] = line.split(': ');
820+
account[key.trim()] = Number(value) ? Number(value) : value.trim()
821+
}
822+
}
823+
accounts.push(account);
824+
}
825+
}
826+
return JSON.stringify(accounts, null, 2);
827+
}
828+
829+
795830
/**
796831
* Computes list of examples for an array shape.
797832
* @param {Object} schema The AMF's array shape

test/ExampleGenerator.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3163,4 +3163,27 @@ describe('ExampleGenerator', () => {
31633163
});
31643164
});
31653165
});
3166+
3167+
describe('computeRaw', () => {
3168+
[
3169+
['json+ld data model', false],
3170+
['Compact data model', true],
3171+
].forEach((args) => {
3172+
const compact = /** @type boolean */ (args[1]);
3173+
/** @type ExampleGenerator */
3174+
let element;
3175+
let amf;
3176+
3177+
beforeEach(async () => {
3178+
amf = await AmfLoader.load(/** @type Boolean */(compact));
3179+
element = new ExampleGenerator(amf);
3180+
});
3181+
3182+
it('should correctly transform raw string to JSON', () => {
3183+
const raw = "-\n balance: 200\n approval-status: P\n account-id: de7228b9-f6bb-4261-9d17-a3ddd5802e03\n account-name: Plaid Saving\n account-number: '1111222233331111'\n account-routing-number: '123123123'\n institution-name: Sample Bank\n institution-id: b3dedb19-157c-4239-880f-a125ef4384e2\n created-by: WREX\n modified-by: WREX\n created-at: February 19, 2023, 3:16:01 AM\n modified-at: February 19, 2023, 3:16:01 AM\n-\n balance: 100\n approval-status: P\n account-id: e7bb8f6d-fdc0-4873-9609-d2f2713900ed\n account-name: Plaid Checking\n account-number: '1111222233330000 '\n account-routing-number: '123123123'\n institution-name: Sample Bank\n institution-id: b3dedb19-157c-4239-880f-a125ef4384e2\n created-by: WREX\n modified-by: WREX\n created-at: February 19, 2023, 3:16:01 AM\n modified-at: February 19, 2023, 3:16:01 AM";
3184+
const expectedJson = '[\n {\n "balance": 200,\n "approval-status": "P",\n "account-id": "de7228b9-f6bb-4261-9d17-a3ddd5802e03",\n "account-name": "Plaid Saving",\n "account-number": "\'1111222233331111\'",\n "account-routing-number": "\'123123123\'",\n "institution-name": "Sample Bank",\n "institution-id": "b3dedb19-157c-4239-880f-a125ef4384e2",\n "created-by": "WREX",\n "modified-by": "WREX",\n "created-at": "February 19, 2023, 3:16:01 AM",\n "modified-at": "February 19, 2023, 3:16:01 AM"\n },\n {\n "balance": 100,\n "approval-status": "P",\n "account-id": "e7bb8f6d-fdc0-4873-9609-d2f2713900ed",\n "account-name": "Plaid Checking",\n "account-number": "\'1111222233330000 \'",\n "account-routing-number": "\'123123123\'",\n "institution-name": "Sample Bank",\n "institution-id": "b3dedb19-157c-4239-880f-a125ef4384e2",\n "created-by": "WREX",\n "modified-by": "WREX",\n "created-at": "February 19, 2023, 3:16:01 AM",\n "modified-at": "February 19, 2023, 3:16:01 AM"\n }\n]';
3185+
assert.equal(element.computeRaw(raw), expectedJson);
3186+
});
3187+
});
3188+
});
31663189
});

0 commit comments

Comments
 (0)