Skip to content
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

fix: required of parent schema not considered in allOf #1570 #1659

Merged
merged 5 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
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
25 changes: 25 additions & 0 deletions tests/configs/react-query.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,31 @@ export default defineConfig({
target: '../specifications/polymorphic.yaml',
},
},
polymorphicRequired: {
output: {
target: '../generated/react-query/polymorphic-required/endpoints.ts',
schemas: '../generated/react-query/polymorphic-required/model',
client: 'react-query',
mock: true,
headers: true,
},
input: {
target: '../specifications/polymorphic-required.yaml',
},
},
polymorphicRequiredNested: {
output: {
target:
'../generated/react-query/polymorphic-required-nested/endpoints.ts',
schemas: '../generated/react-query/polymorphic-required-nested/model',
client: 'react-query',
mock: true,
headers: true,
},
input: {
target: '../specifications/polymorphic-required-nested.yaml',
},
},
namedParameters: {
output: {
target: '../generated/react-query/named-parameters/endpoints.ts',
Expand Down
61 changes: 61 additions & 0 deletions tests/specifications/polymorphic-required-nested.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
openapi: 3.0.1
info:
title: API
version: V1
paths:
/demo:
get:
operationId: getPolymorphicRequiredNestedResponse
responses:
'200':
description: ''
content:
application/json:
schema:
type: object
oneOf:
- $ref: '#/components/schemas/DescendantOne'
- $ref: '#/components/schemas/DescendantTwo'
components:
schemas:
DescendantOne:
required:
- count
type: object
allOf:
- $ref: '#/components/schemas/ParentType'
- type: object
required:
- value
properties:
value:
type: boolean
count:
type: integer
format: int32
otherCount:
type: integer
format: int32
DescendantTwo:
type: object
allOf:
- $ref: '#/components/schemas/ParentType'
- type: object
properties:
value:
type: string
ParentType:
type: object
required:
- key
- type
properties:
key:
type: string
type:
type: string
enum:
- BOOLEAN
- STRING
discriminator:
propertyName: type
59 changes: 59 additions & 0 deletions tests/specifications/polymorphic-required.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
openapi: 3.0.1
Copy link
Member

Choose a reason for hiding this comment

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

could you merge the yaml of these test files?
I don't wanna separeate related tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I merged those two into the existing polymorphic.yaml file now

info:
title: API
version: V1
paths:
/demo:
get:
operationId: getPolymorphicRequiredResponse
responses:
'200':
description: ''
content:
application/json:
schema:
type: object
oneOf:
- $ref: '#/components/schemas/DescendantOne'
- $ref: '#/components/schemas/DescendantTwo'
components:
schemas:
DescendantOne:
required:
- count
type: object
allOf:
- $ref: '#/components/schemas/ParentType'
- type: object
properties:
value:
type: boolean
count:
type: integer
format: int32
otherCount:
type: integer
format: int32
DescendantTwo:
type: object
allOf:
- $ref: '#/components/schemas/ParentType'
- type: object
properties:
value:
type: string
ParentType:
type: object
required:
- key
- type
properties:
key:
type: string
type:
type: string
enum:
- BOOLEAN
- STRING
discriminator:
propertyName: type