[o2k] fix name generation (#3318)

Co-authored-by: Opender Singh <opender.singh@konghq.com>
This commit is contained in:
Thijs Schreijer 2021-04-27 03:00:08 +02:00 committed by GitHub
parent 84e47da5f2
commit a5f67bbb35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 26 additions and 24 deletions

View File

@ -16,14 +16,14 @@
"methods": ["GET"], "methods": ["GET"],
"strip_path": false, "strip_path": false,
"tags": ["OAS3_import", "OAS3file_api-with-examples.yaml"], "tags": ["OAS3_import", "OAS3file_api-with-examples.yaml"],
"name": "x_kong_name_override_at_method", "name": "Simple_API_overview-x_kong_name_override_at_method",
"paths": ["/$"] "paths": ["/$"]
}, },
{ {
"methods": ["GET"], "methods": ["GET"],
"strip_path": false, "strip_path": false,
"tags": ["OAS3_import", "OAS3file_api-with-examples.yaml"], "tags": ["OAS3_import", "OAS3file_api-with-examples.yaml"],
"name": "getVersionDetailsv2", "name": "Simple_API_overview-getVersionDetailsv2",
"paths": ["/v2$"] "paths": ["/v2$"]
} }
], ],

View File

@ -12,35 +12,35 @@
"methods": ["GET"], "methods": ["GET"],
"strip_path": false, "strip_path": false,
"tags": ["OAS3_import", "OAS3file_link-example.yaml"], "tags": ["OAS3_import", "OAS3file_link-example.yaml"],
"name": "getUserByName", "name": "Link_Example-getUserByName",
"paths": ["\/2.0\/users\/(?<username>[^\\\/\\s]+)$"] "paths": ["\/2.0\/users\/(?<username>[^\\\/\\s]+)$"]
}, },
{ {
"methods": ["GET"], "methods": ["GET"],
"strip_path": false, "strip_path": false,
"tags": ["OAS3_import", "OAS3file_link-example.yaml"], "tags": ["OAS3_import", "OAS3file_link-example.yaml"],
"name": "getRepositoriesByOwner", "name": "Link_Example-getRepositoriesByOwner",
"paths": ["\/2.0\/repositories\/(?<username>[^\\\/\\s]+)$"] "paths": ["\/2.0\/repositories\/(?<username>[^\\\/\\s]+)$"]
}, },
{ {
"methods": ["GET"], "methods": ["GET"],
"strip_path": false, "strip_path": false,
"tags": ["OAS3_import", "OAS3file_link-example.yaml"], "tags": ["OAS3_import", "OAS3file_link-example.yaml"],
"name": "getRepository", "name": "Link_Example-getRepository",
"paths": ["\/2.0\/repositories\/(?<username>[^\\\/\\s]+)\/(?<slug>[^\\\/\\s]+)$"] "paths": ["\/2.0\/repositories\/(?<username>[^\\\/\\s]+)\/(?<slug>[^\\\/\\s]+)$"]
}, },
{ {
"methods": ["GET"], "methods": ["GET"],
"strip_path": false, "strip_path": false,
"tags": ["OAS3_import", "OAS3file_link-example.yaml"], "tags": ["OAS3_import", "OAS3file_link-example.yaml"],
"name": "getPullRequestsByRepository", "name": "Link_Example-getPullRequestsByRepository",
"paths": ["\/2.0\/repositories\/(?<username>[^\\\/\\s]+)\/(?<slug>[^\\\/\\s]+)\/pullrequests$"] "paths": ["\/2.0\/repositories\/(?<username>[^\\\/\\s]+)\/(?<slug>[^\\\/\\s]+)\/pullrequests$"]
}, },
{ {
"methods": ["GET"], "methods": ["GET"],
"strip_path": false, "strip_path": false,
"tags": ["OAS3_import", "OAS3file_link-example.yaml"], "tags": ["OAS3_import", "OAS3file_link-example.yaml"],
"name": "getPullRequestsById", "name": "Link_Example-getPullRequestsById",
"paths": [ "paths": [
"\/2.0\/repositories\/(?<username>[^\\\/\\s]+)\/(?<slug>[^\\\/\\s]+)\/pullrequests\/(?<pid>[^\\\/\\s]+)$" "\/2.0\/repositories\/(?<username>[^\\\/\\s]+)\/(?<slug>[^\\\/\\s]+)\/pullrequests\/(?<pid>[^\\\/\\s]+)$"
] ]
@ -49,7 +49,7 @@
"methods": ["POST"], "methods": ["POST"],
"strip_path": false, "strip_path": false,
"tags": ["OAS3_import", "OAS3file_link-example.yaml"], "tags": ["OAS3_import", "OAS3file_link-example.yaml"],
"name": "mergePullRequest", "name": "Link_Example-mergePullRequest",
"paths": [ "paths": [
"\/2.0\/repositories\/(?<username>[^\\\/\\s]+)\/(?<slug>[^\\\/\\s]+)\/pullrequests\/(?<pid>[^\\\/\\s]+)\/merge$" "\/2.0\/repositories\/(?<username>[^\\\/\\s]+)\/(?<slug>[^\\\/\\s]+)\/pullrequests\/(?<pid>[^\\\/\\s]+)\/merge$"
] ]

View File

@ -12,14 +12,14 @@
"methods": ["GET"], "methods": ["GET"],
"strip_path": false, "strip_path": false,
"tags": ["OAS3_import", "OAS3file_no-targets-example.yaml"], "tags": ["OAS3_import", "OAS3file_no-targets-example.yaml"],
"name": "x_kong_name_override_at_method", "name": "Simple_API_overview-x_kong_name_override_at_method",
"paths": ["/$"] "paths": ["/$"]
}, },
{ {
"methods": ["GET"], "methods": ["GET"],
"strip_path": false, "strip_path": false,
"tags": ["OAS3_import", "OAS3file_no-targets-example.yaml"], "tags": ["OAS3_import", "OAS3file_no-targets-example.yaml"],
"name": "getVersionDetailsv2", "name": "Simple_API_overview-getVersionDetailsv2",
"paths": ["/v2$"] "paths": ["/v2$"]
} }
], ],

