MCPcopy Create free account
hub / github.com/loopbackio/loopback-next / buildMethodSpec

Function buildMethodSpec

packages/cli/generators/openapi/spec-helper.js:208–435  ·  view source on GitHub ↗

* Build method spec for an operation * @param {object} OpenAPI operation

(controllerSpec, op, options)

Source from the content-addressed store, hash-verified

206 * @param {object} OpenAPI operation
207 */
208function buildMethodSpec(controllerSpec, op, options) {
209 const methodName = getMethodName(op.spec);
210 const opName = op.spec['x-operation-name'] || op.spec['operationId'];
211 controllerSpec.methodMapping[methodName] = opName;
212 const comments = [];
213 let args = [];
214 let interfaceArgs = [];
215 const namedParameters = [];
216 const parameters = op.spec.parameters;
217 // Keep track of param names to avoid duplicates
218 const paramNames = {};
219 const interfaceParamNames = {};
220 if (parameters) {
221 args = parameters.map(p => {
222 const {paramType, argName} = buildParameter(p, paramNames, comments);
223 const paramJson = printSpecObject(p);
224 const optionalType = !p.required ? ' | undefined' : '';
225 return `@param(${paramJson}) ${argName}: ${paramType.signature}${optionalType}`;
226 });
227 interfaceArgs = parameters.map(p => {
228 const param = buildParameter(p, interfaceParamNames);
229 namedParameters.push(param);
230 const {paramType, argName} = param;
231 const optionalType = !p.required ? ' | undefined' : '';
232 return `${argName}: ${paramType.signature}${optionalType}`;
233 });
234 }
235 if (op.spec.requestBody) {
236 /**
237 * requestBody:
238 * description: Pet to add to the store
239 * required: true
240 * content:
241 * application/json:
242 * schema:
243 * $ref: '#/components/schemas/NewPet'
244 */
245 let bodyType = {signature: 'unknown'};
246 const content = op.spec.requestBody.content;
247 const contentType =
248 content &&
249 (content['application/json'] || content[Object.keys(content)[0]]);
250
251 let bodyName = 'requestBody';
252 if (bodyName in paramNames) {
253 bodyName = `${bodyName}${paramNames[bodyName]++}`;
254 }
255 bodyName = escapeIdentifier(bodyName);
256 if (contentType && contentType.schema) {
257 registerAnonymousSchema(
258 [methodName, bodyName],
259 contentType.schema,
260 options,
261 );
262 bodyType = mapSchemaType(contentType.schema, options);
263 addImportsForType(bodyType);
264 }
265 const bodyParam = bodyName; // + (op.spec.requestBody.required ? '' : '?');

Callers 1

buildControllerSpecsFunction · 0.85

Calls 10

getMethodNameFunction · 0.85
buildParameterFunction · 0.85
printSpecObjectFunction · 0.85
escapeIdentifierFunction · 0.85
registerAnonymousSchemaFunction · 0.85
mapSchemaTypeFunction · 0.85
addImportsForTypeFunction · 0.85
isExtensionFunction · 0.85
keysMethod · 0.65
matchMethod · 0.65

Tested by

no test coverage detected