Skip to content

fix: conditionally import param and requestBody in controller template#11646

Open
mmustafasenoglu wants to merge 1 commit into
loopbackio:masterfrom
mmustafasenoglu:fix/remove-unused-imports
Open

fix: conditionally import param and requestBody in controller template#11646
mmustafasenoglu wants to merge 1 commit into
loopbackio:masterfrom
mmustafasenoglu:fix/remove-unused-imports

Conversation

@mmustafasenoglu

Copy link
Copy Markdown

Summary

Fixes #3335 - The OpenAPI controller template unconditionally imported param and requestBody decorators from @loopback/rest, even when the generated controller did not use any parameters or request bodies.

Changes

  • spec-helper.js: Added tracking for whether @param and @requestBody decorators are used in controller methods
  • controller-template.ts.ejs: Conditionally import these decorators only when needed

Before

import {api, operation, param, requestBody} from "@loopback/rest";

export class MyController {
  constructor() {}
  
  async getAll(): Promise<string[]> {
    throw new Error("Not implemented");
  }
}

After

import {api, operation} from "@loopback/rest";

export class MyController {
  constructor() {}
  
  async getAll(): Promise<string[]> {
    throw new Error("Not implemented");
  }
}

Benefits

  • Eliminates ESLint no-unused-imports errors
  • Cleaner generated code
  • Better developer experience

Closes #3335

The OpenAPI controller template unconditionally imported `param` and
`requestBody` decorators from `@loopback/rest`, even when the
generated controller did not use any parameters or request bodies.

This caused ESLint errors with `no-unused-imports` rule and was
confusing for developers reading the generated code.

Changes:
- Track whether `@param` and `@requestBody` decorators are used
  in controller methods
- Conditionally import these decorators in the template only when needed

Fixes loopbackio#3335
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generated controllers can import unused decorators/empty constructor.

1 participant