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

Use openapi cli plugin to automate swagger api annotation #1590

Open
nitrosx opened this issue Dec 30, 2024 · 1 comment
Open

Use openapi cli plugin to automate swagger api annotation #1590

nitrosx opened this issue Dec 30, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request Important meeting Discuss in meeting before merge needs more discussion

Comments

@nitrosx
Copy link
Contributor

nitrosx commented Dec 30, 2024

Summary

As highlighted by @emigun, maintenance and consistency of the swagger api documentation is expensive and prone to human error as we need to enter the same information in multiple location.

Solution

We propose to adopt the OpenAPI CLI plugin, which reduce information duplication, code cluttering and inconsistencies, and allowing complete freedom in configuration where needed.

##Additional information
Below I included an edited version of the post reported by @emigun in PR #1579

@nitrosx
Copy link
Contributor Author

nitrosx commented Dec 30, 2024

Perhaps we should consider using https://docs.nestjs.com/openapi/cli-plugin instead of annotating manually?
It is already enabled.
I have tested it a bit and it seems to work nicely.

A fairly advanced example:
Let's change

  @ApiProperty({
    type: "array",
    items: { $ref: getSchemaPath(TechniqueClass) },
    required: false,
    default: [],
    description: "Stores the metadata information for techniques.",
  })
  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => CreateTechniqueDto)
  readonly techniques?: TechniqueClass[] = [];

to (remove ApiProperty)

  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => CreateTechniqueDto)
  readonly techniques?: TechniqueClass[] = [];

That would change the openapi json from:

"techniques": {
  "type": "array",
  "items": {
    "$ref": "#/components/schemas/TechniqueClass"
  },
  "default": [],
  "description": "Stores the metadata information for techniques."
},

to

"techniques": {
  "default": [],
  "type": "array",
  "items": {
    "$ref": "#/components/schemas/TechniqueClass"
  }
}

As you can see everything was generated (except description, see below)

I have tried with arrays, dates and ordinary string/number and it seems to work. It also reads the ? for optional fields and correctly identifies the required ones.

We could change nest-cli.json to:

  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "plugins": [{
      "name": "@nestjs/swagger",
      "options": {
        "introspectComments": true
      }
    }]
  }
}

in order to generate the field "description" based on a comment above. Or you could still use

@ApiProperty({
    description: "This is a test description"
  })

if you prefer that. The other fields like type, items, default and others will still be generated with the plugin.

tldr; We could probably get rid of all the ApiProperty decorators and the openapi spec will automatically be generated correctly for the fields. If some doesn't work, we can still use ApiProperty on only those.

Two main benefits:

  1. Less cluttered code
  2. No more inconstencies (as you can see in my merge request I had to change about 20 things just for the dataset dtos)

@nitrosx nitrosx added enhancement New feature or request Important meeting Discuss in meeting before merge needs more discussion labels Dec 30, 2024
emigun added a commit that referenced this issue Jan 22, 2025
## Description
Use openapi plugin in samples module

## Motivation
<!-- Background on use case, changes needed -->

## Fixes
Related to #1590  

## Changes:
* Removed all @ApiProperty decorators in samples module.

## Tests included

- [ ] Included for each change/fix?
- [x] Passing? <!-- Merge will not be approved unless tests pass -->

## Documentation
- [ ] swagger documentation updated (required for API changes)
- [ ] official documentation updated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Important meeting Discuss in meeting before merge needs more discussion
Projects
None yet
Development

No branches or pull requests

2 participants