Skip to content

Commit

Permalink
fix(openapi): allow user to override accept from the schema (#4419)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan authored Sep 1, 2022
1 parent c6fb653 commit 2772150
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-buckets-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@omnigraph/openapi': patch
---

fix(openapi): allow user to override accept from the schema
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,16 @@ export async function getJSONSchemaOptionsFromOpenAPIOptions(
oneOf: [],
};

const allMimeTypes = Object.keys(responseObj.content);
let allMimeTypes: string[] = [];
if (typeof operationHeaders === 'object') {
const acceptFromOperationHeader = operationHeaders.accept || operationHeaders.Accept;
if (acceptFromOperationHeader) {
allMimeTypes = [acceptFromOperationHeader];
}
}
if (allMimeTypes.length === 0) {
allMimeTypes = Object.keys(responseObj.content) as string[];
}
const jsonLikeMimeTypes = allMimeTypes.filter(c => c !== '*/*' && c.toString().includes('json'));
const mimeTypes = jsonLikeMimeTypes.length > 0 ? jsonLikeMimeTypes : allMimeTypes;

Expand Down
21 changes: 19 additions & 2 deletions packages/loaders/openapi/tests/__snapshots__/basket.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ Object {
"description": undefined,
"field": "post_basket",
"headers": Object {
"Content-Type": "application/json",
"accept": "application/json",
},
"method": "POST",
"operationHeaders": undefined,
"path": "/basket",
"requestSchema": Object {
"type": "string",
},
"responseByStatusCode": Object {
"200": Object {
"responseSchema": Object {
Expand Down Expand Up @@ -279,7 +283,11 @@ Object {
"type": "object",
},
"MutationInput": Object {
"additionalProperties": true,
"properties": Object {
"post_basket": Object {
"$ref": "#/definitions/mutationInput_post_basket",
},
},
"title": "MutationInput",
"type": "object",
"writeOnly": true,
Expand Down Expand Up @@ -324,6 +332,15 @@ Object {
"title": "_schema",
"type": "object",
},
"mutationInput_post_basket": Object {
"properties": Object {
"input": Object {
"type": "string",
},
},
"title": "mutationInput_post_basket",
"type": "object",
},
"mutation_post_basket_oneOf_0_products_items": Object {
"oneOf": Array [
Object {
Expand Down Expand Up @@ -418,7 +435,7 @@ type LocationModel {
}
type Mutation {
post_basket: [post_basket_response]
post_basket(input: String): [post_basket_response]
}
union post_basket_response = BasketResponse | ApiError
Expand Down
33 changes: 33 additions & 0 deletions packages/loaders/openapi/tests/basket.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { printSchemaWithDirectives } from '@graphql-tools/utils';
import { Headers } from '@whatwg-node/fetch';
import { execute, parse } from 'graphql';
import loadGraphQLSchemaFromOpenAPI, { createBundle } from '../src';

describe('Basket', () => {
Expand All @@ -16,4 +18,35 @@ describe('Basket', () => {
});
expect(printSchemaWithDirectives(schema)).toMatchSnapshot();
});
it('user can override accept value defined by the schema', async () => {
const providedAccept = 'application/random+json';
let givenHeader: string;
const schema = await loadGraphQLSchemaFromOpenAPI('basket', {
source: './fixtures/basket.json',
cwd: __dirname,
operationHeaders: {
accept: providedAccept,
},
async fetch(input, init) {
const headers = new Headers(init?.headers);
givenHeader = headers.get('accept');
return new Response(
JSON.stringify({
random: 2,
})
);
},
});
await execute({
schema,
document: parse(/* GraphQL */ `
mutation {
post_basket(input: "test") {
__typename
}
}
`),
});
expect(givenHeader).toBe(providedAccept);
});
});
9 changes: 9 additions & 0 deletions packages/loaders/openapi/tests/fixtures/basket.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
"paths": {
"/basket": {
"post": {
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
}
},
"responses": {
"200": {
"description": "Success",
Expand Down

1 comment on commit 2772150

@vercel
Copy link

@vercel vercel bot commented on 2772150 Sep 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.