Skip to content

Commit

Permalink
fix: add option for adding missing props to union types, fix duplicat…
Browse files Browse the repository at this point in the history
…e props
  • Loading branch information
Eric Butler committed Jan 22, 2024
1 parent 0af0ead commit 340012c
Show file tree
Hide file tree
Showing 24 changed files with 118 additions and 148 deletions.
2 changes: 1 addition & 1 deletion packages/angular/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ const generateImplementation = (
const isFormData = override?.formData !== false;
const isFormUrlEncoded = override?.formUrlEncoded !== false;
const isExactOptionalPropertyTypes =
!!context.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;
!!context.output.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;
const isBodyVerb = VERBS_WITH_BODY.includes(verb);
const bodyForm = generateFormDataAndUrlEncodedFunction({
formData,
Expand Down
4 changes: 2 additions & 2 deletions packages/axios/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ const generateAxiosImplementation = (
const isFormData = override?.formData !== false;
const isFormUrlEncoded = override?.formUrlEncoded !== false;
const isExactOptionalPropertyTypes =
!!context.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;
!!context.output.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;

const isSyntheticDefaultImportsAllowed = isSyntheticDefaultImportsAllow(
context.tsconfig,
context.output.tsconfig,
);

const bodyForm = generateFormDataAndUrlEncodedFunction({
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/generators/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const generateInterface = ({
model += jsDoc(schema);

if (isEmptyObject) {
if (context.tslint) {
if (context.output.tslint) {
model += '// tslint:disable-next-line:no-empty-interface\n';
} else {
model +=
Expand All @@ -44,7 +44,7 @@ export const generateInterface = ({

if (
!generalJSTypesWithArray.includes(scalar.value) &&
!context?.override?.useTypeOverInterfaces
!context?.output.override?.useTypeOverInterfaces
) {
model += `export interface ${name} ${scalar.value}\n`;
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/generators/schema-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const generateSchemasDefinition = (
resolvedValue.value,
schemaName,
resolvedValue.originalSchema?.['x-enumNames'],
context.override.useNativeEnums,
context.output.override.useNativeEnums,
);
} else if (schemaName === resolvedValue.value && resolvedValue.isRef) {
// Don't add type if schema has same name and the referred schema will be an interface
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/generators/verbs-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ const generateVerbOptions = async ({
name: operationName,
mutator: override?.mutator,
workspace: context.workspace,
tsconfig: context.tsconfig,
tsconfig: context.output.tsconfig,
});

const formData =
Expand All @@ -151,7 +151,7 @@ const generateVerbOptions = async ({
name: operationName,
mutator: override.formData,
workspace: context.workspace,
tsconfig: context.tsconfig,
tsconfig: context.output.tsconfig,
})
: undefined;

Expand All @@ -162,7 +162,7 @@ const generateVerbOptions = async ({
name: operationName,
mutator: override.formUrlEncoded,
workspace: context.workspace,
tsconfig: context.tsconfig,
tsconfig: context.output.tsconfig,
})
: undefined;

Expand All @@ -173,7 +173,7 @@ const generateVerbOptions = async ({
name: 'paramsSerializer',
mutator: override.paramsSerializer as NormalizedMutator,
workspace: context.workspace,
tsconfig: context.tsconfig,
tsconfig: context.output.tsconfig,
})
: undefined;

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/getters/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const getArray = ({
if (schema.items) {
const resolvedObject = resolveObject({
schema: schema.items,
propName: name + context.override.components.schemas.itemSuffix,
propName: name + context.output.override.components.schemas.itemSuffix,
context,
});
return {
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/getters/body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const getBody = ({
contentType?: OverrideOutputContentType;
}): GetterBody => {
const allBodyTypes = getResReqTypes(
[[context.override.components.requestBodies.suffix, requestBody]],
[[context.output.override.components.requestBodies.suffix, requestBody]],
operationName,
context,
);
Expand Down Expand Up @@ -49,7 +49,8 @@ export const getBody = ({
let implementation =
generalJSTypesWithArray.includes(definition.toLowerCase()) ||
filteredBodyTypes.length > 1
? camel(operationName) + context.override.components.requestBodies.suffix
? camel(operationName) +
context.output.override.components.requestBodies.suffix
: camel(definition);

if (implementation) {
Expand Down
18 changes: 14 additions & 4 deletions packages/core/src/getters/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { getNumberWord, pascal } from '../utils';
import { getEnumImplementation } from './enum';
import { getScalar } from './scalar';
import uniq from 'lodash.uniq';

type CombinedData = {
imports: GeneratorImport[];
Expand All @@ -34,10 +35,12 @@ const combineValues = ({
resolvedData,
resolvedValue,
separator,
context,
}: {
resolvedData: CombinedData;
resolvedValue?: ScalarValue;
separator: Separator;
context: ContextSpecs;
}) => {
const isAllEnums = resolvedData.isEnum.every((v) => v);

Expand All @@ -55,7 +58,7 @@ const combineValues = ({

let values = resolvedData.values;
const hasObjectSubschemas = resolvedData.allProperties.length;
if (hasObjectSubschemas) {
if (hasObjectSubschemas && context.output.unionAddMissingProperties) {
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];
Expand All @@ -64,8 +67,10 @@ const combineValues = ({
continue;
}

const missingProperties = resolvedData.allProperties.filter(
(p) => !Object.keys(subSchema.properties!).includes(p),
const missingProperties = uniq(
resolvedData.allProperties.filter(
(p) => !Object.keys(subSchema.properties!).includes(p),
),
);
values.push(
`${resolvedData.values[i]}${
Expand Down Expand Up @@ -155,7 +160,12 @@ export const combineSchemas = ({
resolvedValue = getScalar({ item: omit(schema, separator), name, context });
}

const value = combineValues({ resolvedData, separator, resolvedValue });
const value = combineValues({
resolvedData,
separator,
resolvedValue,
context,
});

if (isAllEnums && name && items.length > 1) {
const newEnum = `\n\n// eslint-disable-next-line @typescript-eslint/no-redeclare\nexport const ${pascal(
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/getters/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const getProps = ({
};

let paramGetterProps: GetterProps;
if (context.override.useNamedParameters && params.length > 0) {
if (context.output.override.useNamedParameters && params.length > 0) {
const parameterTypeName = `${pascal(operationName)}PathParameters`;

const name = 'pathParams';
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/getters/query-params.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { getQueryParams } from './query-params';
// Mock context for getQueryParams
const context: ContextSpecs = {
specs: {},
// @ts-ignore
override: {
useDates: true,
output: {
// @ts-expect-error
override: {
useDates: true,
},
},
};

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/getters/query-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const getQueryParamsTypes = (
resolvedValue.value,
enumName,
resolvedValue.originalSchema?.['x-enumNames'],
context.override.useNativeEnums,
context.output.override.useNativeEnums,
);

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/getters/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const getRefInfo = (
.map((part) => part.replace(regex, '/'));

const suffix = refPaths
? get(context.override, [...refPaths.slice(0, 2), 'suffix'], '')
? get(context.output.override, [...refPaths.slice(0, 2), 'suffix'], '')
: '';

const originalName = ref
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/getters/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const getScalar = ({
case 'number':
case 'integer': {
let value =
item.format === 'int64' && context.override.useBigInt
item.format === 'int64' && context.output.override.useBigInt
? 'bigint'
: 'number';
let isEnum = false;
Expand Down Expand Up @@ -100,7 +100,7 @@ export const getScalar = ({
value = 'Blob';
}

if (context.override.useDates) {
if (context.output.override.useDates) {
if (item.format === 'date' || item.format === 'date-time') {
value = 'Date';
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/resolvers/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const resolveObjectOriginal = ({
resolvedValue.value,
propName,
resolvedValue.originalSchema?.['x-enumNames'],
context.override.useNativeEnums,
context.output.override.useNativeEnums,
);

return {
Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export type NormalizedOutputOptions = {
baseUrl?: string;
allParamsOptional: boolean;
urlEncodeParameters: boolean;
unionAddMissingProperties: boolean;
};

export type NormalizedParamsSerializerOptions = {
Expand Down Expand Up @@ -174,6 +175,7 @@ export type OutputOptions = {
baseUrl?: string;
allParamsOptional?: boolean;
urlEncodeParameters?: boolean;
unionAddMissingProperties?: boolean;
};

export type SwaggerParserOptions = Omit<SwaggerParser.Options, 'validate'> & {
Expand Down Expand Up @@ -426,12 +428,9 @@ export interface ContextSpecs {
specKey: string;
target: string;
workspace: string;
tslint: boolean;
specs: Record<string, OpenAPIObject>;
override: NormalizedOverrideOutput;
tsconfig?: Tsconfig;
packageJson?: PackageJson;
parents?: string[];
output: NormalizedOutputOptions;
}

export interface GlobalOptions {
Expand Down
2 changes: 1 addition & 1 deletion packages/mock/src/faker/getters/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const getMockScalar = ({
}

if (
(context.override?.mock?.useExamples || mockOptions?.useExamples) &&
(context.output.override?.mock?.useExamples || mockOptions?.useExamples) &&
item.example
) {
return {
Expand Down
5 changes: 4 additions & 1 deletion packages/mock/src/msw/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ export const getResponsesMockDefinition = ({
acc,
{ value: definition, originalSchema, example, examples, imports, isRef },
) => {
if (context.override?.mock?.useExamples || mockOptions?.useExamples) {
if (
context.output.override?.mock?.useExamples ||
mockOptions?.useExamples
) {
const exampleValue =
example ||
originalSchema?.example ||
Expand Down
10 changes: 2 additions & 8 deletions packages/orval/src/import-open-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ export const importOpenApi = async ({
target,
workspace,
specs,
override: output.override,
tslint: output.tslint,
tsconfig: output.tsconfig,
packageJson: output.packageJson,
output,
},
});

Expand Down Expand Up @@ -113,10 +110,7 @@ const getApiSchemas = ({
target,
workspace,
specs,
override: output.override,
tslint: output.tslint,
tsconfig: output.tsconfig,
packageJson: output.packageJson,
output,
};

const schemaDefinition = generateSchemasDefinition(
Expand Down
2 changes: 2 additions & 0 deletions packages/orval/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ export const normalizeOptions = async (
headers: outputOptions.headers ?? false,
indexFiles: outputOptions.indexFiles ?? true,
baseUrl: outputOptions.baseUrl,
unionAddMissingProperties:
outputOptions.unionAddMissingProperties ?? false,
override: {
...outputOptions.override,
mock: {
Expand Down
18 changes: 9 additions & 9 deletions packages/query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,10 @@ const generateQueryRequestFunction = (
const hasSignal = !!override.query.signal;

const isSyntheticDefaultImportsAllowed = isSyntheticDefaultImportsAllow(
context.tsconfig,
context.output.tsconfig,
);
const isExactOptionalPropertyTypes =
!!context.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;
!!context.output.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;
const isBodyVerb = VERBS_WITH_BODY.includes(verb);

const bodyForm = generateFormDataAndUrlEncodedFunction({
Expand Down Expand Up @@ -1117,19 +1117,19 @@ const generateQueryHook = async (
const isRequestOptions = override?.requestOptions !== false;
const operationQueryOptions = operations[operationId]?.query;
const isExactOptionalPropertyTypes =
!!context.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;
!!context.output.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;

const hasVueQueryV4 =
OutputClient.VUE_QUERY === outputClient &&
(!isVueQueryV3(context.packageJson) || query.version === 4);
(!isVueQueryV3(context.output.packageJson) || query.version === 4);
const hasSvelteQueryV4 =
OutputClient.SVELTE_QUERY === outputClient &&
(!isSvelteQueryV3(context.packageJson) || query.version === 4);
(!isSvelteQueryV3(context.output.packageJson) || query.version === 4);

const hasQueryV5 =
query.version === 5 ||
isQueryV5(
context.packageJson,
context.output.packageJson,
outputClient as 'react-query' | 'vue-query' | 'svelte-query',
);

Expand All @@ -1156,7 +1156,7 @@ const generateQueryHook = async (
mutator: query.queryKey,
name: `${operationName}QueryKey`,
workspace: context.workspace,
tsconfig: context.tsconfig,
tsconfig: context.output.tsconfig,
})
: undefined;

Expand All @@ -1166,7 +1166,7 @@ const generateQueryHook = async (
mutator: query.queryOptions,
name: `${operationName}QueryOptions`,
workspace: context.workspace,
tsconfig: context.tsconfig,
tsconfig: context.output.tsconfig,
})
: undefined;

Expand Down Expand Up @@ -1308,7 +1308,7 @@ const generateQueryHook = async (
mutator: query.mutationOptions,
name: `${operationName}MutationOptions`,
workspace: context.workspace,
tsconfig: context.tsconfig,
tsconfig: context.output.tsconfig,
})
: undefined;

Expand Down
4 changes: 2 additions & 2 deletions packages/swr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ const generateSwrRequestFunction = (
const isFormData = override?.formData !== false;
const isFormUrlEncoded = override?.formUrlEncoded !== false;
const isExactOptionalPropertyTypes =
!!context.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;
!!context.output.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;
const isBodyVerb = VERBS_WITH_BODY.includes(verb);
const isSyntheticDefaultImportsAllowed = isSyntheticDefaultImportsAllow(
context.tsconfig,
context.output.tsconfig,
);

const bodyForm = generateFormDataAndUrlEncodedFunction({
Expand Down
Loading

0 comments on commit 340012c

Please sign in to comment.