@api-client/core 0.18.7 → 0.18.8
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/src/modeling/importers/JsonSchemaImporter.d.ts +4 -0
- package/build/src/modeling/importers/JsonSchemaImporter.d.ts.map +1 -1
- package/build/src/modeling/importers/JsonSchemaImporter.js +89 -16
- package/build/src/modeling/importers/JsonSchemaImporter.js.map +1 -1
- package/build/tsconfig.tsbuildinfo +1 -1
- package/data/models/example-generator-api.json +23 -23
- package/package.json +1 -1
- package/src/modeling/importers/JsonSchemaImporter.ts +111 -20
- package/tests/fixtures/schemas/arrays.json +35 -0
- package/tests/fixtures/schemas/complex.json +41 -0
- package/tests/fixtures/schemas/enumerated_values.json +11 -0
- package/tests/fixtures/schemas/person.json +21 -0
- package/tests/fixtures/schemas/regexp.json +12 -0
- package/tests/unit/modeling/importers/json_schema_importer.spec.ts +137 -0
|
@@ -7,6 +7,11 @@ import { DataDomain } from '../../../../src/modeling/DataDomain.js'
|
|
|
7
7
|
import type { JSONSchema7 } from 'json-schema'
|
|
8
8
|
import type { DomainEntity } from '../../../../src/modeling/DomainEntity.js'
|
|
9
9
|
import { DomainAssociation, DomainProperty } from '../../../../src/browser.js'
|
|
10
|
+
import Person from '../../../fixtures/schemas/person.json' with { type: 'json' }
|
|
11
|
+
import Arrays from '../../../fixtures/schemas/arrays.json' with { type: 'json' }
|
|
12
|
+
import EnumValues from '../../../fixtures/schemas/enumerated_values.json' with { type: 'json' }
|
|
13
|
+
import RegexpSchema from '../../../fixtures/schemas/regexp.json' with { type: 'json' }
|
|
14
|
+
import ComplexSchema from '../../../fixtures/schemas/complex.json' with { type: 'json' }
|
|
10
15
|
|
|
11
16
|
const input: InMemorySchema[] = []
|
|
12
17
|
for (const [key, value] of Object.entries(schemas)) {
|
|
@@ -43,6 +48,13 @@ function findAssociationByName(entity: DomainEntity, name: string): DomainAssoci
|
|
|
43
48
|
throw new Error(`Association with name "${name}" not found`)
|
|
44
49
|
}
|
|
45
50
|
|
|
51
|
+
async function importSchemas(input: InMemorySchema[]): Promise<DataDomain> {
|
|
52
|
+
const domain = new DataDomain({ info: { name: 'Test Domain' } })
|
|
53
|
+
const importer = new JsonSchemaImporter(domain)
|
|
54
|
+
await importer.import(input, 'imported_model')
|
|
55
|
+
return domain
|
|
56
|
+
}
|
|
57
|
+
|
|
46
58
|
test.group('JsonSchemaImporter', (g) => {
|
|
47
59
|
let domain: DataDomain
|
|
48
60
|
|
|
@@ -449,3 +461,128 @@ test.group('JsonSchemaImporter', (g) => {
|
|
|
449
461
|
)
|
|
450
462
|
})
|
|
451
463
|
})
|
|
464
|
+
|
|
465
|
+
test.group('small batch of schemas', () => {
|
|
466
|
+
test('imports a single schema', async ({ assert }) => {
|
|
467
|
+
const domain = await importSchemas(input.slice(0, 1))
|
|
468
|
+
const models = [...domain.listModels()]
|
|
469
|
+
assert.lengthOf(models, 1, 'Should import one model')
|
|
470
|
+
const entities = [...domain.listEntities()]
|
|
471
|
+
assert.lengthOf(entities, 1, 'Should import one entity')
|
|
472
|
+
assert.equal(entities[0].info.name, '3_d_model', 'Entity should be 3_d_model')
|
|
473
|
+
})
|
|
474
|
+
|
|
475
|
+
test('imports two schemas only', async ({ assert }) => {
|
|
476
|
+
const domain = await importSchemas(input.slice(0, 2))
|
|
477
|
+
const models = [...domain.listModels()]
|
|
478
|
+
assert.lengthOf(models, 1, 'Should import one model')
|
|
479
|
+
const entities = [...domain.listEntities()]
|
|
480
|
+
assert.lengthOf(entities, 2, 'Should import two entities')
|
|
481
|
+
assert.equal(entities[0].info.name, '3_d_model', 'Entity should be 3_d_model')
|
|
482
|
+
assert.equal(entities[1].info.name, 'am_radio_channel', 'Entity should be am_radio_channel')
|
|
483
|
+
})
|
|
484
|
+
})
|
|
485
|
+
|
|
486
|
+
//
|
|
487
|
+
// From the JSON Schema website.
|
|
488
|
+
//
|
|
489
|
+
test.group('JSON Schema Examples', () => {
|
|
490
|
+
test('imports Person schema correctly', async ({ assert }) => {
|
|
491
|
+
const domain = await importSchemas([{ path: 'person.json', contents: Person as JSONSchema7 }])
|
|
492
|
+
const models = [...domain.listModels()]
|
|
493
|
+
assert.lengthOf(models, 1, 'Should import one model')
|
|
494
|
+
const entities = [...domain.listEntities()]
|
|
495
|
+
assert.lengthOf(entities, 1, 'Should import one entity')
|
|
496
|
+
const [person] = entities
|
|
497
|
+
const properties = [...person.listProperties()]
|
|
498
|
+
assert.lengthOf(properties, 3, 'Person should have three properties')
|
|
499
|
+
assert.equal(properties[0].info.name, 'first_name', 'First property should be first_name')
|
|
500
|
+
assert.equal(properties[1].info.name, 'last_name', 'Second property should be last_name')
|
|
501
|
+
assert.equal(properties[2].info.name, 'age', 'Third property should be age')
|
|
502
|
+
assert.equal(properties[0].type, 'string', 'First property should be string')
|
|
503
|
+
assert.equal(properties[1].type, 'string', 'Second property should be string')
|
|
504
|
+
assert.equal(properties[2].type, 'number', 'Third property should be number')
|
|
505
|
+
assert.strictEqual(properties[2].schema?.minimum, 0, 'Third property should have minimum 0')
|
|
506
|
+
})
|
|
507
|
+
|
|
508
|
+
test('imports Arrays schema correctly', async ({ assert }) => {
|
|
509
|
+
const domain = await importSchemas([{ path: 'arrays.json', contents: Arrays as JSONSchema7 }])
|
|
510
|
+
const models = [...domain.listModels()]
|
|
511
|
+
assert.lengthOf(models, 1, 'Should import one model')
|
|
512
|
+
const entities = [...domain.listEntities()]
|
|
513
|
+
assert.lengthOf(entities, 2, 'Should import two entities')
|
|
514
|
+
const arraysEntity = findEntityByName(domain, 'arrays')
|
|
515
|
+
const veggieEntity = findEntityByName(domain, 'veggie')
|
|
516
|
+
assert.exists(arraysEntity, 'arrays entity should exist')
|
|
517
|
+
assert.exists(veggieEntity, 'veggie entity should exist')
|
|
518
|
+
const arraysProperties = [...arraysEntity.listProperties()]
|
|
519
|
+
const arraysAssociations = [...arraysEntity.listAssociations()]
|
|
520
|
+
assert.lengthOf(arraysProperties, 1, 'Arrays entity should have one property')
|
|
521
|
+
assert.lengthOf(arraysAssociations, 1, 'Arrays entity should have one association')
|
|
522
|
+
const veggieProperties = [...veggieEntity.listProperties()]
|
|
523
|
+
const veggieAssociations = [...veggieEntity.listAssociations()]
|
|
524
|
+
assert.lengthOf(veggieProperties, 2, 'Veggie entity should have two properties')
|
|
525
|
+
assert.lengthOf(veggieAssociations, 0, 'Veggie entity should have no associations')
|
|
526
|
+
const [vp1, vp2] = veggieProperties
|
|
527
|
+
assert.equal(vp1.info.name, 'veggie_name', 'First veggie property should be veggie_name')
|
|
528
|
+
assert.equal(vp2.info.name, 'veggie_like', 'Second veggie property should be veggie_like')
|
|
529
|
+
assert.isTrue(vp1.required, 'veggie_name should be required')
|
|
530
|
+
assert.isTrue(vp2.required, 'veggie_like should not be required')
|
|
531
|
+
})
|
|
532
|
+
|
|
533
|
+
test('imports EnumValues schema correctly', async ({ assert }) => {
|
|
534
|
+
const domain = await importSchemas([{ path: 'enumerated_values.json', contents: EnumValues as JSONSchema7 }])
|
|
535
|
+
const models = [...domain.listModels()]
|
|
536
|
+
assert.lengthOf(models, 1, 'Should import one model')
|
|
537
|
+
const entities = [...domain.listEntities()]
|
|
538
|
+
assert.lengthOf(entities, 1, 'Should import one entity')
|
|
539
|
+
const enumEntity = findEntityByName(domain, 'enumerated_values')
|
|
540
|
+
assert.exists(enumEntity, 'EnumeratedValues entity should exist')
|
|
541
|
+
const properties = [...enumEntity.listProperties()]
|
|
542
|
+
assert.lengthOf(properties, 1, 'EnumeratedValues entity should have one property')
|
|
543
|
+
const [p1] = properties
|
|
544
|
+
assert.equal(p1.info.name, 'data', 'Property should be data')
|
|
545
|
+
assert.deepEqual(
|
|
546
|
+
p1.schema?.enum,
|
|
547
|
+
['42', 'true', 'hello', 'null', '1,2,3'],
|
|
548
|
+
'Property should have correct enum values'
|
|
549
|
+
)
|
|
550
|
+
})
|
|
551
|
+
|
|
552
|
+
test('imports RegexpSchema schema correctly', async ({ assert }) => {
|
|
553
|
+
const domain = await importSchemas([{ path: 'regexp.json', contents: RegexpSchema as JSONSchema7 }])
|
|
554
|
+
const models = [...domain.listModels()]
|
|
555
|
+
assert.lengthOf(models, 1, 'Should import one model')
|
|
556
|
+
const entities = [...domain.listEntities()]
|
|
557
|
+
assert.lengthOf(entities, 1, 'Should import one entity')
|
|
558
|
+
const entity = findEntityByName(domain, 'regular_expression_pattern')
|
|
559
|
+
assert.exists(entity, 'RegularExpressionPattern entity should exist')
|
|
560
|
+
const properties = [...entity.listProperties()]
|
|
561
|
+
assert.lengthOf(properties, 1, 'RegularExpressionPattern entity should have one property')
|
|
562
|
+
const [p1] = properties
|
|
563
|
+
assert.equal(p1.info.name, 'code', 'Property should be code')
|
|
564
|
+
assert.equal(p1.schema?.pattern, '^[A-Z]{3}-\\d{3}$', 'Property should have correct pattern')
|
|
565
|
+
})
|
|
566
|
+
|
|
567
|
+
test('imports ComplexSchema schema correctly', async ({ assert }) => {
|
|
568
|
+
const domain = await importSchemas([{ path: 'complex.json', contents: ComplexSchema as JSONSchema7 }])
|
|
569
|
+
const models = [...domain.listModels()]
|
|
570
|
+
assert.lengthOf(models, 1, 'Should import one model')
|
|
571
|
+
const entities = [...domain.listEntities()]
|
|
572
|
+
assert.lengthOf(entities, 2, 'Should import two entities')
|
|
573
|
+
const entity = findEntityByName(domain, 'complex_object')
|
|
574
|
+
assert.exists(entity, 'ComplexObject entity should exist')
|
|
575
|
+
const properties = [...entity.listProperties()]
|
|
576
|
+
assert.lengthOf(properties, 3, 'ComplexObject entity should have three properties')
|
|
577
|
+
const [p1, p2, p3] = properties
|
|
578
|
+
assert.equal(p1.info.name, 'name', 'First property should be name')
|
|
579
|
+
assert.equal(p2.info.name, 'age', 'Second property should be age')
|
|
580
|
+
assert.equal(p3.info.name, 'hobbies', 'Third property should be hobbies')
|
|
581
|
+
const associations = [...entity.listAssociations()]
|
|
582
|
+
assert.lengthOf(associations, 1, 'ComplexObject entity should have one association')
|
|
583
|
+
const inlineEntity = findEntityByName(domain, 'complex_object_address')
|
|
584
|
+
assert.exists(inlineEntity, 'ComplexObjectAddress entity should exist')
|
|
585
|
+
const [a1] = associations
|
|
586
|
+
assert.deepEqual(a1.targets, [{ key: inlineEntity.key }], 'Association should target ComplexObjectAddress')
|
|
587
|
+
})
|
|
588
|
+
})
|