-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: remove 'sway' dependency from sdk generator (#1701)
## Proposed change <!-- Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any dependencies that is required for this change. --> ## Related issues - 🐛 Fixes #(issue) - 🚀 Feature #(issue) <!-- Please make sure to follow the contributing guidelines on https://github.com/amadeus-digital/Otter/blob/main/CONTRIBUTING.md --> cherry-pick from #1578
- Loading branch information
Showing
11 changed files
with
379 additions
and
325 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
packages/@ama-sdk/generator-sdk/src/generators/core/core/helpers/path-extractor.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import type { OpenAPIV3 } from 'openapi-types'; | ||
import { generateOperationFinderFromSingleFile } from './path-extractor'; | ||
|
||
describe('generateOperationFinderFromSingleFile', () => { | ||
|
||
it('should parse a full path object', () => { | ||
const spec: OpenAPIV3.Document = { | ||
openapi: '3.0.0', | ||
info: { | ||
title: 'test', | ||
version: '0.0.0' | ||
}, | ||
paths: { | ||
'/pet': { | ||
post: { | ||
description: 'Add a new pet to the store', | ||
operationId: 'addPet', | ||
responses: { | ||
200: { | ||
$ref: '' | ||
}, | ||
405: { | ||
$ref: '' | ||
} | ||
} | ||
}, | ||
put: { | ||
description: 'Update an existing pet by Id', | ||
operationId: 'updatePet', | ||
responses: { | ||
200: { | ||
$ref: '' | ||
}, | ||
405: { | ||
$ref: '' | ||
} | ||
} | ||
} | ||
}, | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
'/pet/{petId}': { | ||
get: { | ||
description: 'Returns a single pet', | ||
operationId: 'getPetById', | ||
responses: { | ||
200: { | ||
$ref: '' | ||
}, | ||
405: { | ||
$ref: '' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const result = generateOperationFinderFromSingleFile(spec); | ||
expect(result).toEqual([ | ||
{ | ||
path: '/pet', regexp: new RegExp('^/pet(?:/(?=$))?$'), operations: [{ 'method': 'post', 'operationId': 'addPet' }, { 'method': 'put', 'operationId': 'updatePet' }] | ||
}, | ||
{ | ||
path: '/pet/{petId}', regexp: new RegExp('^/pet/((?:[^/]+?))(?:/(?=$))?$'), operations: [{ 'method': 'get', 'operationId': 'getPetById' }] | ||
} | ||
]); | ||
}); | ||
|
||
it('should parse a path object with reference', () => { | ||
const spec: OpenAPIV3.Document = { | ||
openapi: '3.0.0', | ||
info: { | ||
title: 'test', | ||
version: '0.0.0' | ||
}, | ||
paths: { | ||
'/pet': { | ||
$ref: '#/components/schemas' | ||
} | ||
}, | ||
components: { | ||
schemas: { | ||
post: { | ||
description: 'Add a new pet to the store', | ||
operationId: 'addPet', | ||
responses: { | ||
200: { | ||
$ref: '' | ||
}, | ||
405: { | ||
$ref: '' | ||
} | ||
} | ||
} as any | ||
} | ||
} | ||
}; | ||
|
||
const result = generateOperationFinderFromSingleFile(spec); | ||
expect(result).toEqual([ | ||
{ | ||
path: '/pet', regexp: new RegExp('^/pet(?:/(?=$))?$'), operations: [{ 'method': 'post', 'operationId': 'addPet' }] | ||
} | ||
]); | ||
}); | ||
|
||
}); |
34 changes: 34 additions & 0 deletions
34
packages/@ama-sdk/generator-sdk/src/generators/core/core/helpers/path-extractor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// File copied from @ama-sdk/schematics generator in order to fix an issue | ||
import type { PathObject } from '@ama-sdk/core'; | ||
// eslint-disable-next-line camelcase | ||
import type { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'; | ||
|
||
/** | ||
* Parse a single specification to retrieve the Operation Finder information | ||
* This function is properly working to a specification that does not include external references in the Paths object | ||
* @param specification Specification single object to parse | ||
*/ | ||
// eslint-disable-next-line camelcase | ||
export const generateOperationFinderFromSingleFile = (specification: OpenAPIV2.Document | OpenAPIV3.Document | OpenAPIV3_1.Document): PathObject[] => { | ||
if (!specification.paths) { | ||
return []; | ||
} | ||
|
||
return Object.entries(specification.paths) | ||
.filter(([, pathObject]) => !!pathObject) | ||
.map(([path, pathObjectOrRef]) => { | ||
// eslint-disable-next-line camelcase | ||
const pathObject: Record<OpenAPIV2.HttpMethods, OpenAPIV2.OperationObject | OpenAPIV3.OperationObject | OpenAPIV3_1.OperationObject> = pathObjectOrRef.$ref | ||
? (pathObjectOrRef.$ref as string).replace(/^#\/?/, '').split('/').reduce((acc: any, ref) => acc[ref], specification) | ||
: pathObjectOrRef; | ||
return { | ||
path, | ||
regexp: new RegExp(`^${path.replace(/\{[^}]+}/g, '((?:[^/]+?))')}(?:/(?=$))?$`), | ||
operations: Object.entries(pathObject) | ||
.map(([method, reqObject]) => ({ | ||
method, | ||
operationId: reqObject.operationId | ||
})) | ||
}; | ||
}); | ||
}; |
1 change: 1 addition & 0 deletions
1
packages/@ama-sdk/generator-sdk/src/generators/core/core/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './properties'; | ||
export * from './helpers/path-extractor'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.