Merge pull request #1141 from markbrockhoff/add-model-postfix

Add model postfix
This commit is contained in:
Ferdi Koomen 2022-10-28 10:37:41 +02:00 committed by GitHub
commit 2a26cb7fe4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 107 additions and 140 deletions

View File

@ -48,7 +48,8 @@ $ openapi --help
--exportModels <value> Write models to disk (default: true)
--exportSchemas <value> Write schemas to disk (default: false)
--indent <value> Indentation options [4, 2, tab] (default: "4")
--postfix <value> Service name postfix (default: "Service")
--postfixServices Service name postfix (default: "Service")
--postfixModels Model name postfix
--request <value> Path to custom request file
-h, --help display help for command

View File

@ -21,7 +21,9 @@ const params = program
.option('--exportModels <value>', 'Write models to disk', true)
.option('--exportSchemas <value>', 'Write schemas to disk', false)
.option('--indent <value>', 'Indentation options [4, 2, tabs]', '4')
.option('--postfix <value>', 'Service name postfix', 'Service')
.option('--postfix <value>', 'Deprecated: Use --postfixServices instead. Service name postfix', 'Service')
.option('--postfixServices <value>', 'Service name postfix', 'Service')
.option('--postfixModels <value>', 'Model name postfix')
.option('--request <value>', 'Path to custom request file')
.parse(process.argv)
.opts();
@ -41,7 +43,8 @@ if (OpenAPI) {
exportModels: JSON.parse(params.exportModels) === true,
exportSchemas: JSON.parse(params.exportSchemas) === true,
indent: params.indent,
postfix: params.postfix,
postfixServices: params.postfixServices ?? params.postfix,
postfixModels: params.postfixModels,
request: params.request,
})
.then(() => {

View File

@ -34,8 +34,10 @@ describe('bin', () => {
'true',
'--indent',
'4',
'--postfix',
'--postfixServices',
'Service',
'--postfixModels',
'Dto',
]);
expect(result.stdout.toString()).toBe('');
expect(result.stderr.toString()).toBe('');
@ -67,4 +69,18 @@ describe('bin', () => {
expect(result.stdout.toString()).toContain(`-o, --output <value>`);
expect(result.stderr.toString()).toBe('');
});
it('should still support the deprecated --postfix parameter', () => {
const result = crossSpawn.sync('node', [
'./bin/index.js',
'--input',
'./test/spec/v3.json',
'--output',
'./test/generated/bin',
'--postfix',
'Service',
]);
expect(result.stdout.toString()).toBe('');
expect(result.stderr.toString()).toBe('');
});
});

View File

@ -18,7 +18,8 @@ $ openapi --help
--exportModels <value> Write models to disk (default: true)
--exportSchemas <value> Write schemas to disk (default: false)
--indent <value> Indentation options [4, 2, tab] (default: "4")
--postfix <value> Service name postfix (default: "Service")
--postfixServices Service name postfix (default: "Service")
--postfixModels Model name postfix
--request <value> Path to custom request file
-h, --help display help for command

View File

