loopbackio / loopbackio/loopback-next

Generated controllers can import unused decorators/empty constructor.

Open
#3,335 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug CLI developer-experience good first issue help wanted OpenAPI
Dominant language
TypeScript
Stars
5.1k
Forks
1.1k
Avg merge
2d 21h
Merged PRs (30d)
27

Description

Steps to reproduce

Using the open api generator, generate a controller that does not use any request bodies or does not pass any parameters.

This can be problematic depending on the compiler options, for example having unused parameters. The same could be said for an empty parameterless constructor.

Current Behavior

The param and requestBody decorators are imported in the controller file. An empty constructor is generated.

Expected Behavior

The param and requestBody decorators should not be imported. The constructor should be omitted.

Link to reproduction sandbox

See here: https://github.com/strongloop/loopback-next/blob/72cb8aafad31588deff96fb1af6d33fa03fe5fd5/packages/cli/generators/openapi/templates/src/controllers/controller-template.ts.ejs#L2

The imports are made unconditionally.

Additional information

win32 x64 12.5.0
├─ @loopback/boot@1.4.4
├─ @loopback/context@1.20.2
├─ @loopback/core@1.8.5
├─ @loopback/http-server@1.4.4
├─ @loopback/metadata@1.2.5
├─ @loopback/openapi-v3@1.7.0
├─ @loopback/repository-json-schema@1.8.0
├─ @loopback/repository@1.8.2
├─ @loopback/rest-explorer@1.2.5
├─ @loopback/rest@1.16.3
├─ @loopback/service-proxy@1.2.5
├─ loopback-connector@4.8.0
├─ loopback-datasource-juggler@4.8.2

Related Issues

N/A

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open packages/cli/generators/openapi/templates/src/controllers/controller-template.ts.ejs, where the issue identifies unconditional imports. Generate a controller with no request bodies or parameters and inspect the emitted controller. Done means unused param/requestBody decorator imports and the empty constructor are omitted; check any generator tests covering this template if present.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
backend-api-design, cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.