Skip to content
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
7 changes: 6 additions & 1 deletion lib/plugin/merge-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export interface PluginOptions {
* Skip auto-annotating controller methods with HTTP status codes (e.g., @HttpCode(201))
*/
skipAutoHttpCode?: boolean;
/**
* Skip add default for properties that do not specify default values.
*/
skipDefaultValues?: boolean;
}

const defaultOptions: PluginOptions = {
Expand All @@ -30,7 +34,8 @@ const defaultOptions: PluginOptions = {
introspectComments: false,
esmCompatible: false,
readonly: false,
debug: false
debug: false,
skipDefaultValues: false
};

export const mergePluginOptions = (
Expand Down
6 changes: 6 additions & 0 deletions lib/plugin/visitors/model-class.visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,12 @@ export class ModelClassVisitor extends AbstractFileVisitor {
options: PluginOptions
) {
const key = 'default';
/**
* Skip add default for properties that do not specify default values.
*/
if (options.skipDefaultValues) {
return undefined;
}
if (hasPropertyKey(key, existingProperties)) {
return undefined;
}
Expand Down
6 changes: 4 additions & 2 deletions test/plugin/fixtures/create-option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export const mergedCliPluginMultiOption = {
introspectComments: true,
esmCompatible: false,
readonly: false,
debug: false
debug: false,
skipDefaultValues: false
};

export const mergedCliPluginSingleOption = {
Expand All @@ -31,5 +32,6 @@ export const mergedCliPluginSingleOption = {
introspectComments: true,
esmCompatible: false,
readonly: false,
debug: false
debug: false,
skipDefaultValues: false
};
57 changes: 56 additions & 1 deletion test/plugin/model-class-visitor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ describe('API model properties', () => {
]
}
});
expect(esmResult.outputText).toEqual(parameterPropertyDtoTextTranspiled(true));
expect(esmResult.outputText).toEqual(
parameterPropertyDtoTextTranspiled(true)
);
});

it('should ignore Exclude decorator', () => {
Expand Down Expand Up @@ -442,4 +444,57 @@ describe('API model properties', () => {
});
expect(result.outputText).toEqual(createCatExclusiveDtoTextTranspiled);
});

it('should skip default property when skipDefaultValues is true', () => {
const options: ts.CompilerOptions = {
module: ts.ModuleKind.ES2020,
target: ts.ScriptTarget.ES2020,
newLine: ts.NewLineKind.LineFeed,
noEmitHelpers: true,
experimentalDecorators: true,
strict: true
};
const filename = 'default-value.dto.ts';
const fakeProgram = ts.createProgram([filename], options);

const defaultValueDtoText = `
import { ApiProperty } from '../../decorators';
export class DefaultValueDto {
@ApiProperty()
count: number = 5;
}
`;

const resultWithoutSkip = ts.transpileModule(defaultValueDtoText, {
compilerOptions: options,
fileName: filename,
transformers: {
before: [
before(
{ introspectComments: true, classValidatorShim: true },
fakeProgram
)
]
}
});
expect(resultWithoutSkip.outputText).toContain(`default: 5`);

const resultWithSkip = ts.transpileModule(defaultValueDtoText, {
compilerOptions: options,
fileName: filename,
transformers: {
before: [
before(
{
introspectComments: true,
classValidatorShim: true,
skipDefaultValues: true
},
fakeProgram
)
]
}
});
expect(resultWithSkip.outputText).not.toContain(`default: 5`);
});
});