Skip to content

Commit

Permalink
fix: required of parent schema not considered in allOf #1570 (#1659)
Browse files Browse the repository at this point in the history
* fix: required not considered in allOf #1570

* chore: format files

* fix: required not considered in allOf in mocks

* fix: handle required in both parent and sub schema

* test: merge test specs into existing test spec
  • Loading branch information
MGabr authored Oct 14, 2024
1 parent 8736381 commit 86b63eb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/core/src/getters/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ScalarValue,
SchemaType,
} from '../types';
import { getNumberWord, pascal } from '../utils';
import { getNumberWord, pascal, isSchema } from '../utils';
import { getEnumImplementation } from './enum';
import { getScalar } from './scalar';
import uniq from 'lodash.uniq';
Expand Down Expand Up @@ -113,6 +113,19 @@ export const combineSchemas = ({
propName = propName + pascal(getNumberWord(acc.schemas.length + 1));
}

// the required fields in this schema need to be considered
// in the sub schema under the allOf key
if (separator === 'allOf' && schema.required) {
if (isSchema(subSchema) && subSchema.required) {
subSchema = {
...subSchema,
required: [...schema.required, ...subSchema.required],
};
} else {
subSchema = { ...subSchema, required: schema.required };
}
}

const resolvedValue = resolveObject({
schema: subSchema,
propName,
Expand Down
11 changes: 11 additions & 0 deletions packages/mock/src/faker/getters/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ContextSpecs,
GeneratorImport,
isReference,
isSchema,
MockOptions,
} from '@orval/core';
import omit from 'lodash.omit';
Expand Down Expand Up @@ -77,6 +78,16 @@ export const combineSchemasMock = ({
return acc;
}

// the required fields in this schema need to be considered
// in the sub schema under the allOf key
if (separator === 'allOf' && item.required) {
if (isSchema(val) && val.required) {
val = { ...val, required: [...item.required, ...val.required] };
} else {
val = { ...val, required: item.required };
}
}

const resolvedValue = resolveMockValue({
schema: {
...val,
Expand Down
14 changes: 14 additions & 0 deletions tests/specifications/polymorphic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,26 @@ paths:
components:
schemas:
DescendantOne:
required:
- value
type: object
allOf:
- $ref: '#/components/schemas/ParentType'
- type: object
required:
- count
properties:
value:
type: boolean
count:
type: integer
format: int32
otherCount:
type: integer
format: int32
DescendantTwo:
required:
- value
type: object
allOf:
- $ref: '#/components/schemas/ParentType'
Expand All @@ -35,6 +47,8 @@ components:
value:
type: string
ParentType:
required:
- type
type: object
properties:
key:
Expand Down

0 comments on commit 86b63eb

Please sign in to comment.