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(core): export type with required properties in all-of object #1819

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 9 additions & 12 deletions packages/core/src/getters/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type CombinedData = {
* - used to add missing properties in subschemas to avoid TS error described in @see https://github.com/orval-labs/orval/issues/935
*/
allProperties: string[];
requiredProperties: string[];
};

type Separator = 'allOf' | 'anyOf' | 'oneOf';
Expand All @@ -51,9 +52,13 @@ const combineValues = ({
}

if (separator === 'allOf') {
return `${resolvedData.values.join(` & `)}${
const joined = `${resolvedData.values.join(` & `)}${
resolvedValue ? ` & ${resolvedValue.value}` : ''
}`;
if (resolvedData.requiredProperties.length) {
return `${joined} & Required<Pick<${joined}, '${resolvedData.requiredProperties.join("' | '")}'>>`;
}
return joined;
}

let values = resolvedData.values;
Expand Down Expand Up @@ -113,17 +118,8 @@ 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 };
}
if (separator === 'allOf' && isSchema(subSchema) && subSchema.required) {
acc.requiredProperties.push(...subSchema.required);
}

const resolvedValue = resolveObject({
Expand Down Expand Up @@ -165,6 +161,7 @@ export const combineSchemas = ({
hasReadonlyProps: false,
example: schema.example,
examples: resolveExampleRefs(schema.examples, context),
requiredProperties: separator === 'allOf' ? schema.required ?? [] : [],
} as CombinedData,
);

Expand Down
6 changes: 6 additions & 0 deletions tests/specifications/all-of.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ paths:
- $ref: '#/components/schemas/Pet'
- $ref: '#/components/schemas/PetDetail'
- type: object
properties:
category:
type: string
required:
- category
- type: object
properties:
color:
type: string
required:
- color
/rested-ref-in-all-of-pets:
Expand Down
Loading