| 37 | * @param paramSpec - Parameter specification. |
| 38 | */ |
| 39 | export function param(paramSpec: ParameterObject) { |
| 40 | return function (target: object, member: string, index: number) { |
| 41 | paramSpec = {...paramSpec}; |
| 42 | // Get the design time method parameter metadata |
| 43 | const methodSig = MetadataInspector.getDesignTypeForMethod(target, member); |
| 44 | const paramTypes = methodSig?.parameterTypes || []; |
| 45 | |
| 46 | // Map design-time parameter type to the OpenAPI param type |
| 47 | |
| 48 | const paramType = paramTypes[index]; |
| 49 | |
| 50 | if (paramType) { |
| 51 | if ( |
| 52 | // generate schema if `paramSpec` doesn't have it |
| 53 | !paramSpec.schema || |
| 54 | // generate schema if `paramSpec` has `schema` but without `type` |
| 55 | (isSchemaObject(paramSpec.schema) && !paramSpec.schema.type) |
| 56 | ) { |
| 57 | // If content explicitly mentioned do not resolve schema |
| 58 | if (!paramSpec.content) { |
| 59 | // please note `resolveSchema` only adds `type` and `format` for `schema` |
| 60 | paramSpec.schema = resolveSchema(paramType, paramSpec.schema); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if ( |
| 66 | paramSpec.schema && |
| 67 | isSchemaObject(paramSpec.schema) && |
| 68 | paramSpec.schema.type === 'array' |
| 69 | ) { |
| 70 | // The design-time type is `Object` for `any` |
| 71 | if (paramType != null && paramType !== Object && paramType !== Array) { |
| 72 | throw new Error( |
| 73 | `The parameter type is set to 'array' but the JavaScript type is ${paramType.name}`, |
| 74 | ); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | ParameterDecoratorFactory.createDecorator<ParameterObject>( |
| 79 | OAI3Keys.PARAMETERS_KEY, |
| 80 | paramSpec, |
| 81 | {decoratorName: '@param'}, |
| 82 | )(target, member, index); |
| 83 | }; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * The `type` and `format` inferred by a common name of OpenAPI 3.0.0 data type |