Skip to content

Commit

Permalink
feat: default error for compose models schema that does not use array
Browse files Browse the repository at this point in the history
- added default error for oneOf, anyOf, or allOf schema that is not use top-level array
- added no_array_for_compose_models config option and set its default value to 'error'
- updated schema-ibm test to import the default config values and made necessary changes to test to account for unexpected errors and warnings resulting from having all default values present
- added tests when oneOf, anyOf, or allOf schemas are nested ("oneOf allOf," for example), when oneOf, anyOf, or allOf schema is used as a property in an obect, and when oneOf, anyOf, or allOf schema used as items in array
  • Loading branch information
Barrett Schonefeld committed Feb 19, 2020
1 parent 80b7a45 commit 83d976c
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 160 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ The supported rules are described below:
| no_property_description | Flag any schema that contains a 'property' without a `description` field. | shared |
| description_mentions_json | Flag any schema with a 'property' description that mentions the word 'JSON'. | shared |
| array_of_arrays | Flag any schema with a 'property' of type `array` with items of type `array`. | shared |
| no_array_for_compose_models | Flag any compose model schema that does not use an array. | shared |
| property_case_convention | Flag any property with a `name` that does not follow a given case convention. snake_case_only must be 'off' to use. | shared |
| enum_case_convention | Flag any enum with a `value` that does not follow a given case convention. snake_case_only must be 'off' to use. | shared |
| json_or_param_binary_string | Flag parameters or application/json request/response bodies with schema type: string, format: binary. | oas3 |
Expand Down Expand Up @@ -428,6 +429,7 @@ The default values for each rule are described below.
| no_property_description | warning |
| description_mentions_json | warning |
| array_of_arrays | warning |
| no_array_for_compose_models | error |
| property_case_convention | error, lower_snake_case |
| enum_case_convention | error, lower_snake_case |

Expand Down
1 change: 1 addition & 0 deletions src/.defaultsForValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const defaults = {
'no_property_description': 'warning',
'description_mentions_json': 'warning',
'array_of_arrays': 'warning',
'no_array_for_compose_models': 'error',
'property_case_convention': [ 'error', 'lower_snake_case'],
'enum_case_convention': [ 'error', 'lower_snake_case']
},
Expand Down
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 should be an array',
config.no_array_for_compose_models
);
} else if (obj.oneOf && !Array.isArray(obj.oneOf)) {
messages.addMessage(
path.concat(['oneOf']),
'oneOf should be an array',
config.no_array_for_compose_models
);
} else if (obj.anyOf && !Array.isArray(obj.anyOf)) {
messages.addMessage(
path.concat(['anyOf']),
'anyOf should be an array',
config.no_array_for_compose_models
);
} 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: second 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 83d976c

Please sign in to comment.