@ -24,7 +24,8 @@ export type Options = {
exportModels?: boolean;
exportSchemas?: boolean;
indent?: Indent;
postfix?: string;
postfixServices?: string;
postfixModels?: string;
request?: string;
write?: boolean;
};
@ -44,7 +45,8 @@ export type Options = {
* @param exportModels Generate models
* @param exportSchemas Generate schemas
* @param indent Indentation options (4, 2 or tab)
* @param postfix Service name postfix
* @param postfixServices Service name postfix
* @param postfixModels Model name postfix
* @param request Path to custom request file
* @param write Write the files to disk (true or false)
*/
@ -60,7 +62,8 @@ export const generate = async ({
exportModels = true,
exportSchemas = false,
indent = Indent.SPACE_4,
postfix = 'Service',
postfixServices = 'Service',
postfixModels = '',
request,
write = true,
}: Options): Promise<void> => {
@ -89,7 +92,8 @@ export const generate = async ({
exportModels,
exportSchemas,
indent,
postfix,
postfixServices,
postfixModels,
clientName,
request
);
@ -112,7 +116,8 @@ export const generate = async ({
exportModels,
exportSchemas,
indent,
postfix,
postfixServices,
postfixModels,
clientName,
request
);

View File

@ -18,13 +18,13 @@ export type { OpenAPIConfig } from './core/OpenAPI';
{{#each models}}
{{#if @root.useUnionTypes}}
export type { {{{name}}} } from './models/{{{name}}}';
export type { {{{name}}}{{#if @root.postfixModels}} as {{{name}}}{{{@root.postfixModels}}}{{/if}} } from './models/{{{name}}}';
{{else if enum}}
export { {{{name}}} } from './models/{{{name}}}';
export { {{{name}}}{{#if @root.postfixModels}} as {{{name}}}{{{@root.postfixModels}}}{{/if}} } from './models/{{{name}}}';
{{else if enums}}
export { {{{name}}} } from './models/{{{name}}}';
export { {{{name}}}{{#if @root.postfixModels}} as {{{name}}}{{{@root.postfixModels}}}{{/if}} } from './models/{{{name}}}';
{{else}}
export type { {{{name}}} } from './models/{{{name}}}';
export type { {{{name}}}{{#if @root.postfixModels}} as {{{name}}}{{{@root.postfixModels}}}{{/if}} } from './models/{{{name}}}';
{{/if}}
{{/each}}
{{/if}}
@ -41,7 +41,7 @@ export { ${{{name}}} } from './schemas/${{{name}}}';
{{#if services}}
{{#each services}}
export { {{{name}}}{{{@root.postfix}}} } from './services/{{{name}}}{{{@root.postfix}}}';
export { {{{name}}}{{{@root.postfixServices}}} } from './services/{{{name}}}{{{@root.postfixServices}}}';
{{/each}}
{{/if}}
{{/if}}

View File

@ -28,7 +28,8 @@ import { writeClientServices } from './writeClientServices';
* @param exportSchemas Generate schemas
* @param exportSchemas Generate schemas
* @param indent Indentation options (4, 2 or tab)
* @param postfix Service name postfix
* @param postfixServices Service name postfix
* @param postfixModels Model name postfix
* @param clientName Custom client class name
* @param request Path to custom request file
*/
@ -44,7 +45,8 @@ export const writeClient = async (
exportModels: boolean,
exportSchemas: boolean,
indent: Indent,
postfix: string,
postfixServices: string,
postfixModels: string,
clientName?: string,
request?: string
): Promise<void> => {
@ -75,7 +77,7 @@ export const writeClient = async (
useUnionTypes,
useOptions,
indent,
postfix,
postfixServices,
clientName
);
}
@ -94,7 +96,7 @@ export const writeClient = async (
if (isDefined(clientName)) {
await mkdir(outputPath);
await writeClientClass(client, templates, outputPath, httpClient, clientName, indent, postfix);
await writeClientClass(client, templates, outputPath, httpClient, clientName, indent, postfixServices);
}
if (exportCore || exportServices || exportSchemas || exportModels) {
@ -108,7 +110,8 @@ export const writeClient = async (
exportServices,
exportModels,
exportSchemas,
postfix,
postfixServices,
postfixModels,
clientName
);
}

View File

@ -34,7 +34,7 @@ describe('writeClientIndex', () => {
},
};
await writeClientIndex(client, templates, '/', true, true, true, true, true, 'Service');
await writeClientIndex(client, templates, '/', true, true, true, true, true, 'Service', '');
expect(writeFile).toBeCalledWith('/index.ts', 'index');
});

View File

@ -19,7 +19,8 @@ import { sortServicesByName } from './sortServicesByName';
* @param exportServices Generate services
* @param exportModels Generate models
* @param exportSchemas Generate schemas
* @param postfix Service name postfix
* @param postfixServices Service name postfix
* @param postfixModels Model name postfix
* @param clientName Custom client class name
*/
export const writeClientIndex = async (
@ -31,7 +32,8 @@ export const writeClientIndex = async (
exportServices: boolean,
exportModels: boolean,
exportSchemas: boolean,
postfix: string,
postfixServices: string,
postfixModels: string,
clientName?: string
): Promise<void> => {
const templateResult = templates.index({
@ -40,7 +42,8 @@ export const writeClientIndex = async (
exportModels,
exportSchemas,
useUnionTypes,
postfix,
postfixServices,
postfixModels,
clientName,
server: client.server,
version: client.version,

View File

@ -549,53 +549,53 @@ export { CancelablePromise, CancelError } from './core/CancelablePromise';
export { OpenAPI } from './core/OpenAPI';
export type { OpenAPIConfig } from './core/OpenAPI';
export type { ArrayWithArray } from './models/ArrayWithArray';
export type { ArrayWithBooleans } from './models/ArrayWithBooleans';
export type { ArrayWithNumbers } from './models/ArrayWithNumbers';
export type { ArrayWithProperties } from './models/ArrayWithProperties';
export type { ArrayWithReferences } from './models/ArrayWithReferences';
export type { ArrayWithStrings } from './models/ArrayWithStrings';
export type { CommentWithBackticks } from './models/CommentWithBackticks';
export type { CommentWithBreaks } from './models/CommentWithBreaks';
export type { CommentWithExpressionPlaceholders } from './models/CommentWithExpressionPlaceholders';
export type { CommentWithQuotes } from './models/CommentWithQuotes';
export type { CommentWithReservedCharacters } from './models/CommentWithReservedCharacters';
export type { CommentWithSlashes } from './models/CommentWithSlashes';
export type { Date } from './models/Date';
export type { DictionaryWithArray } from './models/DictionaryWithArray';
export type { DictionaryWithDictionary } from './models/DictionaryWithDictionary';
export type { DictionaryWithProperties } from './models/DictionaryWithProperties';
export type { DictionaryWithReference } from './models/DictionaryWithReference';
export type { DictionaryWithString } from './models/DictionaryWithString';
export type { EnumFromDescription } from './models/EnumFromDescription';
export { EnumWithExtensions } from './models/EnumWithExtensions';
export { EnumWithNumbers } from './models/EnumWithNumbers';
export { EnumWithStrings } from './models/EnumWithStrings';
export type { ModelThatExtends } from './models/ModelThatExtends';
export type { ModelThatExtendsExtends } from './models/ModelThatExtendsExtends';
export type { ModelWithArray } from './models/ModelWithArray';
export type { ModelWithBoolean } from './models/ModelWithBoolean';
export type { ModelWithCircularReference } from './models/ModelWithCircularReference';
export type { ModelWithDictionary } from './models/ModelWithDictionary';
export type { ModelWithDuplicateImports } from './models/ModelWithDuplicateImports';
export type { ModelWithDuplicateProperties } from './models/ModelWithDuplicateProperties';
export { ModelWithEnum } from './models/ModelWithEnum';
export type { ModelWithEnumFromDescription } from './models/ModelWithEnumFromDescription';
export type { ModelWithInteger } from './models/ModelWithInteger';
export type { ModelWithNestedEnums } from './models/ModelWithNestedEnums';
export type { ModelWithNestedProperties } from './models/ModelWithNestedProperties';
export type { ModelWithNullableString } from './models/ModelWithNullableString';
export type { ModelWithOrderedProperties } from './models/ModelWithOrderedProperties';
export type { ModelWithPattern } from './models/ModelWithPattern';
export type { ModelWithProperties } from './models/ModelWithProperties';
export type { ModelWithReference } from './models/ModelWithReference';
export type { ModelWithString } from './models/ModelWithString';
export type { SimpleBoolean } from './models/SimpleBoolean';
export type { SimpleFile } from './models/SimpleFile';
export type { SimpleInteger } from './models/SimpleInteger';
export type { SimpleReference } from './models/SimpleReference';
export type { SimpleString } from './models/SimpleString';
export type { SimpleStringWithPattern } from './models/SimpleStringWithPattern';
export type { ArrayWithArray as ArrayWithArrayDto } from './models/ArrayWithArray';
export type { ArrayWithBooleans as ArrayWithBooleansDto } from './models/ArrayWithBooleans';
export type { ArrayWithNumbers as ArrayWithNumbersDto } from './models/ArrayWithNumbers';
export type { ArrayWithProperties as ArrayWithPropertiesDto } from './models/ArrayWithProperties';
export type { ArrayWithReferences as ArrayWithReferencesDto } from './models/ArrayWithReferences';
export type { ArrayWithStrings as ArrayWithStringsDto } from './models/ArrayWithStrings';
export type { CommentWithBackticks as CommentWithBackticksDto } from './models/CommentWithBackticks';
export type { CommentWithBreaks as CommentWithBreaksDto } from './models/CommentWithBreaks';
export type { CommentWithExpressionPlaceholders as CommentWithExpressionPlaceholdersDto } from './models/CommentWithExpressionPlaceholders';
export type { CommentWithQuotes as CommentWithQuotesDto } from './models/CommentWithQuotes';
export type { CommentWithReservedCharacters as CommentWithReservedCharactersDto } from './models/CommentWithReservedCharacters';
export type { CommentWithSlashes as CommentWithSlashesDto } from './models/CommentWithSlashes';
export type { Date as DateDto } from './models/Date';
export type { DictionaryWithArray as DictionaryWithArrayDto } from './models/DictionaryWithArray';
export type { DictionaryWithDictionary as DictionaryWithDictionaryDto } from './models/DictionaryWithDictionary';
export type { DictionaryWithProperties as DictionaryWithPropertiesDto } from './models/DictionaryWithProperties';
export type { DictionaryWithReference as DictionaryWithReferenceDto } from './models/DictionaryWithReference';
export type { DictionaryWithString as DictionaryWithStringDto } from './models/DictionaryWithString';
export type { EnumFromDescription as EnumFromDescriptionDto } from './models/EnumFromDescription';
export { EnumWithExtensions as EnumWithExtensionsDto } from './models/EnumWithExtensions';
export { EnumWithNumbers as EnumWithNumbersDto } from './models/EnumWithNumbers';
export { EnumWithStrings as EnumWithStringsDto } from './models/EnumWithStrings';
export type { ModelThatExtends as ModelThatExtendsDto } from './models/ModelThatExtends';
export type { ModelThatExtendsExtends as ModelThatExtendsExtendsDto } from './models/ModelThatExtendsExtends';
export type { ModelWithArray as ModelWithArrayDto } from './models/ModelWithArray';
export type { ModelWithBoolean as ModelWithBooleanDto } from './models/ModelWithBoolean';
export type { ModelWithCircularReference as ModelWithCircularReferenceDto } from './models/ModelWithCircularReference';
export type { ModelWithDictionary as ModelWithDictionaryDto } from './models/ModelWithDictionary';
export type { ModelWithDuplicateImports as ModelWithDuplicateImportsDto } from './models/ModelWithDuplicateImports';
export type { ModelWithDuplicateProperties as ModelWithDuplicatePropertiesDto } from './models/ModelWithDuplicateProperties';
export { ModelWithEnum as ModelWithEnumDto } from './models/ModelWithEnum';
export type { ModelWithEnumFromDescription as ModelWithEnumFromDescriptionDto } from './models/ModelWithEnumFromDescription';
export type { ModelWithInteger as ModelWithIntegerDto } from './models/ModelWithInteger';
export type { ModelWithNestedEnums as ModelWithNestedEnumsDto } from './models/ModelWithNestedEnums';
export type { ModelWithNestedProperties as ModelWithNestedPropertiesDto } from './models/ModelWithNestedProperties';
export type { ModelWithNullableString as ModelWithNullableStringDto } from './models/ModelWithNullableString';
export type { ModelWithOrderedProperties as ModelWithOrderedPropertiesDto } from './models/ModelWithOrderedProperties';
export type { ModelWithPattern as ModelWithPatternDto } from './models/ModelWithPattern';
export type { ModelWithProperties as ModelWithPropertiesDto } from './models/ModelWithProperties';
export type { ModelWithReference as ModelWithReferenceDto } from './models/ModelWithReference';
export type { ModelWithString as ModelWithStringDto } from './models/ModelWithString';
export type { SimpleBoolean as SimpleBooleanDto } from './models/SimpleBoolean';
export type { SimpleFile as SimpleFileDto } from './models/SimpleFile';
export type { SimpleInteger as SimpleIntegerDto } from './models/SimpleInteger';
export type { SimpleReference as SimpleReferenceDto } from './models/SimpleReference';
export type { SimpleString as SimpleStringDto } from './models/SimpleString';
export type { SimpleStringWithPattern as SimpleStringWithPatternDto } from './models/SimpleStringWithPattern';
export { $ArrayWithArray } from './schemas/$ArrayWithArray';
export { $ArrayWithBooleans } from './schemas/$ArrayWithBooleans';
@ -3614,74 +3614,6 @@ export { CancelablePromise, CancelError } from './core/CancelablePromise';
export { OpenAPI } from './core/OpenAPI';
export type { OpenAPIConfig } from './core/OpenAPI';
export type { ArrayWithArray } from './models/ArrayWithArray';
export type { ArrayWithBooleans } from './models/ArrayWithBooleans';
export type { ArrayWithNumbers } from './models/ArrayWithNumbers';
export type { ArrayWithProperties } from './models/ArrayWithProperties';
export type { ArrayWithReferences } from './models/ArrayWithReferences';
export type { ArrayWithStrings } from './models/ArrayWithStrings';
export type { CommentWithBackticks } from './models/CommentWithBackticks';
export type { CommentWithBreaks } from './models/CommentWithBreaks';
export type { CommentWithExpressionPlaceholders } from './models/CommentWithExpressionPlaceholders';
export type { CommentWithQuotes } from './models/CommentWithQuotes';
export type { CommentWithReservedCharacters } from './models/CommentWithReservedCharacters';
export type { CommentWithSlashes } from './models/CommentWithSlashes';
export type { CompositionBaseModel } from './models/CompositionBaseModel';
export type { CompositionExtendedModel } from './models/CompositionExtendedModel';
export type { CompositionWithAllOfAndNullable } from './models/CompositionWithAllOfAndNullable';
export type { CompositionWithAnyOf } from './models/CompositionWithAnyOf';
export type { CompositionWithAnyOfAndNullable } from './models/CompositionWithAnyOfAndNullable';
export type { CompositionWithAnyOfAnonymous } from './models/CompositionWithAnyOfAnonymous';
export type { CompositionWithOneOf } from './models/CompositionWithOneOf';
export type { CompositionWithOneOfAndComplexArrayDictionary } from './models/CompositionWithOneOfAndComplexArrayDictionary';
export type { CompositionWithOneOfAndNullable } from './models/CompositionWithOneOfAndNullable';
export type { CompositionWithOneOfAndSimpleArrayDictionary } from './models/CompositionWithOneOfAndSimpleArrayDictionary';
export type { CompositionWithOneOfAndSimpleDictionary } from './models/CompositionWithOneOfAndSimpleDictionary';
export type { CompositionWithOneOfAnonymous } from './models/CompositionWithOneOfAnonymous';
export type { CompositionWithOneOfDiscriminator } from './models/CompositionWithOneOfDiscriminator';
export type { DeprecatedModel } from './models/DeprecatedModel';
export type { DictionaryWithArray } from './models/DictionaryWithArray';
export type { DictionaryWithDictionary } from './models/DictionaryWithDictionary';
export type { DictionaryWithProperties } from './models/DictionaryWithProperties';
export type { DictionaryWithReference } from './models/DictionaryWithReference';
export type { DictionaryWithString } from './models/DictionaryWithString';
export type { EnumFromDescription } from './models/EnumFromDescription';
export { EnumWithExtensions } from './models/EnumWithExtensions';
export { EnumWithNumbers } from './models/EnumWithNumbers';
export { EnumWithStrings } from './models/EnumWithStrings';
export type { File } from './models/File';
export type { FreeFormObjectWithAdditionalPropertiesEqEmptyObject } from './models/FreeFormObjectWithAdditionalPropertiesEqEmptyObject';
export type { FreeFormObjectWithAdditionalPropertiesEqTrue } from './models/FreeFormObjectWithAdditionalPropertiesEqTrue';
export type { FreeFormObjectWithoutAdditionalProperties } from './models/FreeFormObjectWithoutAdditionalProperties';
export type { ModelCircle } from './models/ModelCircle';
export type { ModelSquare } from './models/ModelSquare';
export type { ModelThatExtends } from './models/ModelThatExtends';
export type { ModelThatExtendsExtends } from './models/ModelThatExtendsExtends';
export type { ModelWithArray } from './models/ModelWithArray';
export type { ModelWithBoolean } from './models/ModelWithBoolean';
export type { ModelWithCircularReference } from './models/ModelWithCircularReference';
export type { ModelWithDictionary } from './models/ModelWithDictionary';
export type { ModelWithDuplicateImports } from './models/ModelWithDuplicateImports';
export type { ModelWithDuplicateProperties } from './models/ModelWithDuplicateProperties';
export { ModelWithEnum } from './models/ModelWithEnum';
export type { ModelWithEnumFromDescription } from './models/ModelWithEnumFromDescription';
export type { ModelWithInteger } from './models/ModelWithInteger';
export type { ModelWithNestedEnums } from './models/ModelWithNestedEnums';
export type { ModelWithNestedProperties } from './models/ModelWithNestedProperties';
export type { ModelWithNullableString } from './models/ModelWithNullableString';
export type { ModelWithOrderedProperties } from './models/ModelWithOrderedProperties';
export type { ModelWithPattern } from './models/ModelWithPattern';
export type { ModelWithProperties } from './models/ModelWithProperties';
export type { ModelWithReference } from './models/ModelWithReference';
export type { ModelWithString } from './models/ModelWithString';
export type { Pageable } from './models/Pageable';
export type { SimpleBoolean } from './models/SimpleBoolean';
export type { SimpleFile } from './models/SimpleFile';
export type { SimpleInteger } from './models/SimpleInteger';
export type { SimpleReference } from './models/SimpleReference';
export type { SimpleString } from './models/SimpleString';
export type { SimpleStringWithPattern } from './models/SimpleStringWithPattern';
export { $ArrayWithArray } from './schemas/$ArrayWithArray';
export { $ArrayWithBooleans } from './schemas/$ArrayWithBooleans';
export { $ArrayWithNumbers } from './schemas/$ArrayWithNumbers';

View File

@ -15,6 +15,7 @@ describe('v2', () => {
exportSchemas: true,
exportModels: true,
exportServices: true,
postfixModels: 'Dto',
});
sync('./test/generated/v2/**/*.ts').forEach(file => {
@ -36,6 +37,7 @@ describe('v3', () => {
exportSchemas: true,
exportModels: true,
exportServices: true,
postfixModels: 'Dto',
});
sync('./test/generated/v3/**/*.ts').forEach(file => {

3
types/index.d.ts vendored
View File

@ -24,7 +24,8 @@ export type Options = {
exportModels?: boolean;
exportSchemas?: boolean;
indent?: Indent | '4' | '2' | 'tab';
postfix?: string;
postfixServices?: string;
postfixModels?: string;
request?: string;
write?: boolean;
};