Skip to content

Commit

Permalink
perf: only include components if circular $ref pointers are in the JS…
Browse files Browse the repository at this point in the history
…ON Schema (#479)

* fix: only include components if $ref pointers are in the json schema

* test: fixing some broken tests

* feat: only including components in response schemas if there's circular refs

* test: fixing a broken test
  • Loading branch information
erunion authored Aug 12, 2021
1 parent f17bf5d commit f3193a0
Show file tree
Hide file tree
Showing 12 changed files with 646 additions and 1,128 deletions.
66 changes: 65 additions & 1 deletion __tests__/__datasets__/circular.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
},
"servers": [
{
"url": "https://httpsbin.org"
"url": "https://httpbin.org/anything"
}
],
"paths": {
"/": {
"get": {
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
Expand Down Expand Up @@ -60,6 +61,51 @@
}
}
}
},
"responses": {
"200": {
"description": "OK"
}
}
},
"put": {
"description": "This operation is different because it has a circular ref array as a parameter and in its response, but not its request body.",
"parameters": [
{
"name": "content",
"in": "header",
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/SalesLine"
}
}
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/SalesLine"
}
}
}
}
},
"201": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/SalesLine"
}
}
}
}
}
}
}
Expand Down Expand Up @@ -106,6 +152,24 @@
}
}
}
},
"SalesLine": {
"type": "object",
"properties": {
"stock": {
"$ref": "#/components/schemas/ProductStock"
}
}
},
"ProductStock": {
"properties": {
"test_param": {
"type": "array",
"items": {
"$ref": "#/components/schemas/SalesLine"
}
}
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions __tests__/__fixtures__/create-oas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const Oas = require('../../src');

module.exports = function createOas(operation, components) {
const schema = {
paths: {
'/': {
get: operation,
},
},
};

if (components) {
schema.components = components;
}

return new Oas(schema);
};
1 change: 1 addition & 0 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,7 @@ describe('#dereference()', () => {
expect(oas.paths['/'].get).toStrictEqual({
responses: {
200: {
description: 'OK',
content: {
'application/json': {
schema: {
Expand Down
6 changes: 1 addition & 5 deletions __tests__/lib/openapi-to-json-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,7 @@ describe('`example` / `examples` support', () => {

const schema = operation.getParametersAsJsonSchema()[0].schema;

expect(schema.components).toBeUndefined();
expect(schema.properties.id.examples).toStrictEqual([20]);

// Not `buster` because `doggie` is set directly alongside `name` in the definition.
Expand All @@ -749,11 +750,6 @@ describe('`example` / `examples` support', () => {
examples: ['https://example.com/dog.png'],
},
});

// `Pet` schema `id` example should not be present because that `id` was set against the `requestBody`, not the
// component.
expect(schema.components.schemas.Pet.properties.id.examples).toBeUndefined();
expect(schema.components.schemas.Pet.properties.name.examples).toStrictEqual(['doggie']);
});
});

Expand Down
Loading

0 comments on commit f3193a0

Please sign in to comment.