Skip to content

Add normalizer rule for required on allOf #408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/normalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ rules.set('Transform const to singleton enum', schema => {
}
})

rules.set('Spread required on allOf into its subSchema', schema => {
if (schema.allOf && schema.required) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll also want to consider:

  • anyOf and oneOf
  • Nested sub-schemas N layers deep

const schemaRequired = schema.required
schema.allOf.forEach(subSchema => {
subSchema.required = [...(subSchema.required || []), ...schemaRequired]
})
}
})

export function normalize(rootSchema: LinkedJSONSchema, filename: string, options: Options): NormalizedJSONSchema {
rules.forEach(rule => traverse(rootSchema, schema => rule(schema, filename, options)))
return rootSchema as NormalizedJSONSchema
Expand Down
59 changes: 59 additions & 0 deletions test/__snapshots__/test/test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -9021,3 +9021,62 @@ Generated by [AVA](https://avajs.dev).
""␊
]␊
}`

## Spread required on AllOf into its sub-schema

> Snapshot 1

`{␊
"type": "object",␊
"properties": {␊
"reference": {␊
"allOf": [␊
{␊
"$ref": "#/definitions/defined",␊
"required": [␊
"subproperty"␊
]␊
}␊
],␊
"required": [␊
"subproperty"␊
]␊
}␊
},␊
"definitions": {␊
"defined": {␊
"type": "object",␊
"properties": {␊
"subproperty": {␊
"type": "integer"␊
}␊
},␊
"required": [],␊
"additionalProperties": true␊
}␊
},␊
"additionalProperties": false,␊
"required": [],␊
"id": "RequiredOnAllOf"␊
}`

## mergeRequiredFromAllOf.js

> Expected output to match snapshot for e2e test: mergeRequiredFromAllOf.js

`/* tslint:disable */␊
/**␊
* This file was automatically generated by json-schema-to-typescript.␊
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
* and run json-schema-to-typescript to regenerate this file.␊
*/␊
export interface MergeRequiredFromAllOf {␊
reference?: Defined;␊
}␊
export interface Defined {␊
foo: number;␊
bar: string;␊
[k: string]: unknown;␊
}␊
`
Binary file modified test/__snapshots__/test/test.ts.snap
Binary file not shown.
31 changes: 31 additions & 0 deletions test/e2e/mergeRequiredFromAllOf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export const input = {
title: 'MergeRequiredFromAllOf',
type: 'object',
properties: {
reference: {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when there's a second property that also references #/definitions/defined, but doesn't have the required?

allOf: [
{
$ref: '#/definitions/defined'
}
],
required: ['foo']
}
},
definitions: {
defined: {
additionalProperties: true,
type: 'object',
properties: {
foo: {
type: 'integer'
},
bar: {
type: 'string'
}
},
required: ['bar']
}
},
required: [],
additionalProperties: false
}
62 changes: 62 additions & 0 deletions test/normalizer/requiredOnAllOf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"name": "Spread required on AllOf into its sub-schema",
"in": {
"type": "object",
"properties": {
"reference": {
"allOf": [
{
"$ref": "#/definitions/defined"
}
],
"required": [
"subproperty"
]
}
},
"definitions": {
"defined": {
"type": "object",
"properties": {
"subproperty": {
"type": "integer"
}
}
}
},
"additionalProperties": false
},
"out": {
"id": "RequiredOnAllOf",
"type": "object",
"properties": {
"reference": {
"allOf": [
{
"$ref": "#/definitions/defined",
"required": [
"subproperty"
]
}
],
"required": [
"subproperty"
]
}
},
"definitions": {
"defined": {
"additionalProperties": true,
"type": "object",
"properties": {
"subproperty": {
"type": "integer"
}
},
"required": []
}
},
"required": [],
"additionalProperties": false
}
}