Skip to content

feat: added output.overrides.namingConvention param to orval config, … #2125

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

Merged
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
40 changes: 39 additions & 1 deletion docs/src/pages/reference/configuration/output.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ Valid values: `camelCase`, `PascalCase`, `snake_case`, `kebab-case`.

Default Value: `camelCase`.

Specify the naming convention for the generated files.
Specify the naming convention for the generated **files**.

If you're looking for **property keys** naming convention, see [namingConvention](#namingconvention-for-property-keys).

```js
module.exports = {
Expand Down Expand Up @@ -930,6 +932,42 @@ module.exports = {
};
```

#### namingConvention for property keys

Type: `Object`.

Change output naming convention generated **for property keys**.

**By default, preserves keys** naming convention as is.

If you're looking **for file** naming convention, see [namingConvention](#namingconvention).

```ts

module.exports = {
petstore: {
output: {
...
override: {
namingConvention: {
enum: 'PascalCase',
},
},
},
...
},
};
```

##### Enum

Type: `String`.

Changes naming convention for **enum** keys. All generated [enum types](#enumgenerationtype) supported.

Valid values: : `camelCase`, `PascalCase`, `snake_case`, `kebab-case`.
_same as for file_ [namingConvention](#namingconvention).

#### fetch

Type: `Object`.
Expand Down
32 changes: 32 additions & 0 deletions packages/core/src/generators/schema-definition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,36 @@ describe('generateSchemasDefinition', () => {
expect(result[0].name).toBe('TestSchemaSuffix');
expect(result[1].name).toBe('AnotherSchemaSuffix');
});

it('should generate schemas with changed enum nameConvention', () => {
const context: ContextSpecs = {
specKey: 'testSpec',
output: {
override: {
enumGenerationType: 'enum',
namingConvention: {
enum: 'PascalCase',
},
},
},
target: 'typescript',
specs: {},
};

const schemas: SchemasObject = {
TestSchema: {
type: 'object',
properties: {
testedEnum: {
type: 'string',
enum: ['snake_case', 'camelCase'],
},
},
},
};

const result = generateSchemasDefinition(schemas, context, 'Suffix');
expect(result[0].model.includes('SnakeCase')).toBe(true);
expect(result[0].model.includes('CamelCase')).toBe(true);
});
});
1 change: 1 addition & 0 deletions packages/core/src/generators/schema-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export const generateSchemasDefinition = (
getEnumNames(resolvedValue.originalSchema),
context.output.override.enumGenerationType,
getEnumDescriptions(resolvedValue.originalSchema),
context.output.override.namingConvention?.enum,
);
} 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
47 changes: 39 additions & 8 deletions packages/core/src/getters/enum.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { keyword } from 'esutils';
import { SchemaObject } from 'openapi3-ts/dist/model/openapi30';
import { EnumGeneration } from '../types';
import { isNumeric, sanitize } from '../utils';
import { EnumGeneration, NamingConvention } from '../types';
import { conventionName, isNumeric, sanitize } from '../utils';

export const getEnumNames = (schemaObject: SchemaObject | undefined) => {
return (
Expand All @@ -25,11 +25,18 @@ export const getEnum = (
names: string[] | undefined,
enumGenerationType: EnumGeneration,
descriptions?: string[],
enumNamingConvention?: NamingConvention,
) => {
if (enumGenerationType === EnumGeneration.CONST)
return getTypeConstEnum(value, enumName, names, descriptions);
return getTypeConstEnum(
value,
enumName,
names,
descriptions,
enumNamingConvention,
);
if (enumGenerationType === EnumGeneration.ENUM)
return getNativeEnum(value, enumName, names);
return getNativeEnum(value, enumName, names, enumNamingConvention);
if (enumGenerationType === EnumGeneration.UNION)
return getUnion(value, enumName);
throw new Error(`Invalid enumGenerationType: ${enumGenerationType}`);
Expand All @@ -40,6 +47,7 @@ const getTypeConstEnum = (
enumName: string,
names?: string[],
descriptions?: string[],
enumNamingConvention?: NamingConvention,
) => {
let enumValue = `export type ${enumName} = typeof ${enumName}[keyof typeof ${enumName}]`;

Expand All @@ -50,7 +58,12 @@ const getTypeConstEnum = (

enumValue += ';\n';

const implementation = getEnumImplementation(value, names, descriptions);
const implementation = getEnumImplementation(
value,
names,
descriptions,
enumNamingConvention,
);

enumValue += '\n\n';

Expand All @@ -65,6 +78,7 @@ export const getEnumImplementation = (
value: string,
names?: string[],
descriptions?: string[],
enumNamingConvention?: NamingConvention,
) => {
// empty enum or null-only enum
if (value === '') return '';
Expand Down Expand Up @@ -99,6 +113,10 @@ export const getEnumImplementation = (
});
}

if (enumNamingConvention) {
key = conventionName(key, enumNamingConvention);
}

return (
acc +
comment +
Expand All @@ -107,14 +125,23 @@ export const getEnumImplementation = (
}, '');
};

const getNativeEnum = (value: string, enumName: string, names?: string[]) => {
const enumItems = getNativeEnumItems(value, names);
const getNativeEnum = (
value: string,
enumName: string,
names?: string[],
enumNamingConvention?: NamingConvention,
) => {
const enumItems = getNativeEnumItems(value, names, enumNamingConvention);
const enumValue = `export enum ${enumName} {\n${enumItems}\n}`;

return enumValue;
};

const getNativeEnumItems = (value: string, names?: string[]) => {
const getNativeEnumItems = (
value: string,
names?: string[],
enumNamingConvention?: NamingConvention,
) => {
if (value === '') return '';

return [...new Set(value.split(' | '))].reduce((acc, val, index) => {
Expand Down Expand Up @@ -143,6 +170,10 @@ const getNativeEnumItems = (value: string, names?: string[]) => {
});
}

if (enumNamingConvention) {
key = conventionName(key, enumNamingConvention);
}

return (
acc +
` ${keyword.isIdentifierNameES5(key) ? key : `'${key}'`}= ${val},\n`
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/getters/query-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const getQueryParamsTypes = (
getEnumNames(resolvedValue.originalSchema),
context.output.override.enumGenerationType,
getEnumDescriptions(resolvedValue.originalSchema),
context.output.override.namingConvention?.enum,
);

return {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/resolvers/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const resolveObjectOriginal = ({
getEnumNames(resolvedValue.originalSchema),
context.output.override.enumGenerationType,
getEnumDescriptions(resolvedValue.originalSchema),
context.output.override.namingConvention?.enum,
);

return {
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ export type NormalizedOverrideOutput = {
formUrlEncoded: boolean | NormalizedMutator;
paramsSerializer?: NormalizedMutator;
paramsSerializerOptions?: NormalizedParamsSerializerOptions;
namingConvention: {
enum?: NamingConvention;
};
components: {
schemas: {
suffix: string;
Expand Down Expand Up @@ -422,6 +425,9 @@ export type OverrideOutput = {
formUrlEncoded?: boolean | Mutator;
paramsSerializer?: Mutator;
paramsSerializerOptions?: ParamsSerializerOptions;
namingConvention?: {
enum?: NamingConvention;
};
components?: {
schemas?: {
suffix?: string;
Expand Down
1 change: 1 addition & 0 deletions packages/orval/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ export const normalizeOptions = async (
? outputOptions.override?.header!
: getDefaultFilesHeader,
requestOptions: outputOptions.override?.requestOptions ?? true,
namingConvention: outputOptions.override?.namingConvention ?? {},
components: {
schemas: {
suffix: RefComponentSuffix.schemas,
Expand Down