Skip to content

Commit b889c88

Browse files
authoredDec 19, 2023
fix(core): add missing properties in subschemas to fix TS error (#1115)
* fix(core): add missing properties in subschemas to fix TS error * chore: updated samples Signed-off-by: Olzhas Alexandrov <9992724+o-alexandrov@users.noreply.github.com> --------- Signed-off-by: Olzhas Alexandrov <9992724+o-alexandrov@users.noreply.github.com>
1 parent 7580f55 commit b889c88

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed
 

‎packages/core/src/getters/combine.ts

+38-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ type CombinedData = {
2121
isEnum: boolean[];
2222
types: string[];
2323
hasReadonlyProps: boolean;
24+
/**
25+
* List of all properties in all subschemas
26+
* - used to add missing properties in subschemas to avoid TS error described in @see https://github.com/anymaniax/orval/issues/935
27+
*/
28+
allProperties: string[];
2429
};
2530

2631
type Separator = 'allOf' | 'anyOf' | 'oneOf';
@@ -48,13 +53,37 @@ const combineValues = ({
4853
}`;
4954
}
5055

56+
let values = resolvedData.values;
57+
const hasObjectSubschemas = resolvedData.allProperties.length;
58+
if (hasObjectSubschemas) {
59+
values = []; // the list of values will be rebuilt to add missing properties (if exist) in subschemas
60+
for (let i = 0; i < resolvedData.values.length; i += 1) {
61+
const subSchema = resolvedData.originalSchema[i];
62+
if (subSchema?.type !== 'object') {
63+
values.push(resolvedData.values[i]);
64+
continue;
65+
}
66+
67+
const missingProperties = resolvedData.allProperties.filter(
68+
(p) => !Object.keys(subSchema.properties!).includes(p),
69+
);
70+
values.push(
71+
`${resolvedData.values[i]}${
72+
missingProperties.length
73+
? ` & {${missingProperties.map((p) => `${p}?: never`).join('; ')}}`
74+
: ''
75+
}`,
76+
);
77+
}
78+
}
79+
5180
if (resolvedValue) {
52-
return `(${resolvedData.values.join(` & ${resolvedValue.value}) | (`)} & ${
81+
return `(${values.join(` & ${resolvedValue.value}) | (`)} & ${
5382
resolvedValue.value
5483
})`;
5584
}
5685

57-
return resolvedData.values.join(' | ');
86+
return values.join(' | ');
5887
};
5988

6089
export const combineSchemas = ({
@@ -95,6 +124,12 @@ export const combineSchemas = ({
95124
acc.originalSchema.push(resolvedValue.originalSchema);
96125
acc.hasReadonlyProps ||= resolvedValue.hasReadonlyProps;
97126

127+
if (resolvedValue.type === 'object') {
128+
acc.allProperties.push(
129+
...Object.keys(resolvedValue.originalSchema.properties!),
130+
);
131+
}
132+
98133
return acc;
99134
},
100135
{
@@ -105,6 +140,7 @@ export const combineSchemas = ({
105140
isRef: [],
106141
types: [],
107142
originalSchema: [],
143+
allProperties: [],
108144
hasReadonlyProps: false,
109145
example: schema.example,
110146
examples: resolveExampleRefs(schema.examples, context),

‎samples/react-query/basic/src/api/model/dog.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import type { Dachshund } from './dachshund';
99
import type { DogType } from './dogType';
1010

1111
export type Dog =
12-
| (Labradoodle & {
12+
| (Labradoodle & { length?: never } & {
1313
barksPerMinute?: number;
1414
type: DogType;
1515
})
16-
| (Dachshund & {
16+
| (Dachshund & { cuteness?: never } & {
1717
barksPerMinute?: number;
1818
type: DogType;
1919
});

‎samples/react-query/basic/src/api/model/pet.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { PetCallingCode } from './petCallingCode';
1010
import type { PetCountry } from './petCountry';
1111

1212
export type Pet =
13-
| (Dog & {
13+
| (Dog & { petsRequested?: never } & {
1414
'@id'?: string;
1515
callingCode?: PetCallingCode;
1616
country?: PetCountry;
@@ -19,7 +19,7 @@ export type Pet =
1919
name: string;
2020
tag?: string;
2121
})
22-
| (Cat & {
22+
| (Cat & { barksPerMinute?: never } & {
2323
'@id'?: string;
2424
callingCode?: PetCallingCode;
2525
country?: PetCountry;

0 commit comments

Comments
 (0)
Please sign in to comment.