diff --git a/scripts/api-diff.js b/scripts/api-diff.js index 5297e5e..041da80 100644 --- a/scripts/api-diff.js +++ b/scripts/api-diff.js @@ -180,34 +180,34 @@ function findComponentUsage(details, componentName) { // Check parameters if (details.parameters) { - const hasComponent = details.parameters.some(p => - (p.$ref && schemaReferencesComponent({ $ref: p.$ref }, componentName)) || - (p.schema && schemaReferencesComponent(p.schema, componentName)) - ); + const hasComponent = details.parameters.some(p => { + // Check direct parameter reference + if (p.$ref && p.$ref.includes(`/parameters/${componentName}`)) return true; + // Check schema reference if it exists + if (p.schema && p.schema.$ref && p.schema.$ref.includes(`/schemas/${componentName}`)) return true; + return false; + }); if (hasComponent) usage.push('parameters'); } // Check requestBody if (details.requestBody) { - let hasComponent = false; - if (details.requestBody.$ref) { - hasComponent = schemaReferencesComponent({ $ref: details.requestBody.$ref }, componentName); + if (details.requestBody.$ref && details.requestBody.$ref.includes(componentName)) { + usage.push('requestBody'); } else if (details.requestBody.content) { - hasComponent = Object.values(details.requestBody.content).some(c => - c.schema && schemaReferencesComponent(c.schema, componentName) - ); + const hasComponent = Object.values(details.requestBody.content).some(c => + c.schema && c.schema.$ref && c.schema.$ref.includes(`/schemas/${componentName}`)); + if (hasComponent) usage.push('requestBody'); } - if (hasComponent) usage.push('requestBody'); } // Check responses if (details.responses) { - const hasComponent = Object.entries(details.responses).some(([code, r]) => { - if (r.$ref) return schemaReferencesComponent({ $ref: r.$ref }, componentName); - if (r.content) { - return Object.values(r.content).some(c => - c.schema && schemaReferencesComponent(c.schema, componentName) - ); + const hasComponent = Object.entries(details.responses).some(([code, response]) => { + if (response.$ref && response.$ref.includes(`/responses/${componentName}`)) return true; + if (response.content) { + return Object.values(response.content).some(c => + c.schema && c.schema.$ref && c.schema.$ref.includes(`/schemas/${componentName}`)); } return false; }); diff --git a/tests/fixtures/modified-parameter-component/current.json b/tests/fixtures/modified-parameter-component/current.json new file mode 100644 index 0000000..654b487 --- /dev/null +++ b/tests/fixtures/modified-parameter-component/current.json @@ -0,0 +1,71 @@ +{ + "openapi": "3.0.0", + "info": { + "title": "Test API", + "description": "A sample API for testing" + }, + "servers": [ + { + "url": "https://api.example.com/v1" + } + ], + "paths": { + "/user/{userId}": { + "get": { + "summary": "Get a user by ID", + "parameters": [ + { + "$ref": "#/components/parameters/UserId" + } + ], + "responses": { + "200": { + "description": "A user object", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/User" + } + } + } + }, + "404": { + "description": "User not found" + } + } + } + } + }, + "components": { + "parameters": { + "UserId": { + "name": "userID", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + }, + "schemas": { + "User": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "name": { + "type": "string" + }, + "email": { + "$ref": "#/components/schemas/Email" + } + } + }, + "Email": { + "type": "string", + "format": "email" + } + } + } +} diff --git a/tests/fixtures/modified-parameter-component/expected.md b/tests/fixtures/modified-parameter-component/expected.md new file mode 100644 index 0000000..610c16d --- /dev/null +++ b/tests/fixtures/modified-parameter-component/expected.md @@ -0,0 +1,3 @@ +## Modified +- [GET] `/user/{userId}` + - `UserId` modified in parameters diff --git a/tests/fixtures/modified-parameter-component/previous.json b/tests/fixtures/modified-parameter-component/previous.json new file mode 100644 index 0000000..2c13fb7 --- /dev/null +++ b/tests/fixtures/modified-parameter-component/previous.json @@ -0,0 +1,71 @@ +{ + "openapi": "3.0.0", + "info": { + "title": "Test API", + "description": "A sample API for testing" + }, + "servers": [ + { + "url": "https://api.example.com/v1" + } + ], + "paths": { + "/user/{userId}": { + "get": { + "summary": "Get a user by ID", + "parameters": [ + { + "$ref": "#/components/parameters/UserId" + } + ], + "responses": { + "200": { + "description": "A user object", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/User" + } + } + } + }, + "404": { + "description": "User not found" + } + } + } + } + }, + "components": { + "parameters": { + "UserId": { + "name": "userId", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + }, + "schemas": { + "User": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "name": { + "type": "string" + }, + "email": { + "$ref": "#/components/schemas/Email" + } + } + }, + "Email": { + "type": "string", + "format": "email" + } + } + } +}