Skip to content
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

Properties declared throught "constructor shorthand" do not appear in Swagger UI #2056

Closed
2 of 4 tasks
tsimbalar opened this issue Aug 29, 2022 · 3 comments
Closed
2 of 4 tasks

Comments

@tsimbalar
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When using the @nestjs/swagger plugin, with classes that use "constructor shorthand" (i.e. properties declared inline with the constructor cf https://dev.to/satansdeer/typescript-constructor-shorthand-3ibd ), in the generated OpenAPI doc, properties declared in constructor are missing.

Minimum reproduction code

https://github.com/tsimbalar/nest-constructor-shorthand-repro

Steps to reproduce

  1. npm install
  2. npm run start:dev
  3. open http://localhost:3000/api

Expected behavior

Under the /example/shorthand endpoint, the example value should contain a property items, like for the /example endpoint.

i.e. currently shorthand looks like this :
image

but we expect it to look like this (the one without shorthand behaves ok):

image

Package version

6.1.0

NestJS version

9.0.0 (but also happened with 8)

Node.js version

16.13.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@tsteuwer-accesso
Copy link

This happens with extending classes as well:


export class Price {
	@IsNumber({
		allowInfinity: false,
		allowNaN: false,
	})
	@ApiProperty({
		example: '52.31',
	})
	total: number;
}

export class CustomerType {
	@IsInt()
	@ApiProperty({
		example: 543,
	})
	id: number;
}

export class PackageData {
	@IsString()
	@ApiProperty({
		description: 'The display name of the package.',
	})
	name: string;

       @ApiProperty({
              description: 'Pricing information'
       })
       price: Price;

        /**
	 * Customer Types
	 */
	@ApiProperty({
		description: 'The customer types of the package.',
                type: CustomerType,
                isArray: true,
	})
	customerTypes: CustomerType[];
}

export class PackageDto extends Entity<number, PackageData, PackageRelationships> {
	constructor(id: number, attributes: PackageData, relationships: PackageRelationships, links: RelationshipLink) {
		super(new EntityData(EntityName.PACKAGE, id, attributes, relationships, links));
	}
}

This produces the same result as above even though the generics are passed in and Entity is a class, not a interface.

{}

@iSeiryu
Copy link

iSeiryu commented Feb 23, 2023

Same on Windows and Ubuntu.
This works but not desired (too much code):

export class Product {
  id: string;
  name: string;

  constructor(
    id: string,
    name: string,
  ) {
    this.id = id;
    this.name = name;
  }
}

Desired

export class Product {
  constructor(
    public id: string,
    public name: string,
  ) { }
}

Replacing public with readonly does not work either.

devoto13 added a commit to devoto13/swagger that referenced this issue Nov 2, 2023
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 nestjs#2056
devoto13 added a commit to devoto13/swagger that referenced this issue Nov 2, 2023
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 nestjs#2056
@kamilmysliwiec
Copy link
Member

#2687

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants