Skip to content

Commit

Permalink
(fix) readonly/required with one required field fails (#421)
Browse files Browse the repository at this point in the history
* remove required if no required props exist

* (fix) #400 readonly with single required prop fails

* remove 15
  • Loading branch information
cdimascio committed Oct 27, 2020
1 parent ca87e20 commit 14cf30a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
1 change: 0 additions & 1 deletion .travis.yml
@@ -1,7 +1,6 @@
sudo: required
language: node_js
node_js:
- '15'
- '14'
- '12'
- '10'
Expand Down
3 changes: 3 additions & 0 deletions src/middlewares/parsers/request.schema.preprocessor.ts
Expand Up @@ -82,6 +82,9 @@ export class RequestSchemaPreprocessor {
schema.required = required
.slice(0, index)
.concat(required.slice(index + 1));
if (schema.required.length === 0) {
delete schema.required;
}
}
}
};
Expand Down
10 changes: 10 additions & 0 deletions test/oneof.readonly.spec.ts
Expand Up @@ -14,6 +14,9 @@ describe('one.of readonly', () => {
app.use(
express
.Router()
.post(`${app.basePath}/any_of_one_required`, (req, res) =>
res.status(200).json({ success: true }),
)
.post(`${app.basePath}/any_of`, (req, res) =>
res.status(200).json({ success: true }),
)
Expand Down Expand Up @@ -46,6 +49,13 @@ describe('one.of readonly', () => {
expect(error.message).to.include('to one of the allowed values: C, D');
}));

it('post type anyof without providing the single required readonly property should pass', async () =>
request(app)
.post(`${app.basePath}/one_of`)
.send({ type: 'C' }) // do not provide id
.set('Content-Type', 'application/json')
.expect(200));

it('post type oneOf (without readonly id) should pass', async () =>
request(app)
.post(`${app.basePath}/one_of`)
Expand Down
39 changes: 38 additions & 1 deletion test/oneof.readonly.yaml
Expand Up @@ -6,6 +6,23 @@ info:
servers:
- url: /v1
paths:
/any_of_one_required:
post:
requestBody:
content:
application/json:
schema:
anyOf:
- $ref: '#/components/schemas/subE'
- $ref: '#/components/schemas/subF'
discriminator:
propertyName: type
mapping:
A: '#/components/schemas/subE'
B: '#/components/schemas/subF'
responses:
200:
description: successful operation
/any_of:
post:
requestBody:
Expand Down Expand Up @@ -57,6 +74,18 @@ components:
- id
- type

one_required:
type: object
properties:
id:
readOnly: true
type: string
type:
type: string
enum: [A, B]
required:
- id

subA:
allOf:
- $ref: '#/components/schemas/common'
Expand Down Expand Up @@ -91,4 +120,12 @@ components:
enum: [C, D]
required:
- id
- type
- type

subE:
allOf:
- $ref: '#/components/schemas/one_required'

subF:
allOf:
- $ref: '#/components/schemas/one_required'

0 comments on commit 14cf30a

Please sign in to comment.