-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add basic support for parameter properties
The feature is controlled by the `parameterProperties` option, which is disabled by default. As the feature is opt-in, it should be possible to release it as a minor version. Worth noting, that using parameter properties has some limitations compared to the regular properties, but it can still be useful for some basic scenarios. 1. Can't use validation annotations as it is not supported by the class-validator - typestack/class-validator#1669. 2. Can't use JSDoc tags, e.g. `@example` or `@deprecated`. Parsing description from the comment is not implemented in this PR, but can potentially be added from the `@param` JSDoc tag. 3. Can't use `@ApiProperty` and `@ApiHideProperty` decorators. This can be supported by introducing a dedicated decorators which works on parameters, e.g. `@ApiParameterProperty` and `@ApiHideParameterProperty()` with the same signatures. It shouldn't be hard to add, but just wanted to hear if you're interested in adding this feature and collect feedback before I put more work into it. Features which are supported and work the same way as for regular properties: 1. `required` and `nullable` 2. `type` 3. `enum` 4. `default` Fixes #2056
- Loading branch information
Showing
4 changed files
with
159 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
export const parameterPropertyDtoText = ` | ||
export class ParameterPropertyDto { | ||
constructor( | ||
readonly readonlyValue?: string, | ||
private privateValue: string | null, | ||
public publicValue: ItemDto[], | ||
regularParameter: string | ||
protected protectedValue: string = '1234', | ||
) {} | ||
} | ||
export enum LettersEnum { | ||
A = 'A', | ||
B = 'B', | ||
C = 'C' | ||
} | ||
export class ItemDto { | ||
constructor(readonly enumValue: LettersEnum) {} | ||
} | ||
`; | ||
|
||
export const parameterPropertyDtoTextTranspiled = `import * as openapi from "@nestjs/swagger"; | ||
export class ParameterPropertyDto { | ||
constructor(readonlyValue, privateValue, publicValue, regularParameter, protectedValue = '1234') { | ||
this.readonlyValue = readonlyValue; | ||
this.privateValue = privateValue; | ||
this.publicValue = publicValue; | ||
this.protectedValue = protectedValue; | ||
} | ||
static _OPENAPI_METADATA_FACTORY() { | ||
return { readonlyValue: { required: false, type: () => String }, privateValue: { required: true, type: () => String, nullable: true }, publicValue: { required: true, type: () => [require("./parameter-property.dto").ItemDto] }, protectedValue: { required: true, type: () => String, default: "1234" } }; | ||
} | ||
} | ||
export var LettersEnum; | ||
(function (LettersEnum) { | ||
LettersEnum["A"] = "A"; | ||
LettersEnum["B"] = "B"; | ||
LettersEnum["C"] = "C"; | ||
})(LettersEnum || (LettersEnum = {})); | ||
export class ItemDto { | ||
constructor(enumValue) { | ||
this.enumValue = enumValue; | ||
} | ||
static _OPENAPI_METADATA_FACTORY() { | ||
return { enumValue: { required: true, enum: require("./parameter-property.dto").LettersEnum } }; | ||
} | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters