From 033875000f24823a632bf59e4ad721a350002424 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ciar=C3=A1n=20Walsh?= Date: Thu, 9 Feb 2023 17:21:10 +0000 Subject: [PATCH 1/3] Handle responses in schemas --- src/lib/apiSchemaUtils.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/lib/apiSchemaUtils.ts b/src/lib/apiSchemaUtils.ts index aed93ce..ab39957 100644 --- a/src/lib/apiSchemaUtils.ts +++ b/src/lib/apiSchemaUtils.ts @@ -117,8 +117,8 @@ export function getReferencedSchema(ref: string) { let found: | OpenAPIV3.ReferenceObject - | OpenAPIV3.ArraySchemaObject - | OpenAPIV3.NonArraySchemaObject + | OpenAPIV3.SchemaObject + | OpenAPIV3.ResponseObject | undefined = apiSpec.components?.schemas?.[getSchemaNameFromRef(ref)]; if (!found && section === "responses") { @@ -127,11 +127,18 @@ export function getReferencedSchema(ref: string) { found = apiSpec.components?.schemas?.[name]; } + if (found && "content" in found) { + const jsonResponse = found.content?.['application/json'] + if (jsonResponse && jsonResponse.schema) { + found = jsonResponse.schema + } + } + if (found && "$ref" in found) { throw new Error("Referenced schema can not be a reference schema itself"); } - return found; + return found as OpenAPIV3.SchemaObject; } // TODO: type this better, remove all the any casts From 7f9a942661a4e9eb96d00d83553e897021b776ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ciar=C3=A1n=20Walsh?= Date: Thu, 9 Feb 2023 17:34:53 +0000 Subject: [PATCH 2/3] Update .env.local-sample --- .env.local-sample | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.env.local-sample b/.env.local-sample index 16b29dc..ca93945 100644 --- a/.env.local-sample +++ b/.env.local-sample @@ -1,4 +1,5 @@ -REACT_APP_BUNGIE_CLIENT_ID=... +REACT_APP_OAUTH_CLIENT_ID=... +REACT_APP_OAUTH_CLIENT_SECRET=... REACT_APP_API_KEY=... HTTPS=true PORT=4000 From 9a4c0d3a844737b28031a4e33417e0a609478b7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ciar=C3=A1n=20Walsh?= Date: Fri, 10 Feb 2023 14:25:23 +0000 Subject: [PATCH 3/3] Add a guard throw instead of a cast --- src/lib/apiSchemaUtils.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/lib/apiSchemaUtils.ts b/src/lib/apiSchemaUtils.ts index ab39957..611a1e5 100644 --- a/src/lib/apiSchemaUtils.ts +++ b/src/lib/apiSchemaUtils.ts @@ -112,6 +112,10 @@ export function getShortSchemaNameFromRef(ref: string) { return bits[bits.length - 1]; } +function isResponseObject(node: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject | OpenAPIV3.ResponseObject): node is OpenAPIV3.ResponseObject { + return 'content' in node +} + export function getReferencedSchema(ref: string) { const [, , section, name] = ref.split("/"); @@ -127,7 +131,7 @@ export function getReferencedSchema(ref: string) { found = apiSpec.components?.schemas?.[name]; } - if (found && "content" in found) { + if (found && isResponseObject(found)) { const jsonResponse = found.content?.['application/json'] if (jsonResponse && jsonResponse.schema) { found = jsonResponse.schema @@ -138,7 +142,11 @@ export function getReferencedSchema(ref: string) { throw new Error("Referenced schema can not be a reference schema itself"); } - return found as OpenAPIV3.SchemaObject; + if (found && isResponseObject(found)) { + throw new Error("Expected a Schema object, found a ResponseObject"); + } + + return found; } // TODO: type this better, remove all the any casts