From 6c605fccd5cce95e59dc074dc6d0a8fc486df033 Mon Sep 17 00:00:00 2001 From: chris fowlkes Date: Mon, 24 Mar 2025 15:42:17 -0400 Subject: [PATCH 1/2] sort by path params --- src/parser/openapi.parser.ts | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/parser/openapi.parser.ts b/src/parser/openapi.parser.ts index 477247d..dc75295 100644 --- a/src/parser/openapi.parser.ts +++ b/src/parser/openapi.parser.ts @@ -82,7 +82,7 @@ export abstract class OpenApiParser { const components = document.components ? this.parseComponents(document.components) : {}; - const paths = (document.paths ? this.parsePaths(document.paths, components) : []).sort((a, b) => a.name.localeCompare(b.name)); + const paths = document.paths ? this.parsePaths(document.paths, components) : []; return { title, apiName, description, version, components, paths, tags }; } @@ -200,7 +200,29 @@ export abstract class OpenApiParser { definition, }); } - return nodes; + + // Sort paths to ensure more specific routes (without path parameters) come before routes with path parameters + // This prevents path parameter routes from capturing requests meant for more specific routes + return nodes.sort((a, b) => { + // Count parameters in each path + const paramsInA = (a.name.match(/{[^}]+}/g) || []).length; + const paramsInB = (b.name.match(/{[^}]+}/g) || []).length; + + // If one has parameters and the other doesn't, the one without parameters comes first + if (paramsInA === 0 && paramsInB > 0) return -1; + if (paramsInA > 0 && paramsInB === 0) return 1; + + // If both have parameters or neither has parameters, sort by parameter count (fewer first) + if (paramsInA !== paramsInB) return paramsInA - paramsInB; + + // If parameter count is the same, sort by path segments (more segments first for more specificity) + const segmentsA = a.name.split('/').filter(Boolean).length; + const segmentsB = b.name.split('/').filter(Boolean).length; + if (segmentsA !== segmentsB) return segmentsB - segmentsA; + + // If all else is equal, sort alphabetically + return a.name.localeCompare(b.name); + }); } private isReferenceObject(test: object): test is ReferenceObject { @@ -291,7 +313,7 @@ export abstract class OpenApiParser { return this.createReference(schema); } - schema.type ??= type ?? schema.enum ? 'string' : 'object'; + schema.type ??= (type ?? schema.enum) ? 'string' : 'object'; const modifiers = this.getModifiers(schema); if (this.isArraySchemaObject(schema)) { From a33bbb80a256bf91020fb8985198382ddc5b4072 Mon Sep 17 00:00:00 2001 From: chris fowlkes Date: Mon, 24 Mar 2025 18:04:16 -0400 Subject: [PATCH 2/2] bump package --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 299f4e7..53d4ff8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@flexbase/openapi-generator", - "version": "3.1.1", + "version": "3.1.13", "description": "Open API code generator", "author": { "name": "Flexbase Technologies",