@@ -12,64 +12,64 @@ const SCENARIO = Object.freeze({
12
12
13
13
function checkAnnotations ( csn , annotations , scenario = SCENARIO . positive , property = '' ) {
14
14
const openApi = toOpenApi ( csn ) ;
15
- const schemas = Object . entries ( openApi . components . schemas ) . filter ( ( [ key ] ) => key . startsWith ( 'sap.odm.test.A.E1' ) )
16
- // Test if the openAPI document was generated with some schemas.
17
- expect ( openApi . components . schemas ) . toBeDefined ( )
18
- expect ( openApi ) . toBeDefined ( )
19
- expect ( schemas . length > 0 ) . toBeTruthy ( )
20
-
21
- // Expect that not-allowed ODM annotations are unavailable in the schema.
22
- if ( scenario === SCENARIO . notAllowedAnnotations ) {
23
- for ( const [ , schema ] of schemas ) {
24
- for ( const [ annKey ] of annotations ) {
25
- expect ( schema [ annKey ] ) . not . toBeDefined ( )
26
- }
27
- }
28
- return ;
29
- }
30
-
31
- // Expect that even the ODM annotations with not-matched values will be derived.
32
- if ( scenario === SCENARIO . notMatchingValues ) {
33
- for ( const [ , schema ] of schemas ) {
34
- for ( const [ annKey , annValue ] of annotations ) {
35
- expect ( schema [ annKey ] ) . toBe ( annValue )
36
- }
15
+ const schemas = Object . entries ( openApi . components . schemas ) . filter ( ( [ key ] ) => key . startsWith ( 'sap.odm.test.A.E1' ) )
16
+ // Test if the openAPI document was generated with some schemas.
17
+ expect ( openApi . components . schemas ) . toBeDefined ( )
18
+ expect ( openApi ) . toBeDefined ( )
19
+ expect ( schemas . length > 0 ) . toBeTruthy ( )
20
+
21
+ // Expect that not-allowed ODM annotations are unavailable in the schema.
22
+ if ( scenario === SCENARIO . notAllowedAnnotations ) {
23
+ for ( const [ , schema ] of schemas ) {
24
+ for ( const [ annKey ] of annotations ) {
25
+ expect ( schema [ annKey ] ) . not . toBeDefined ( )
37
26
}
38
- return ;
39
27
}
28
+ return ;
29
+ }
40
30
41
- if ( scenario === SCENARIO . checkProperty ) {
42
- for ( const [ , schema ] of schemas ) {
43
- const propertyObj = schema . properties [ property ]
44
- for ( const [ annKey , annValue ] of annotations ) {
45
- expect ( propertyObj [ annKey ] ) . toBe ( annValue )
46
- }
31
+ // Expect that even the ODM annotations with not-matched values will be derived.
32
+ if ( scenario === SCENARIO . notMatchingValues ) {
33
+ for ( const [ , schema ] of schemas ) {
34
+ for ( const [ annKey , annValue ] of annotations ) {
35
+ expect ( schema [ annKey ] ) . toBe ( annValue )
47
36
}
48
- return
49
37
}
38
+ return ;
39
+ }
50
40
41
+ if ( scenario === SCENARIO . checkProperty ) {
51
42
for ( const [ , schema ] of schemas ) {
43
+ const propertyObj = schema . properties [ property ]
52
44
for ( const [ annKey , annValue ] of annotations ) {
53
- expect ( schema [ annKey ] ) . toBe ( annValue )
45
+ expect ( propertyObj [ annKey ] ) . toBe ( annValue )
54
46
}
55
47
}
48
+ return
49
+ }
56
50
57
- // Test that no other places contain the ODM extensions in the OpenAPI document.
58
-
59
- // components.schemas where the schemas are not from entity E1.
60
- const notE1 = Object . entries ( openApi . components . schemas ) . filter ( ( [ key ] ) => ! key . startsWith ( 'sap.odm.test.A.E1' ) )
61
- for ( const [ , schema ] of notE1 ) {
62
- const schemaString = JSON . stringify ( schema )
63
- for ( const [ annKey ] of annotations ) {
64
- expect ( schemaString ) . not . toContain ( annKey )
65
- }
51
+ for ( const [ , schema ] of schemas ) {
52
+ for ( const [ annKey , annValue ] of annotations ) {
53
+ expect ( schema [ annKey ] ) . toBe ( annValue )
66
54
}
55
+ }
56
+
57
+ // Test that no other places contain the ODM extensions in the OpenAPI document.
67
58
68
- // all other components of the OpenAPI document except the schemas.
69
- const openApiNoSchemas = JSON . stringify ( { ...openApi , components : { parameters : { ...openApi . components . parameters } , responses : { ...openApi . components . responses } } } )
59
+ // components.schemas where the schemas are not from entity E1.
60
+ const notE1 = Object . entries ( openApi . components . schemas ) . filter ( ( [ key ] ) => ! key . startsWith ( 'sap.odm.test.A.E1' ) )
61
+ for ( const [ , schema ] of notE1 ) {
62
+ const schemaString = JSON . stringify ( schema )
70
63
for ( const [ annKey ] of annotations ) {
71
- expect ( openApiNoSchemas ) . not . toContain ( annKey )
64
+ expect ( schemaString ) . not . toContain ( annKey )
72
65
}
66
+ }
67
+
68
+ // all other components of the OpenAPI document except the schemas.
69
+ const openApiNoSchemas = JSON . stringify ( { ...openApi , components : { parameters : { ...openApi . components . parameters } , responses : { ...openApi . components . responses } } } )
70
+ for ( const [ annKey ] of annotations ) {
71
+ expect ( openApiNoSchemas ) . not . toContain ( annKey )
72
+ }
73
73
74
74
}
75
75
@@ -416,6 +416,87 @@ service CatalogService {
416
416
)
417
417
} )
418
418
419
+ describe ( 'ER annotations' , ( ) => {
420
+ test ( 'er annotations is correct' , ( ) => {
421
+ const csn = cds . compile . to . csn ( `
422
+ service A {
423
+ @EntityRelationship.entityType: 'sap.vdm.sont:Material'
424
+ @EntityRelationship.entityIds : [{propertyTypes: ['sap.vdm.gfn:MaterialId']}]
425
+ @ODM.entityName : 'Product'
426
+ @ODM.oid : 'oid'
427
+ entity Material {
428
+ @EntityRelationship.propertyType: 'sap.vdm.gfn:MaterialId'
429
+ key ObjectID : String(18);
430
+
431
+ @EntityRelationship.reference : {
432
+ referencedEntityType : 'sap.vdm.sont:BusinessPartner',
433
+ referencedPropertyType: 'sap.vdm.gfn::BusinessPartnerNumber'
434
+ }
435
+ manufacturer : String(40);
436
+
437
+ @EntityRelationship.reference : {
438
+ referencedEntityType : 'sap.sm:PurchaseOrder',
439
+ referencedPropertyType: 'sap.sm:PurchaseOrderUUID'
440
+ }
441
+ @ODM.oidReference.entityName : 'PurchaseOrder'
442
+ PurchaseOrder : UUID;
443
+
444
+ @EntityRelationship.reference : {
445
+ referencedEntityType : 'sap.vdm.sont:BillOfMaterial',
446
+ referencedPropertyType: 'sap.vdm.gfn:BillOfMaterialId'
447
+ }
448
+ BOM : String(30);
449
+ }
450
+ }
451
+ ` )
452
+ const openAPI = toOpenApi ( csn ) ;
453
+ expect ( openAPI ) . toBeDefined ( ) ;
454
+ const materialSchema = openAPI . components . schemas [ "A.Material" ] ;
455
+ expect ( materialSchema ) . toBeDefined ( ) ;
456
+ expect ( materialSchema [ "x-entity-relationship-entity-type" ] ) . toBe ( 'sap.vdm.sont:Material' ) ;
457
+ expect ( materialSchema [ "x-entity-relationship-entity-ids" ] ) . toMatchObject ( [ { "propertyTypes" : [ "sap.vdm.gfn:MaterialId" ] } ] ) ;
458
+ expect ( materialSchema [ "x-sap-odm-entity-name" ] ) . toBe ( 'Product' ) ;
459
+ expect ( materialSchema [ "x-sap-odm-oid" ] ) . toBe ( 'oid' ) ;
460
+
461
+ const properties = materialSchema . properties ;
462
+ expect ( properties ) . toBeDefined ( ) ;
463
+ expect ( properties . ObjectID ) . toMatchObject ( {
464
+ maxLength : 18 ,
465
+ type : 'string' ,
466
+ "x-entity-relationship-property-type" : 'sap.vdm.gfn:MaterialId'
467
+ } ) ;
468
+ expect ( properties . manufacturer ) . toMatchObject ( {
469
+ maxLength : 40 ,
470
+ nullable : true ,
471
+ type : 'string' ,
472
+ "x-entity-relationship-reference" : {
473
+ referencedEntityType : 'sap.vdm.sont:BusinessPartner' ,
474
+ referencedPropertyType : 'sap.vdm.gfn::BusinessPartnerNumber'
475
+ }
476
+ } ) ;
477
+ expect ( properties . PurchaseOrder ) . toMatchObject ( {
478
+ example : '01234567-89ab-cdef-0123-456789abcdef' ,
479
+ format : 'uuid' ,
480
+ nullable : true ,
481
+ type : 'string' ,
482
+ "x-entity-relationship-reference" : {
483
+ referencedEntityType : 'sap.sm:PurchaseOrder' ,
484
+ referencedPropertyType : 'sap.sm:PurchaseOrderUUID'
485
+ } ,
486
+ "x-sap-odm-oid-reference-entity-name" : 'PurchaseOrder'
487
+ } ) ;
488
+ expect ( properties . BOM ) . toMatchObject ( {
489
+ maxLength : 30 ,
490
+ nullable : true ,
491
+ type : 'string' ,
492
+ "x-entity-relationship-reference" : {
493
+ referencedEntityType : 'sap.vdm.sont:BillOfMaterial' ,
494
+ referencedPropertyType : 'sap.vdm.gfn:BillOfMaterialId'
495
+ }
496
+ } ) ;
497
+ } )
498
+ } ) ;
499
+
419
500
test ( 'OpenAPI annotations: @OpenAPI.externalDocs annotation is added to the schema' , ( ) => {
420
501
const csn = cds . compile . to . csn ( `
421
502
namespace sap.OpenAPI.test;
@@ -475,16 +556,16 @@ service CatalogService {
475
556
476
557
}` ) ;
477
558
const openAPI = toOpenApi ( csn ) ;
478
- expect ( openAPI ) . toBeDefined ( ) ;
479
- expect ( openAPI [ 'x-sap-compliance-level' ] ) . toBe ( 'sap:base:v1' ) ;
480
- expect ( openAPI [ 'x-sap-ext-overview' ] . name ) . toBe ( 'Communication Scenario' ) ;
481
- expect ( openAPI [ 'x-sap-ext-overview' ] . values . text ) . toBe ( 'Planning Calendar API Integration' ) ;
482
- expect ( openAPI [ 'x-sap-ext-overview' ] . values . format ) . toBe ( 'plain' ) ;
483
- expect ( openAPI . components . schemas [ "sap.OpenAPI.test.A.E1" ] [ "x-sap-dpp-is-potentially-sensitive" ] ) . toBe ( 'true' ) ;
484
- expect ( openAPI . paths [ "/F1" ] . get [ "x-sap-operation-intent" ] ) . toBe ( 'read-collection for function' ) ;
485
- expect ( openAPI . paths [ "/A1" ] . post [ "x-sap-operation-intent" ] ) . toBe ( 'read-collection for action' ) ;
486
- expect ( openAPI . paths [ "/F1" ] . get [ "x-sap-deprecated-operation" ] . deprecationDate ) . toBe ( '2022-12-31' ) ;
487
- expect ( openAPI . paths [ "/F1" ] . get [ "x-sap-deprecated-operation" ] . successorOperationId ) . toBe ( 'successorOperation' ) ;
488
- expect ( openAPI . paths [ "/F1" ] . get [ "x-sap-deprecated-operation" ] . notValidKey ) . toBeUndefined ( ) ;
559
+ expect ( openAPI ) . toBeDefined ( ) ;
560
+ expect ( openAPI [ 'x-sap-compliance-level' ] ) . toBe ( 'sap:base:v1' ) ;
561
+ expect ( openAPI [ 'x-sap-ext-overview' ] . name ) . toBe ( 'Communication Scenario' ) ;
562
+ expect ( openAPI [ 'x-sap-ext-overview' ] . values . text ) . toBe ( 'Planning Calendar API Integration' ) ;
563
+ expect ( openAPI [ 'x-sap-ext-overview' ] . values . format ) . toBe ( 'plain' ) ;
564
+ expect ( openAPI . components . schemas [ "sap.OpenAPI.test.A.E1" ] [ "x-sap-dpp-is-potentially-sensitive" ] ) . toBe ( 'true' ) ;
565
+ expect ( openAPI . paths [ "/F1" ] . get [ "x-sap-operation-intent" ] ) . toBe ( 'read-collection for function' ) ;
566
+ expect ( openAPI . paths [ "/A1" ] . post [ "x-sap-operation-intent" ] ) . toBe ( 'read-collection for action' ) ;
567
+ expect ( openAPI . paths [ "/F1" ] . get [ "x-sap-deprecated-operation" ] . deprecationDate ) . toBe ( '2022-12-31' ) ;
568
+ expect ( openAPI . paths [ "/F1" ] . get [ "x-sap-deprecated-operation" ] . successorOperationId ) . toBe ( 'successorOperation' ) ;
569
+ expect ( openAPI . paths [ "/F1" ] . get [ "x-sap-deprecated-operation" ] . notValidKey ) . toBeUndefined ( ) ;
489
570
} ) ;
490
571
} ) ;
0 commit comments