Skip to content

Commit

Permalink
Specific parameters should override general ones
Browse files Browse the repository at this point in the history
Fixes #297
  • Loading branch information
Luis Fernando Planella Gonzalez committed Nov 12, 2023
1 parent ce7d4ae commit ba45dd4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
31 changes: 21 additions & 10 deletions lib/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,21 @@ export class Operation {
this.methodName = spec['x-operation-name'] || this.id;

// Add both the common and specific parameters
this.parameters = [
...this.collectParameters(pathSpec.parameters),
...this.collectParameters(spec.parameters),
const allParams = [
...this.collectParameters(false, pathSpec.parameters),
...this.collectParameters(true, spec.parameters),
];
// Maybe there were duplicated parameters? In this case, let specific parameters replace the general ones
this.parameters = [];
allParams.forEach(param => {
let skip = false;
if (!param.specific) {
skip = !!allParams.find(p => p !== param && p.name === param.name && p.specific);
}
if (!skip) {
this.parameters.push(param);
}
});
if (this.parameters.find(p => p.required)) {
this.parametersRequired = true;
}
Expand Down Expand Up @@ -84,23 +95,23 @@ export class Operation {
return false;
}

private collectParameters(params: (ParameterObject | ReferenceObject)[] | undefined): Parameter[] {
private collectParameters(specific: boolean, params: (ParameterObject | ReferenceObject)[] | undefined): Parameter[] {
const result: Parameter[] = [];
if (params) {
for (let param of params) {

if (param.$ref) {
param = resolveRef(this.openApi, param.$ref);
}
param = param as ParameterObject;

if (param.in === 'cookie') {
this.logger.warn(`Ignoring cookie parameter ${this.id}.${param.name} as cookie parameters cannot be sent in XmlHttpRequests.`);
} else if (this.paramIsNotExcluded(param)) {
result.push(new Parameter(param as ParameterObject, this.options, this.openApi));
} else if (!this.paramIsExcluded(param)) {
const parameter = new Parameter(param as ParameterObject, this.options, this.openApi);
parameter.specific = specific;
result.push(parameter);
}
}

}
return result;
}
Expand All @@ -119,9 +130,9 @@ export class Operation {
});
}

private paramIsNotExcluded(param: ParameterObject): boolean {
private paramIsExcluded(param: ParameterObject): boolean {
const excludedParameters = this.options.excludeParameters || [];
return !excludedParameters.includes(param.name);
return excludedParameters.includes(param.name);
}

private collectContent(desc: ContentObject | undefined): Content[] {
Expand Down
1 change: 1 addition & 0 deletions lib/parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class Parameter {
style?: string;
explode?: boolean;
parameterOptions: string;
specific = false;

constructor(public spec: ParameterObject, options: Options, openApi: OpenAPIObject) {
this.name = spec.name;
Expand Down
7 changes: 7 additions & 0 deletions test/all-operations.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@
],
"description": "Path 1 GET description",
"parameters": [
{
"name": "common1",
"description": "Specific common param 1",
"schema": {
"$ref": "#/components/schemas/RefString"
}
},
{
"name": "get1",
"description": "GET param 1",
Expand Down
12 changes: 6 additions & 6 deletions test/all-operations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ describe('Generation tests using all-operations.json', () => {
expect(operation.method).toBe('get');
expect(operation.parameters.length).toBe(9); // 2 shared, 7 own
const params = operation.parameters;
expect(params[0].name).toBe('common1');
expect(params[0].type).toBe('RefString');
expect(params[0].in).toBe('query');
expect(params[1].name).toBe('common2');
expect(params[1].in).toBe('header');
expect(params[1].type).toBe('RefObject');
expect(params[0].name).toBe('common2');
expect(params[0].in).toBe('header');
expect(params[0].type).toBe('RefObject');
expect(params[1].name).toBe('common1');
expect(params[1].type).toBe('RefString');
expect(params[1].in).toBe('query');
expect(params[2].name).toBe('get1');
expect(params[2].type).toBe('RefString');
expect(params[2].in).toBe('query');
Expand Down

0 comments on commit ba45dd4

Please sign in to comment.