Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions scripts/api-diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ function schemaReferencesComponent(schema, componentName, visitedRefs = new Set(
if (schema.items && schemaReferencesComponent(schema.items, componentName, visitedRefs)) {
return true;
}

// Check additionalProperties
if (schema.additionalProperties && typeof schema.additionalProperties === 'object') {
if (schemaReferencesComponent(schema.additionalProperties, componentName, visitedRefs)) {
return true;
}
}

return false;
}
Expand All @@ -184,7 +191,10 @@ function findComponentUsage(details, componentName) {
// 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;
if (p.schema && schemaReferencesComponent(p.schema, componentName)) return true;
// Check examples
if (p.examples && Object.values(p.examples).some(e =>
e.$ref && e.$ref.includes(`/examples/${componentName}`))) return true;
return false;
});
if (hasComponent) usage.push('parameters');
Expand All @@ -195,8 +205,12 @@ function findComponentUsage(details, componentName) {
if (details.requestBody.$ref && details.requestBody.$ref.includes(componentName)) {
usage.push('requestBody');
} else if (details.requestBody.content) {
const hasComponent = Object.values(details.requestBody.content).some(c =>
c.schema && c.schema.$ref && c.schema.$ref.includes(`/schemas/${componentName}`));
const hasComponent = Object.values(details.requestBody.content).some(c => {
if (c.schema && schemaReferencesComponent(c.schema, componentName)) return true;
if (c.examples && Object.values(c.examples).some(e =>
e.$ref && e.$ref.includes(`/examples/${componentName}`))) return true;
return false;
});
if (hasComponent) usage.push('requestBody');
}
}
Expand All @@ -206,8 +220,16 @@ function findComponentUsage(details, 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 Object.values(response.content).some(c => {
if (c.schema && schemaReferencesComponent(c.schema, componentName)) return true;
if (c.examples && Object.values(c.examples).some(e =>
e.$ref && e.$ref.includes(`/examples/${componentName}`))) return true;
return false;
});
}
if (response.headers) {
return Object.values(response.headers).some(h =>
schemaReferencesComponent(h.schema, componentName));
}
return false;
});
Expand Down
81 changes: 81 additions & 0 deletions tests/fixtures/component-section/current.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"openapi": "3.0.0",
"info": {
"title": "Test API",
"description": "A sample API for testing"
},
"servers": [
{
"url": "https://api.example.com/v1"
}
],
"paths": {
"/user/{id}": {
"get": {
"summary": "Get user by ID",
"parameters": [
{
"$ref": "#/components/parameters/StringUserId"
},
{
"$ref": "#/components/parameters/NumberUserId"
}
],
"responses": {
"200": {
"description": "User found",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"userCreated": {
"type": "array",
"items": {
"$ref": "#/components/schemas/User"
}
}
}
}
}
}
}
}
}
}
},
"components": {
"parameters": {
"StringUserId": {
"type": "string",
"description": "The unique identifier for a user",
"example": 1234
},
"NumberUserId": {
"type": "number",
"description": "The unique identifier for a user",
"example": 1234
}
},
"schemas": {
"User": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"email": {
"$ref": "#/components/schemas/Email"
}
}
},
"Email": {
"type": "string",
"format": "email"
}
}
}
}
4 changes: 4 additions & 0 deletions tests/fixtures/component-section/expected.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## Modified
- [GET] `/user/{id}`
- `NumberUserId` modified in parameters
- `User` modified in responses
80 changes: 80 additions & 0 deletions tests/fixtures/component-section/previous.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"openapi": "3.0.0",
"info": {
"title": "Test API",
"description": "A sample API for testing"
},
"servers": [
{
"url": "https://api.example.com/v1"
}
],
"paths": {
"/user/{id}": {
"get": {
"summary": "Get user by ID",
"parameters": [
{
"$ref": "#/components/parameters/StringUserId"
},
{
"$ref": "#/components/parameters/NumberUserId"
}
],
"responses": {
"200": {
"description": "User found",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"userCreated": {
"type": "array",
"items": {
"$ref": "#/components/schemas/User"
}
}
}
}
}
}
}
}
}
}
},
"components": {
"parameters": {
"StringUserId": {
"type": "string",
"description": "The unique identifier for a user",
"example": 1234
},
"NumberUserId": {
"type": "number",
"description": "The unique identifier for a user",
"example": 12345
}
},
"schemas": {
"User": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"email": {
"$ref": "#/components/schemas/Email"
}
}
},
"Email": {
"type": "string"
}
}
}
}