Skip to content

Commit

Permalink
Adds initial test for InlineObjectSchemaValidator (#289)
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Farr <[email protected]>
  • Loading branch information
Xtansia authored May 10, 2024
1 parent de939d2 commit 6cd7ca1
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 3 deletions.
8 changes: 5 additions & 3 deletions tools/linter/InlineObjectSchemaValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ export default class InlineObjectSchemaValidator {
return
}

const this_key = ctx.key
const parent_key = ctx.parent().key
const ancestry = ctx.keys.reverse()

if (parent_key === 'properties' || this_key === 'additionalProperties' || this_key === 'items') {
if (ancestry[1] === 'properties' ||
ancestry[0] === 'additionalProperties' ||
ancestry[0] === 'items' ||
(ancestry[0] === 'schema' && ancestry[2] === 'parameters' && ancestry[3] !== 'components')) {
errors.push(ctx.error('object schemas should be defined out-of-line via a $ref'))
}
}
Expand Down
4 changes: 4 additions & 0 deletions tools/linter/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export class SpecificationContext {
get key (): string {
return this._location[this._location.length - 1]
}

get keys (): string[] {
return [...this._location]
}
}

export type MaybeRef<O extends object> = O | OpenAPIV3.ReferenceObject
Expand Down
32 changes: 32 additions & 0 deletions tools/test/linter/InlineObjectSchemaValidator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import SchemasFolder from '../../linter/components/SchemasFolder'
import NamespacesFolder from '../../linter/components/NamespacesFolder'
import InlineObjectSchemaValidator from '../../linter/InlineObjectSchemaValidator'

test('validate()', () => {
const root_folder = './test/linter/fixtures/inline_object_schema_validator'
const namespaces_folder = new NamespacesFolder(`${root_folder}/namespaces`)
const schemas_folder = new SchemasFolder(`${root_folder}/schemas`)
const validator = new InlineObjectSchemaValidator(namespaces_folder, schemas_folder)
expect(validator.validate()).toEqual([
{
file: 'namespaces/ops.yaml',
location: '#/paths/~1the~1path/post/parameters/3/schema',
message: 'object schemas should be defined out-of-line via a $ref'
},
{
file: 'namespaces/ops.yaml',
location: '#/paths/~1the~1path/post/requestBody/content/application~1json/schema/items',
message: 'object schemas should be defined out-of-line via a $ref'
},
{
file: 'namespaces/ops.yaml',
location: '#/paths/~1the~1path/post/responses/200/content/application~1json/schema/properties/inline_object_as_a_property_is_not_ok',
message: 'object schemas should be defined out-of-line via a $ref'
},
{
file: 'schemas/schemas.yaml',
location: '#/components/schemas/additionalProperties_with_object_value_schema_can_not_be_inline/additionalProperties',
message: 'object schemas should be defined out-of-line via a $ref'
}
])
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
paths:
/the/path:
post:
parameters:
- $ref: '#/components/parameters/query.ref_string_is_ok'
- $ref: '#/components/parameters/query.ref_object_is_ok'
- name: inline_string_is_ok
in: query
schema:
type: string
- name: inline_object_is_not_ok
in: query
schema:
type: object
properties:
the_prop:
type: boolean
requestBody:
content:
application/json:
schema:
type: array
items:
title: inline_object_as_array_items_is_not_ok
type: object
properties:
item_prop:
type: number
responses:
'200':
content:
application/json:
schema:
type: object
properties:
inline_object_as_a_property_is_not_ok:
type: object
properties:
nested_prop:
type: string
components:
parameters:
query.ref_string_is_ok:
name: ref_string_is_ok
in: query
schema:
type: string
query.ref_object_is_ok:
name: ref_object_is_ok
in: query
schema:
type: object
properties:
setting:
type: integer
requestBodies:
obj:
content:
application/json:
schema:
type: object
properties:
prop:
type: number
responses:
obj@200:
content:
application/json:
schema:
type: object
properties:
prop:
type: number
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
components:
schemas:
object_is_ok:
type: object
properties:
prop:
type: string
object_with_only_additionalProperties_can_be_inline:
type: object
additionalProperties:
type: string
additionalProperties_with_object_value_schema_can_not_be_inline:
type: object
additionalProperties:
type: object
properties:
inline_object_with_no_properties_is_ok:
type: object

0 comments on commit 6cd7ca1

Please sign in to comment.