Skip to content

Commit

Permalink
fix(operations): support parameters in the path section
Browse files Browse the repository at this point in the history
fix #37
  • Loading branch information
aleksandryackovlev committed Jan 31, 2022
1 parent 78785b0 commit 90cbae6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/operations/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,25 @@ export class Operation {

securitySchemes: { [key: string]: OpenAPIV3.SecuritySchemeObject } | null;

parentParams: (OpenAPIV3.ReferenceObject | OpenAPIV3.ParameterObject)[] | null;

constructor({
method,
path,
operation,
securitySchemes,
generator,
parentParams,
}: {
path: string;
method: string;
operation: OpenAPIV3.OperationObject;
generator: JSF;
securitySchemes?: { [key: string]: OpenAPIV3.SecuritySchemeObject };
parentParams?: (OpenAPIV3.ReferenceObject | OpenAPIV3.ParameterObject)[];
}) {
this.pathPattern = path.replace(/\{([^/}]+)\}/g, (p1: string, p2: string): string => `:${p2}`);
this.parentParams = parentParams || null;

this.method = method.toUpperCase();
this.operation = operation;
Expand Down Expand Up @@ -127,7 +132,17 @@ export class Operation {
},
};

const parameters = get(this.operation, ['parameters']);
let parameters: (OpenAPIV3.ReferenceObject | OpenAPIV3.ParameterObject)[] = [];

if (this.parentParams) {
parameters = [...this.parentParams];
}

const localParams = get(this.operation, ['parameters']);

if (localParams) {
parameters = [...parameters, ...localParams];
}

if (parameters) {
parameters.forEach((parameter) => {
Expand Down Expand Up @@ -186,17 +201,20 @@ export const createOperation = ({
operation,
generator,
securitySchemes,
parentParams,
}: {
path: string;
method: string;
operation: OpenAPIV3.OperationObject;
generator: JSF;
securitySchemes?: { [key: string]: OpenAPIV3.SecuritySchemeObject };
parentParams?: (OpenAPIV3.ReferenceObject | OpenAPIV3.ParameterObject)[];
}): Operation =>
new Operation({
method,
path,
operation,
generator,
securitySchemes,
parentParams,
});
4 changes: 3 additions & 1 deletion src/operations/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@ export class Operations {
pathOperations: OpenAPIV3.PathItemObject,
securitySchemes?: { [key: string]: OpenAPIV3.SecuritySchemeObject }
): Operation[] {
return toPairs(pathOperations).map(([method, operation]) =>
const { parameters, ...rest } = pathOperations;
return toPairs(rest).map(([method, operation]) =>
createOperation({
method,
path: pathName,
operation: operation as OpenAPIV3.OperationObject,
securitySchemes,
generator: this.generator,
parentParams: parameters,
})
);
}
Expand Down

0 comments on commit 90cbae6

Please sign in to comment.