View File

@ -12,28 +12,28 @@
"methods": ["GET"], "methods": ["GET"],
"strip_path": false, "strip_path": false,
"tags": ["OAS3_import", "OAS3file_petstore-expanded.yaml"], "tags": ["OAS3_import", "OAS3file_petstore-expanded.yaml"],
"name": "findPets", "name": "Swagger_Petstore-findPets",
"paths": ["/pets$"] "paths": ["/pets$"]
}, },
{ {
"methods": ["POST"], "methods": ["POST"],
"strip_path": false, "strip_path": false,
"tags": ["OAS3_import", "OAS3file_petstore-expanded.yaml"], "tags": ["OAS3_import", "OAS3file_petstore-expanded.yaml"],
"name": "addPet", "name": "Swagger_Petstore-addPet",
"paths": ["/pets$"] "paths": ["/pets$"]
}, },
{ {
"methods": ["GET"], "methods": ["GET"],
"strip_path": false, "strip_path": false,
"tags": ["OAS3_import", "OAS3file_petstore-expanded.yaml"], "tags": ["OAS3_import", "OAS3file_petstore-expanded.yaml"],
"name": "find pet by id", "name": "Swagger_Petstore-find_pet_by_id",
"paths": ["\/pets\/(?<id>[^\\\/\\s]+)$"] "paths": ["\/pets\/(?<id>[^\\\/\\s]+)$"]
}, },
{ {
"methods": ["DELETE"], "methods": ["DELETE"],
"strip_path": false, "strip_path": false,
"tags": ["OAS3_import", "OAS3file_petstore-expanded.yaml"], "tags": ["OAS3_import", "OAS3file_petstore-expanded.yaml"],
"name": "deletePet", "name": "Swagger_Petstore-deletePet",
"paths": ["\/pets\/(?<id>[^\\\/\\s]+)$"] "paths": ["\/pets\/(?<id>[^\\\/\\s]+)$"]
} }
], ],

View File

