Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: quirk with request/response examples where readonly/writeonly was ignored in allOf #482

Merged
merged 2 commits into from
Aug 20, 2021
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
46 changes: 45 additions & 1 deletion __tests__/__datasets__/readonly-writeonly.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"servers": [
{
"url": "https://httpbin.org/"
"url": "https://httpbin.org"
}
],
"paths": {
Expand Down Expand Up @@ -69,10 +69,54 @@
}
}
}
},
"/allOf": {
"post": {
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/product_allOf"
}
}
}
},
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/product_allOf"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"product_allOf": {
"allOf": [
{ "$ref": "#/components/schemas/product" },
{
"type": "object",
"properties": {
"readOnly_primitive": {
"type": "string",
"readOnly": true
},
"writeOnly_primitive": {
"type": "string",
"writeOnly": true
}
}
}
]
},
"product": {
"type": "object",
"properties": {
Expand Down
23 changes: 23 additions & 0 deletions __tests__/operation/get-requestbody-examples.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,27 @@ describe('readOnly / writeOnly handling', () => {
},
]);
});

it('should retain `readOnly` and `writeOnly` settings when merging an allOf', async () => {
const spec = new Oas(exampleRoWo);
await spec.dereference();

const operation = spec.operation('/allOf', 'post');

expect(operation.getRequestBodyExamples()).toStrictEqual([
{
mediaType: 'application/json',
examples: [
{
value: {
end_date: '2021-08-20',
product_id: '3fa85f64-5717-4562-b3fc-2c963f66afa6',
start_date: '2021-08-20',
writeOnly_primitive: 'string',
},
},
],
},
]);
});
});
28 changes: 28 additions & 0 deletions __tests__/operation/get-response-examples.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,34 @@ describe('readOnly / writeOnly handling', () => {
},
]);
});

it('should retain `readOnly` and `writeOnly` settings when merging an allOf', async () => {
const spec = new Oas(exampleRoWo);
await spec.dereference();

const operation = spec.operation('/allOf', 'post');

expect(operation.getResponseExamples()).toStrictEqual([
{
status: '200',
mediaTypes: {
'application/json': [
{
value: {
end_date: '2021-08-20',
end_hour: 'string',
id: 'string',
product_id: '3fa85f64-5717-4562-b3fc-2c963f66afa6',
readOnly_primitive: 'string',
start_date: '2021-08-20',
start_hour: 'string',
},
},
],
},
},
]);
});
});

test('sample generation should not corrupt the supplied operation', async () => {
Expand Down
5 changes: 3 additions & 2 deletions src/samples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ const sampleFromSchema = (schema, config = {}) => {
// Ignore any unrecognized OAS-specific keywords that might be present on the schema (like `xml`).
defaultResolver: mergeAllOf.options.resolvers.title,
},
})
}),
config
);
} catch (error) {
return undefined;
}
} else if (hasPolymorphism) {
return sampleFromSchema(objectifySchema[hasPolymorphism][0]);
return sampleFromSchema(objectifySchema[hasPolymorphism][0], config);
}

const { example, additionalProperties, properties, items } = objectifySchema;
Expand Down