Skip to content

Commit

Permalink
feat: error for oneOf, anyOf, allOf schema that does not use array (#143
Browse files Browse the repository at this point in the history
)

The fields for compose models must be arrays for an OpenAPI definition. This check raises an error if this is not the case
  • Loading branch information
barrett-schonefeld authored Feb 24, 2020
1 parent 9f18deb commit 1db6b27
Show file tree
Hide file tree
Showing 8 changed files with 319 additions and 160 deletions.
23 changes: 21 additions & 2 deletions src/plugins/validation/2and3/semantic-validators/schema-ibm.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,33 @@ module.exports.validate = function({ jsSpec, isOAS3 }, config) {
This includes responses, request bodies, parameters (with content rather than schema),
both at the operation level and within the top-level "components" object
*/

const modelLocations = ['definitions', 'schemas', 'properties'];
if (
current === 'schema' ||
current === 'items' ||
modelLocations.indexOf(path[path.length - 2]) > -1
modelLocations.includes(path[path.length - 2])
) {
const pushLeafSchemas = (obj, path) => {
if (obj.allOf && Array.isArray(obj.allOf)) {
if (obj.allOf && !Array.isArray(obj.allOf)) {
messages.addMessage(
path.concat(['allOf']),
'allOf value should be an array',
'error'
);
} else if (isOAS3 && obj.oneOf && !Array.isArray(obj.oneOf)) {
messages.addMessage(
path.concat(['oneOf']),
'oneOf value should be an array',
'error'
);
} else if (isOAS3 && obj.anyOf && !Array.isArray(obj.anyOf)) {
messages.addMessage(
path.concat(['anyOf']),
'anyOf value should be an array',
'error'
);
} else if (obj.allOf && Array.isArray(obj.allOf)) {
obj.allOf.forEach((e, i) =>
pushLeafSchemas(e, [...path, 'allOf', i])
);
Expand Down
22 changes: 22 additions & 0 deletions test/cli-validator/mockFiles/oas3/complexTestComposeModel.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
openapi: 3.0.0
components:
schemas:
A:
description: a schema
type: string
format: byte
complexOneOfError:
description: second oneOf should be array
oneOf:
- oneOf:
$ref: '#/components/schemas/A'
complexAllOfError:
description: allOf should be array
oneOf:
- allOf:
type: string
complexAnyOfError:
description: anyOf should be array
oneOf:
- anyOf:
$ref: '#/components/schemas/A'
21 changes: 21 additions & 0 deletions test/cli-validator/mockFiles/oas3/composeModelItems.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
openapi: 3.0.0
components:
schemas:
one_of_array:
type: array
description: a oneOf array schema
items:
oneOf:
type: string
all_of_array:
type: array
description: an allOf array schema
items:
allOf:
type: string
any_of_array:
type: array
description: an anyOf array schema
items:
anyOf:
type: string
22 changes: 22 additions & 0 deletions test/cli-validator/mockFiles/oas3/composeModelProps.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
openapi: 3.0.0
components:
schemas:
object1:
type: object
description: an object schema
properties:
one_of_error_prop:
description: oneOf should be array
schema:
oneOf:
type: string
all_of_error_prop:
description: allOf should be array
schema:
allOf:
type: string
any_of_error_prop:
description: anyOf should be array
schema:
anyOf:
type: string
22 changes: 22 additions & 0 deletions test/cli-validator/mockFiles/oas3/composeModelsUseArray.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
openapi: 3.0.0
components:
schemas:
object1:
type: object
description: an object schema
properties:
one_of_error_prop:
description: oneOf should be array
schema:
oneOf:
- type: string
all_of_error_prop:
description: allOf should be array
schema:
allOf:
- type: string
any_of_error_prop:
description: anyOf should be array
schema:
anyOf:
- type: string
19 changes: 19 additions & 0 deletions test/cli-validator/mockFiles/oas3/testComposeModel.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
openapi: 3.0.0
components:
schemas:
A:
description: a schema
type: string
format: byte
oneOfError:
description: should be array but instead just a ref
oneOf:
$ref: '#/components/schemas/A'
allOfError:
description: should be array but instead just a type
allOf:
type: string
anyOfError:
description: should be array but instead just a ref
anyOf:
$ref: '#/components/schemas/A'
2 changes: 2 additions & 0 deletions test/cli-validator/mockFiles/oas3/testoneof.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ components:
- $ref: '#/components/schemas/A'
- $ref: '#/components/schemas/B'
C:
description: one of two allOf schemas
oneOf:
- allOf:
- type: foo
Expand All @@ -29,6 +30,7 @@ components:
format: url
description: 'url string'
Ok:
description: ok object
type: object
properties:
ok:
Expand Down
Loading

0 comments on commit 1db6b27

Please sign in to comment.