Skip to content

Commit

Permalink
fix(core): add missing properties in subschemas to fix TS error (#1115)
Browse files Browse the repository at this point in the history
* fix(core): add missing properties in subschemas to fix TS error

* chore: updated samples

Signed-off-by: Olzhas Alexandrov <[email protected]>

---------

Signed-off-by: Olzhas Alexandrov <[email protected]>
  • Loading branch information
o-alexandrov authored Dec 19, 2023
1 parent 7580f55 commit b889c88
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
40 changes: 38 additions & 2 deletions packages/core/src/getters/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ type CombinedData = {
isEnum: boolean[];
types: string[];
hasReadonlyProps: boolean;
/**
* List of all properties in all subschemas
* - used to add missing properties in subschemas to avoid TS error described in @see https://github.com/anymaniax/orval/issues/935
*/
allProperties: string[];
};

type Separator = 'allOf' | 'anyOf' | 'oneOf';
Expand Down Expand Up @@ -48,13 +53,37 @@ const combineValues = ({
}`;
}

let values = resolvedData.values;
const hasObjectSubschemas = resolvedData.allProperties.length;
if (hasObjectSubschemas) {
values = []; // the list of values will be rebuilt to add missing properties (if exist) in subschemas
for (let i = 0; i < resolvedData.values.length; i += 1) {
const subSchema = resolvedData.originalSchema[i];
if (subSchema?.type !== 'object') {
values.push(resolvedData.values[i]);
continue;
}

const missingProperties = resolvedData.allProperties.filter(
(p) => !Object.keys(subSchema.properties!).includes(p),
);
values.push(
`${resolvedData.values[i]}${
missingProperties.length
? ` & {${missingProperties.map((p) => `${p}?: never`).join('; ')}}`
: ''
}`,
);
}
}

if (resolvedValue) {
return `(${resolvedData.values.join(` & ${resolvedValue.value}) | (`)} & ${
return `(${values.join(` & ${resolvedValue.value}) | (`)} & ${
resolvedValue.value
})`;
}

return resolvedData.values.join(' | ');
return values.join(' | ');
};

export const combineSchemas = ({
Expand Down Expand Up @@ -95,6 +124,12 @@ export const combineSchemas = ({
acc.originalSchema.push(resolvedValue.originalSchema);
acc.hasReadonlyProps ||= resolvedValue.hasReadonlyProps;

if (resolvedValue.type === 'object') {
acc.allProperties.push(
...Object.keys(resolvedValue.originalSchema.properties!),
);
}

return acc;
},
{
Expand All @@ -105,6 +140,7 @@ export const combineSchemas = ({
isRef: [],
types: [],
originalSchema: [],
allProperties: [],
hasReadonlyProps: false,
example: schema.example,
examples: resolveExampleRefs(schema.examples, context),
Expand Down
4 changes: 2 additions & 2 deletions samples/react-query/basic/src/api/model/dog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import type { Dachshund } from './dachshund';
import type { DogType } from './dogType';

export type Dog =
| (Labradoodle & {
| (Labradoodle & { length?: never } & {
barksPerMinute?: number;
type: DogType;
})
| (Dachshund & {
| (Dachshund & { cuteness?: never } & {
barksPerMinute?: number;
type: DogType;
});
4 changes: 2 additions & 2 deletions samples/react-query/basic/src/api/model/pet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { PetCallingCode } from './petCallingCode';
import type { PetCountry } from './petCountry';

export type Pet =
| (Dog & {
| (Dog & { petsRequested?: never } & {
'@id'?: string;
callingCode?: PetCallingCode;
country?: PetCountry;
Expand All @@ -19,7 +19,7 @@ export type Pet =
name: string;
tag?: string;
})
| (Cat & {
| (Cat & { barksPerMinute?: never } & {
'@id'?: string;
callingCode?: PetCallingCode;
country?: PetCountry;
Expand Down

0 comments on commit b889c88

Please sign in to comment.