diff --git a/.changeset/nine-buckets-provide.md b/.changeset/nine-buckets-provide.md new file mode 100644 index 0000000000000..b6d5360b0b2bb --- /dev/null +++ b/.changeset/nine-buckets-provide.md @@ -0,0 +1,5 @@ +--- +'@omnigraph/openapi': patch +--- + +fix(openapi): allow user to override accept from the schema diff --git a/packages/loaders/openapi/src/getJSONSchemaOptionsFromOpenAPIOptions.ts b/packages/loaders/openapi/src/getJSONSchemaOptionsFromOpenAPIOptions.ts index 9d07c0b1694e8..e35ab2100e217 100644 --- a/packages/loaders/openapi/src/getJSONSchemaOptionsFromOpenAPIOptions.ts +++ b/packages/loaders/openapi/src/getJSONSchemaOptionsFromOpenAPIOptions.ts @@ -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; diff --git a/packages/loaders/openapi/tests/__snapshots__/basket.test.ts.snap b/packages/loaders/openapi/tests/__snapshots__/basket.test.ts.snap index 0fad5fedb5b81..5f7c3878eb863 100644 --- a/packages/loaders/openapi/tests/__snapshots__/basket.test.ts.snap +++ b/packages/loaders/openapi/tests/__snapshots__/basket.test.ts.snap @@ -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 { @@ -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, @@ -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 { @@ -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 diff --git a/packages/loaders/openapi/tests/basket.test.ts b/packages/loaders/openapi/tests/basket.test.ts index 1040691987ad7..8de6673797fe2 100644 --- a/packages/loaders/openapi/tests/basket.test.ts +++ b/packages/loaders/openapi/tests/basket.test.ts @@ -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', () => { @@ -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); + }); }); diff --git a/packages/loaders/openapi/tests/fixtures/basket.json b/packages/loaders/openapi/tests/fixtures/basket.json index c32f0a5b51ae9..0230a1eba3f9e 100644 --- a/packages/loaders/openapi/tests/fixtures/basket.json +++ b/packages/loaders/openapi/tests/fixtures/basket.json @@ -7,6 +7,15 @@ "paths": { "/basket": { "post": { + "requestBody": { + "content": { + "application/json": { + "schema": { + "type": "string" + } + } + } + }, "responses": { "200": { "description": "Success",