Skip to content

Commit

Permalink
fix: API-21737 When an Accept parameter is present in an example its …
Browse files Browse the repository at this point in the history
…value… (#123)

* API-21737 When an Accept parameter is present in an example its value is copied...

... to responseContentType so the swagger client picks it up properly

* API-21737 added check for Accept to be a header parameter

* API-21737 removed unnecessary use of optional chaining operator
  • Loading branch information
stevelong00 authored May 16, 2023
1 parent 81d6af3 commit 6297dd7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions @types/swagger-client/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ declare module 'swagger-client' {
requestBody?: RequestBody;
server?: string;
requestInterceptor?: (request: Request) => Request;
responseContentType?: string;
}

export interface Request {
Expand Down
11 changes: 11 additions & 0 deletions src/oas-parsing/schema/oas-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ class OASSchema {
options = { server, ...options };
}

// if the example group contains an Accept header parameter,
// copy that over to responseContentType so it gets added to the header by the swagger client
if (
options.parameters?.Accept !== undefined && // an inexpensive initial test
operation.parameters?.find(
(parameter) => parameter.name === 'Accept' && parameter.in === 'header',
) !== undefined
) {
options.responseContentType = options.parameters.Accept;
}

return schema.execute(options).catch((error) => {
return error.response;
});
Expand Down
16 changes: 16 additions & 0 deletions test/oas-parsing/schema/oas-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ describe('OASSchema', () => {
description: 'a number',
},
},
{
name: 'Accept',
in: 'header',
required: true,
example: 'application/geo+json',
schema: {
type: 'string',
enum: [
'application/geo+json',
'application/vnd.geo+json',
'text/csv',
],
},
},
],
requestBody: {
required: true,
Expand Down Expand Up @@ -121,6 +135,7 @@ describe('OASSchema', () => {
operationId: 'getFacilityById',
parameters: {
id: 'testId',
Accept: 'application/geo+json',
},
requestBody: {
id: 'secondTestId',
Expand All @@ -129,6 +144,7 @@ describe('OASSchema', () => {
authorized: {},
},
server,
responseContentType: 'application/geo+json',
});
});
});
Expand Down

0 comments on commit 6297dd7

Please sign in to comment.