From fb94138c5ca7cdbdbb72bd297dcb91496b60d2d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nuno=20G=C3=B3is?= Date: Fri, 22 Dec 2023 08:17:23 +0000 Subject: [PATCH] chore: support full path schemas (#5723) This backwards compatible change allows us to specify a schema `id` (full path) which to me feels a bit better than specifying the schema name as a string, since a literal string is prone to typos. ### Before ```ts requestBody: createRequestSchema( 'createResourceSchema', ), responses: { ...getStandardResponses(400, 401, 403, 415), 201: resourceCreatedResponseSchema( 'resourceSchema', ), }, ``` ### After ```ts requestBody: createRequestSchema( createResourceSchema.$id, ), responses: { ...getStandardResponses(400, 401, 403, 415), 201: resourceCreatedResponseSchema( resourceSchema.$id, ), }, ``` --- src/lib/openapi/util/create-request-schema.ts | 4 +++- src/lib/openapi/util/create-response-schema.ts | 8 ++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/lib/openapi/util/create-request-schema.ts b/src/lib/openapi/util/create-request-schema.ts index 19d45e532abe..8ea6617af757 100644 --- a/src/lib/openapi/util/create-request-schema.ts +++ b/src/lib/openapi/util/create-request-schema.ts @@ -9,7 +9,9 @@ export const createRequestSchema = ( content: { 'application/json': { schema: { - $ref: `#/components/schemas/${schemaName}`, + $ref: schemaName.startsWith('#') + ? schemaName + : `#/components/schemas/${schemaName}`, }, }, }, diff --git a/src/lib/openapi/util/create-response-schema.ts b/src/lib/openapi/util/create-response-schema.ts index 5f4ab2ebb897..ac395dcb4cd5 100644 --- a/src/lib/openapi/util/create-response-schema.ts +++ b/src/lib/openapi/util/create-response-schema.ts @@ -13,7 +13,9 @@ export const createResponseSchemas = ( export const schemaNamed = (schemaName: string): OpenAPIV3.MediaTypeObject => { return { schema: { - $ref: `#/components/schemas/${schemaName}`, + $ref: schemaName.startsWith('#') + ? schemaName + : `#/components/schemas/${schemaName}`, }, }; }; @@ -62,7 +64,9 @@ export const resourceCreatedResponseSchema = ( content: { 'application/json': { schema: { - $ref: `#/components/schemas/${schemaName}`, + $ref: schemaName.startsWith('#') + ? schemaName + : `#/components/schemas/${schemaName}`, }, }, },