@ -12,21 +12,21 @@
"methods": ["GET"], "methods": ["GET"],
"strip_path": false, "strip_path": false,
"tags": ["OAS3_import", "OAS3file_petstore.yaml"], "tags": ["OAS3_import", "OAS3file_petstore.yaml"],
"name": "listPets", "name": "Swagger_Petstore-listPets",
"paths": ["/pets$"] "paths": ["/pets$"]
}, },
{ {
"methods": ["POST"], "methods": ["POST"],
"strip_path": false, "strip_path": false,
"tags": ["OAS3_import", "OAS3file_petstore.yaml"], "tags": ["OAS3_import", "OAS3file_petstore.yaml"],
"name": "createPets", "name": "Swagger_Petstore-createPets",
"paths": ["/pets$"] "paths": ["/pets$"]
}, },
{ {
"methods": ["GET"], "methods": ["GET"],
"strip_path": false, "strip_path": false,
"tags": ["OAS3_import", "OAS3file_petstore.yaml"], "tags": ["OAS3_import", "OAS3file_petstore.yaml"],
"name": "showPetById", "name": "Swagger_Petstore-showPetById",
"paths": ["\/pets\/(?<petId>[^\\\/\\s]+)$"] "paths": ["\/pets\/(?<petId>[^\\\/\\s]+)$"]
} }
], ],

View File

@ -12,21 +12,21 @@
"methods": ["GET"], "methods": ["GET"],
"strip_path": false, "strip_path": false,
"tags": ["OAS3_import", "OAS3file_uspto.yaml"], "tags": ["OAS3_import", "OAS3file_uspto.yaml"],
"name": "list-data-sets", "name": "USPTO_Data_Set_API-list_data_sets",
"paths": ["/$"] "paths": ["/$"]
}, },
{ {
"methods": ["GET"], "methods": ["GET"],
"strip_path": false, "strip_path": false,
"tags": ["OAS3_import", "OAS3file_uspto.yaml"], "tags": ["OAS3_import", "OAS3file_uspto.yaml"],
"name": "list-searchable-fields", "name": "USPTO_Data_Set_API-list_searchable_fields",
"paths": ["\/(?<dataset>[^\\\/\\s]+)\/(?<version>[^\\\/\\s]+)\/fields$"] "paths": ["\/(?<dataset>[^\\\/\\s]+)\/(?<version>[^\\\/\\s]+)\/fields$"]
}, },
{ {
"methods": ["POST"], "methods": ["POST"],
"strip_path": false, "strip_path": false,
"tags": ["OAS3_import", "OAS3file_uspto.yaml"], "tags": ["OAS3_import", "OAS3file_uspto.yaml"],
"name": "perform-search", "name": "USPTO_Data_Set_API-perform_search",
"paths": ["\/(?<dataset>[^\\\/\\s]+)\/(?<version>[^\\\/\\s]+)\/records$"] "paths": ["\/(?<dataset>[^\\\/\\s]+)\/(?<version>[^\\\/\\s]+)\/records$"]
} }
], ],

View File

@ -27,7 +27,7 @@ const compare = (expected: string, pathItem: OA3PathItem) => {
describe('names', () => { describe('names', () => {
it(`api.paths[path][method]['x-kong-name'] is highest priority`, () => { it(`api.paths[path][method]['x-kong-name'] is highest priority`, () => {
compare('method_smash', { compare('Nebulo_9-method_smash', {
'x-kong-name': 'pathItem-smash', 'x-kong-name': 'pathItem-smash',
post: { post: {
'x-kong-name': 'method-smash', 'x-kong-name': 'method-smash',
@ -37,7 +37,7 @@ describe('names', () => {
}); });
it('api.paths[path][method].operationId is second priority (and not slugified)', () => { it('api.paths[path][method].operationId is second priority (and not slugified)', () => {
compare('operationId-smash', { compare('Nebulo_9-operationId_smash', {
'x-kong-name': 'pathItem-smash', 'x-kong-name': 'pathItem-smash',
post: { post: {
operationId: 'operationId-smash', operationId: 'operationId-smash',

View File

@ -157,11 +157,13 @@ export function generateRouteName(
const pathItem = api.paths[routePath]; const pathItem = api.paths[routePath];
if (pathItem[method] && typeof pathItem[method]['x-kong-name'] === 'string') { if (pathItem[method] && typeof pathItem[method]['x-kong-name'] === 'string') {
return generateSlug(pathItem[method]['x-kong-name']); const opsName = generateSlug(pathItem[method]['x-kong-name']);
return `${name}-${opsName}`;
} }
if (pathItem[method] && pathItem[method].operationId) { if (pathItem[method] && pathItem[method].operationId) {
return pathItem[method].operationId; const opsName = generateSlug(pathItem[method].operationId);
return `${name}-${opsName}`;
} }
// replace all `/` with `-` except the ones at the beginng or end of a string // replace all `/` with `-` except the ones at the beginng or end of a string