Skip to content

Commit

Permalink
fix: quirk with request/response examples where readonly/writeonly wa…
Browse files Browse the repository at this point in the history
…s ignored in allOf (#482)

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

* test: update test snapshot
  • Loading branch information
erunion authored Aug 20, 2021
1 parent 30d7ae1 commit 1968246
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
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

0 comments on commit 1968246

Please sign in to comment.