@@ -12,12 +12,10 @@ const SCENARIO = Object.freeze({
12
12
13
13
function checkAnnotations ( csn , annotations , scenario = SCENARIO . positive , property = '' ) {
14
14
const openApi = toOpenApi ( csn ) ;
15
- if ( openApi . next ) {
16
- const { value } = openApi . next ( ) ;
17
- const schemas = Object . entries ( value [ 0 ] . components . schemas ) . filter ( ( [ key ] ) => key . startsWith ( 'sap.odm.test.A.E1' ) )
15
+ const schemas = Object . entries ( openApi . components . schemas ) . filter ( ( [ key ] ) => key . startsWith ( 'sap.odm.test.A.E1' ) )
18
16
// Test if the openAPI document was generated with some schemas.
19
- expect ( value [ 0 ] . components . schemas ) . toBeDefined ( )
20
- expect ( value [ 0 ] ) . toBeDefined ( )
17
+ expect ( openApi . components . schemas ) . toBeDefined ( )
18
+ expect ( openApi ) . toBeDefined ( )
21
19
expect ( schemas . length > 0 ) . toBeTruthy ( )
22
20
23
21
// Expect that not-allowed ODM annotations are unavailable in the schema.
@@ -59,7 +57,7 @@ function checkAnnotations(csn, annotations, scenario = SCENARIO.positive, proper
59
57
// Test that no other places contain the ODM extensions in the OpenAPI document.
60
58
61
59
// components.schemas where the schemas are not from entity E1.
62
- const notE1 = Object . entries ( value [ 0 ] . components . schemas ) . filter ( ( [ key ] ) => ! key . startsWith ( 'sap.odm.test.A.E1' ) )
60
+ const notE1 = Object . entries ( openApi . components . schemas ) . filter ( ( [ key ] ) => ! key . startsWith ( 'sap.odm.test.A.E1' ) )
63
61
for ( const [ , schema ] of notE1 ) {
64
62
const schemaString = JSON . stringify ( schema )
65
63
for ( const [ annKey ] of annotations ) {
@@ -68,11 +66,11 @@ function checkAnnotations(csn, annotations, scenario = SCENARIO.positive, proper
68
66
}
69
67
70
68
// all other components of the OpenAPI document except the schemas.
71
- const openApiNoSchemas = JSON . stringify ( { ...value [ 0 ] , components : { parameters : { ...value [ 0 ] . components . parameters } , responses : { ...value [ 0 ] . components . responses } } } )
69
+ const openApiNoSchemas = JSON . stringify ( { ...openApi , components : { parameters : { ...openApi . components . parameters } , responses : { ...openApi . components . responses } } } )
72
70
for ( const [ annKey ] of annotations ) {
73
71
expect ( openApiNoSchemas ) . not . toContain ( annKey )
74
72
}
75
- }
73
+
76
74
}
77
75
78
76
describe ( 'OpenAPI export' , ( ) => {
@@ -90,12 +88,9 @@ describe('OpenAPI export', () => {
90
88
service A {entity E { key ID : UUID; };};`
91
89
) ;
92
90
const openapi = toOpenApi ( csn ) ;
93
- if ( openapi . next ) {
94
- const { value } = openapi . next ( ) ;
95
- expect ( value [ 0 ] ) . toMatchObject ( someOpenApi ) ;
96
- // UUID elements are not required
97
- expect ( value [ 0 ] . components . schemas [ 'A.E-create' ] ) . not . toHaveProperty ( 'required' ) ;
98
- }
91
+ expect ( openapi ) . toMatchObject ( someOpenApi ) ;
92
+ // UUID elements are not required
93
+ expect ( openapi . components . schemas [ 'A.E-create' ] ) . not . toHaveProperty ( 'required' ) ;
99
94
} ) ;
100
95
101
96
test ( 'one service, namespace' , ( ) => {
@@ -104,10 +99,7 @@ describe('OpenAPI export', () => {
104
99
service A {entity E { key ID : UUID; };};`
105
100
) ;
106
101
const openapi = toOpenApi ( csn ) ;
107
- if ( openapi . next ) {
108
- const { value } = openapi . next ( ) ;
109
- expect ( value [ 0 ] ) . toMatchObject ( someOpenApi ) ;
110
- }
102
+ expect ( openapi ) . toMatchObject ( someOpenApi ) ;
111
103
} ) ;
112
104
113
105
test ( 'one service, multiple protocols' , ( ) => {
@@ -154,11 +146,8 @@ service CatalogService {
154
146
` ) ;
155
147
156
148
const openAPI = toOpenApi ( csn ) ;
157
- if ( openAPI . next ) {
158
- const { value } = openAPI . next ( ) ;
159
- expect ( value [ 0 ] ) . toBeDefined ( ) ;
160
- expect ( value [ 0 ] . tags . length ) . toBe ( 1 ) ;
161
- }
149
+ expect ( openAPI ) . toBeDefined ( ) ;
150
+ expect ( openAPI . tags . length ) . toBe ( 1 ) ;
162
151
} ) ;
163
152
164
153
test ( 'multiple services' , ( ) => {
@@ -169,16 +158,10 @@ service CatalogService {
169
158
expect ( ( ) => toOpenApi ( csn , { service : 'foo' } ) ) . toThrowError ( / n o s e r v i c e / si)
170
159
171
160
let openapi = toOpenApi ( csn , { service : 'A' } ) ;
172
- if ( openapi . next ) {
173
- const { value } = openapi . next ( ) ;
174
- expect ( value [ 0 ] ) . toMatchObject ( someOpenApi ) ;
175
- }
161
+ expect ( openapi ) . toMatchObject ( someOpenApi ) ;
176
162
177
163
openapi = toOpenApi ( csn , { service : 'B' } ) ;
178
- if ( openapi . next ) {
179
- const { value } = openapi . next ( ) ;
180
- expect ( value [ 0 ] ) . toMatchObject ( someOpenApi ) ;
181
- }
164
+ expect ( openapi ) . toMatchObject ( someOpenApi ) ;
182
165
183
166
openapi = toOpenApi ( csn , { service : 'all' } ) ;
184
167
const filesFound = new Set ( ) ;
@@ -198,16 +181,11 @@ service CatalogService {
198
181
expect ( ( ) => toOpenApi ( csn , { service : 'foo' } ) ) . toThrowError ( / n o s e r v i c e / si)
199
182
200
183
let openapi = toOpenApi ( csn , { service : 'com.sap.A' } ) ;
201
- if ( openapi . next ) {
202
- const { value } = openapi . next ( ) ;
203
- expect ( value [ 0 ] ) . toMatchObject ( someOpenApi ) ;
204
- }
184
+ expect ( openapi ) . toMatchObject ( someOpenApi ) ;
205
185
206
186
openapi = toOpenApi ( csn , { service : 'com.sap.B' } ) ;
207
- if ( openapi . next ) {
208
- const { value } = openapi . next ( ) ;
209
- expect ( value [ 0 ] ) . toMatchObject ( someOpenApi ) ;
210
- }
187
+ expect ( openapi ) . toMatchObject ( someOpenApi ) ;
188
+
211
189
openapi = toOpenApi ( csn , { service : 'all' } ) ;
212
190
const filesFound = new Set ( ) ;
213
191
for ( const [ content , metadata ] of openapi ) {
@@ -240,62 +218,44 @@ service CatalogService {
240
218
service B {entity F { key ID : UUID; };};`
241
219
) ;
242
220
let openapi = toOpenApi ( csn , { service : 'A' } ) ;
243
- if ( openapi . next ) {
244
- const { value } = openapi . next ( ) ;
245
- expect ( value [ 0 ] ) . toMatchObject ( { servers : [ { url : '/a' } ] } ) ;
246
- expect ( value [ 0 ] . info . description ) . toBe ( "Use @Core.LongDescription: '...' on your CDS service to provide a meaningful description." )
247
- }
221
+ expect ( openapi ) . toMatchObject ( { servers : [ { url : '/a' } ] } ) ;
222
+ expect ( openapi . info . description ) . toBe ( "Use @Core.LongDescription: '...' on your CDS service to provide a meaningful description." )
248
223
249
224
openapi = toOpenApi ( csn , { service : 'A' , 'openapi:url' : 'http://foo.bar:8080' } ) ;
250
- if ( openapi . next ) {
251
- const { value } = openapi . next ( ) ;
252
- expect ( value [ 0 ] ) . toMatchObject ( { servers : [ { url : 'http://foo.bar:8080' } ] } ) ;
253
- }
225
+ expect ( openapi ) . toMatchObject ( { servers : [ { url : 'http://foo.bar:8080' } ] } ) ;
226
+
254
227
255
228
openapi = toOpenApi ( csn , { service : 'A' , 'openapi:url' : 'http://foo.bar:8080//${service-path}/foo' } ) ;
256
- if ( openapi . next ) {
257
- const { value } = openapi . next ( ) ;
258
- expect ( value [ 0 ] ) . toMatchObject ( { servers : [ { url : 'http://foo.bar:8080/a/foo' } ] } ) ;
259
- }
229
+ expect ( openapi ) . toMatchObject ( { servers : [ { url : 'http://foo.bar:8080/a/foo' } ] } ) ;
230
+
260
231
} ) ;
261
232
262
233
test ( 'options: diagram' , ( ) => {
263
234
const csn = cds . compile . to . csn ( `
264
235
service A {entity E { key ID : UUID; };};`
265
236
) ;
266
237
let openapi = toOpenApi ( csn ) ;
267
- if ( openapi . next ) {
268
- const { value } = openapi . next ( ) ;
269
- expect ( value [ 0 ] . info . description ) . not . toMatch ( / y u m l .* d i a g r a m / i) ;
270
- }
238
+ expect ( openapi . info . description ) . not . toMatch ( / y u m l .* d i a g r a m / i) ;
239
+
271
240
openapi = toOpenApi ( csn , { 'openapi:diagram' : true } ) ;
272
- if ( openapi . next ) {
273
- const { value } = openapi . next ( ) ;
274
- expect ( value [ 0 ] . info . description ) . toMatch ( / y u m l .* d i a g r a m / i) ;
275
- }
241
+ expect ( openapi . info . description ) . toMatch ( / y u m l .* d i a g r a m / i) ;
276
242
} ) ;
277
243
278
244
test ( 'options: servers' , ( ) => {
279
245
const csn = cds . compile . to . csn ( `
280
246
service A {entity E { key ID : UUID; };};`
281
247
) ;
282
248
const serverObj = "[{\n \"url\": \"https://{customerId}.saas-app.com:{port}/v2\",\n \"variables\": {\n \"customerId\": \"demo\",\n \"description\": \"Customer ID assigned by the service provider\"\n }\n}]"
283
- const openapi = toOpenApi ( csn , { 'openapi:servers' : serverObj } ) ;
284
- if ( openapi . next ) {
285
- const { value } = openapi . next ( ) ;
286
- expect ( value [ 0 ] . servers ) . toBeTruthy ( ) ;
287
- }
249
+ const openapi = toOpenApi ( csn , { 'openapi:servers' : serverObj } )
250
+ expect ( openapi . servers ) . toBeTruthy ( ) ;
288
251
} ) ;
289
252
290
253
test ( 'options: odata-version check server URL' , ( ) => {
291
254
const csn = cds . compile . to . csn ( `
292
255
service A {entity E { key ID : UUID; };};`
293
256
) ;
294
257
const openapi = toOpenApi ( csn , { 'odata-version' : '4.0' } ) ;
295
- if ( openapi . next ) {
296
- const { value } = openapi . next ( ) ;
297
- expect ( value [ 0 ] . servers [ 0 ] . url ) . toMatch ( 'odata' ) ;
298
- }
258
+ expect ( openapi . servers [ 0 ] . url ) . toMatch ( 'odata' ) ;
299
259
} ) ;
300
260
301
261
test ( 'options: Multiple servers' , ( ) => {
@@ -304,11 +264,8 @@ service CatalogService {
304
264
) ;
305
265
const serverObj = "[{\n \"url\": \"https://{customer1Id}.saas-app.com:{port}/v2\",\n \"variables\": {\n \"customer1Id\": \"demo\",\n \"description\": \"Customer1 ID assigned by the service provider\"\n }\n}, {\n \"url\": \"https://{customer2Id}.saas-app.com:{port}/v2\",\n \"variables\": {\n \"customer2Id\": \"demo\",\n \"description\": \"Customer2 ID assigned by the service provider\"\n }\n}]"
306
266
const openapi = toOpenApi ( csn , { 'openapi:servers' : serverObj } ) ;
307
- if ( openapi . next ) {
308
- const { value } = openapi . next ( ) ;
309
- expect ( value [ 0 ] . servers ) . toBeTruthy ( ) ;
310
- expect ( value [ 0 ] . servers [ 0 ] . url ) . toMatch ( 'https://{customer1Id}.saas-app.com:{port}/v2' )
311
- }
267
+ expect ( openapi . servers ) . toBeTruthy ( ) ;
268
+ expect ( openapi . servers [ 0 ] . url ) . toMatch ( 'https://{customer1Id}.saas-app.com:{port}/v2' )
312
269
} ) ;
313
270
314
271
@@ -330,13 +287,10 @@ service CatalogService {
330
287
service A {entity E { key ID : UUID; };};`
331
288
) ;
332
289
const openapi = toOpenApi ( csn , { 'openapi:config-file' : path . resolve ( "./test/lib/compile/data/configFile.json" ) } ) ;
333
- if ( openapi . next ) {
334
- const { value } = openapi . next ( ) ;
335
- expect ( value [ 0 ] . servers ) . toBeTruthy ( ) ;
336
- expect ( value [ 0 ] ) . toMatchObject ( { servers : [ { url : 'http://foo.bar:8080' } , { url : "http://foo.bar:8080/a/foo" } ] } ) ;
337
- expect ( value [ 0 ] . info . description ) . toMatch ( / y u m l .* d i a g r a m / i) ;
338
- expect ( value [ 0 ] [ 'x-odata-version' ] ) . toMatch ( '4.1' ) ;
339
- }
290
+ expect ( openapi . servers ) . toBeTruthy ( ) ;
291
+ expect ( openapi ) . toMatchObject ( { servers : [ { url : 'http://foo.bar:8080' } , { url : "http://foo.bar:8080/a/foo" } ] } ) ;
292
+ expect ( openapi . info . description ) . toMatch ( / y u m l .* d i a g r a m / i) ;
293
+ expect ( openapi [ 'x-odata-version' ] ) . toMatch ( '4.1' ) ;
340
294
} ) ;
341
295
342
296
test ( 'options: config-file - with inline options, inline options given precedence' , ( ) => {
@@ -350,13 +304,10 @@ service CatalogService {
350
304
'openapi:diagram' : "false"
351
305
}
352
306
const openapi = toOpenApi ( csn , options ) ;
353
- if ( openapi . next ) {
354
- const { value } = openapi . next ( ) ;
355
- expect ( value [ 0 ] . info . title ) . toMatch ( / h t t p : \/ \/ e x a m p l e .c o m : 8 0 8 0 / i)
356
- expect ( value [ 0 ] . info . description ) . not . toMatch ( / y u m l .* d i a g r a m / i) ;
357
- expect ( value [ 0 ] [ 'x-odata-version' ] ) . toMatch ( '4.0' ) ;
358
- expect ( value [ 0 ] ) . toMatchObject ( { servers : [ { url : 'http://foo.bar:8080' } , { url : "http://foo.bar:8080/a/foo" } ] } ) ;
359
- }
307
+ expect ( openapi . info . title ) . toMatch ( / h t t p : \/ \/ e x a m p l e .c o m : 8 0 8 0 / i)
308
+ expect ( openapi . info . description ) . not . toMatch ( / y u m l .* d i a g r a m / i) ;
309
+ expect ( openapi [ 'x-odata-version' ] ) . toMatch ( '4.0' ) ;
310
+ expect ( openapi ) . toMatchObject ( { servers : [ { url : 'http://foo.bar:8080' } , { url : "http://foo.bar:8080/a/foo" } ] } ) ;
360
311
} ) ;
361
312
362
313
test ( 'annotations: root entity property' , ( ) => {
@@ -370,13 +321,10 @@ service CatalogService {
370
321
` )
371
322
372
323
const openAPI = toOpenApi ( csn )
373
- if ( openAPI . next ) {
374
- const { value } = openAPI . next ( ) ;
375
- expect ( value [ 0 ] ) . toBeDefined ( )
376
- expect ( value [ 0 ] . components . schemas [ "sap.odm.test.A.E1" ] ) . toMatchObject ( { "x-sap-root-entity" : true } )
377
- expect ( value [ 0 ] . components . schemas [ "sap.odm.test.A.E1-create" ] [ "x-sap-root-entity" ] ) . toBeUndefined ( )
378
- expect ( value [ 0 ] . components . schemas [ "sap.odm.test.A.E1-update" ] [ "x-sap-root-entity" ] ) . toBeUndefined ( )
379
- }
324
+ expect ( openAPI ) . toBeDefined ( )
325
+ expect ( openAPI . components . schemas [ "sap.odm.test.A.E1" ] ) . toMatchObject ( { "x-sap-root-entity" : true } )
326
+ expect ( openAPI . components . schemas [ "sap.odm.test.A.E1-create" ] [ "x-sap-root-entity" ] ) . toBeUndefined ( )
327
+ expect ( openAPI . components . schemas [ "sap.odm.test.A.E1-update" ] [ "x-sap-root-entity" ] ) . toBeUndefined ( )
380
328
} ) ;
381
329
382
330
test ( 'odm annotations: entity name and oid property' , ( ) => {
@@ -482,12 +430,9 @@ service CatalogService {
482
430
}
483
431
}` ) ;
484
432
const openAPI = toOpenApi ( csn ) ;
485
- if ( openAPI . next ) {
486
- const { value } = openAPI . next ( ) ;
487
- expect ( value [ 0 ] . externalDocs ) . toBeDefined ( ) ;
488
- expect ( value [ 0 ] . externalDocs . description ) . toBe ( 'API Guide' ) ;
489
- expect ( value [ 0 ] . externalDocs . url ) . toBe ( 'https://help.sap.com/docs/product/123.html' ) ;
490
- }
433
+ expect ( openAPI . externalDocs ) . toBeDefined ( ) ;
434
+ expect ( openAPI . externalDocs . description ) . toBe ( 'API Guide' ) ;
435
+ expect ( openAPI . externalDocs . url ) . toBe ( 'https://help.sap.com/docs/product/123.html' ) ;
491
436
}
492
437
) ;
493
438
@@ -530,19 +475,16 @@ service CatalogService {
530
475
531
476
}` ) ;
532
477
const openAPI = toOpenApi ( csn ) ;
533
- if ( openAPI . next ) {
534
- const { value} = openAPI . next ( ) ;
535
- expect ( value [ 0 ] ) . toBeDefined ( ) ;
536
- expect ( value [ 0 ] [ 'x-sap-compliance-level' ] ) . toBe ( 'sap:base:v1' ) ;
537
- expect ( value [ 0 ] [ 'x-sap-ext-overview' ] . name ) . toBe ( 'Communication Scenario' ) ;
538
- expect ( value [ 0 ] [ 'x-sap-ext-overview' ] . values . text ) . toBe ( 'Planning Calendar API Integration' ) ;
539
- expect ( value [ 0 ] [ 'x-sap-ext-overview' ] . values . format ) . toBe ( 'plain' ) ;
540
- expect ( value [ 0 ] . components . schemas [ "sap.OpenAPI.test.A.E1" ] [ "x-sap-dpp-is-potentially-sensitive" ] ) . toBe ( 'true' ) ;
541
- expect ( value [ 0 ] . paths [ "/F1" ] . get [ "x-sap-operation-intent" ] ) . toBe ( 'read-collection for function' ) ;
542
- expect ( value [ 0 ] . paths [ "/A1" ] . post [ "x-sap-operation-intent" ] ) . toBe ( 'read-collection for action' ) ;
543
- expect ( value [ 0 ] . paths [ "/F1" ] . get [ "x-sap-deprecated-operation" ] . deprecationDate ) . toBe ( '2022-12-31' ) ;
544
- expect ( value [ 0 ] . paths [ "/F1" ] . get [ "x-sap-deprecated-operation" ] . successorOperationId ) . toBe ( 'successorOperation' ) ;
545
- expect ( value [ 0 ] . paths [ "/F1" ] . get [ "x-sap-deprecated-operation" ] . notValidKey ) . toBeUndefined ( ) ;
546
- }
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 ( ) ;
547
489
} ) ;
548
490
} ) ;
0 commit comments