Skip to content

Commit bd93a63

Browse files
fix: conditionally import param and requestBody in controller template
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 #3335
1 parent d1838ad commit bd93a63

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

packages/cli/generators/openapi/spec-helper.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,13 @@ function buildControllerSpecs(operationsMapByController, options) {
455455
controllerSpec.methods = entry.operations.map(op =>
456456
buildMethodSpec(controllerSpec, op, options),
457457
);
458+
// Track whether param and requestBody decorators are used
459+
controllerSpec.usesParam = controllerSpec.methods.some(m =>
460+
m.signature && m.signature.includes('@param('),
461+
);
462+
controllerSpec.usesRequestBody = controllerSpec.methods.some(m =>
463+
m.signature && m.signature.includes('@requestBody('),
464+
);
458465
controllerSpec.methodMappingObject = printSpecObject(
459466
controllerSpec.methodMapping,
460467
);

packages/cli/generators/openapi/templates/src/controllers/controller-template.ts.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {api, operation, param, requestBody} from '@loopback/rest';<%if (withImplementation) { %>
1+
import {api, operation<% if (usesParam) { %>, param<% } %><% if (usesRequestBody) { %>, requestBody<% } %>} from '@loopback/rest';<%if (withImplementation) { %>
22
import {inject} from '@loopback/core';
33
44
import {<%- serviceClassName %>} from '../services';

0 commit comments

Comments
 (0)