From 885da719ca56b299adc670ca51cc023a9b299621 Mon Sep 17 00:00:00 2001 From: Maece Fischer Date: Sun, 24 Mar 2024 18:02:04 +0100 Subject: [PATCH] add componentRef specification --- src/oas3/codegen/ts/endpoint.ts | 1 + src/oas3/codegen/ts/response.ts | 5 ++-- src/oas3/codegen/ts/schema.ts | 6 ++-- src/oas3/specification/componentRef.ts | 38 ++++++++++++++++++++++++++ src/oas3/specification/request.ts | 4 +-- src/oas3/specification/response.ts | 4 +-- 6 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/oas3/codegen/ts/endpoint.ts b/src/oas3/codegen/ts/endpoint.ts index 2c15f6b0..f3181975 100644 --- a/src/oas3/codegen/ts/endpoint.ts +++ b/src/oas3/codegen/ts/endpoint.ts @@ -89,6 +89,7 @@ export function applyEndpointCallerFunction( path, requiredOutputPaths: [ endpointIdConstDefinition.path, + templateRequestType.path, requestResultTypeDefinition.path, templateRequestHandlerType.path, ], diff --git a/src/oas3/codegen/ts/response.ts b/src/oas3/codegen/ts/response.ts index 96eea331..50d745ae 100644 --- a/src/oas3/codegen/ts/response.ts +++ b/src/oas3/codegen/ts/response.ts @@ -1,5 +1,5 @@ import { - isComponentRef, + isResponseComponentRef, ResponseBodyContent, ResponseByStatusCodeMap, } from '@oas3/specification'; @@ -61,7 +61,7 @@ export function applyResponseByStatusCodeMap( for (const statusCode in schema) { const responseOutputPath: OutputPath = [...path, statusCode]; const responseOrRef = schema[statusCode]; - if (isComponentRef(responseOrRef)) { + if (isResponseComponentRef(responseOrRef)) { const responseOutput = applyComponentRefSchema( codeGenerator, responseOrRef, @@ -107,6 +107,7 @@ export function applyResponseByStatusCodeMap( }, requiredOutputPaths: responseOutputs.map(o => o.path), }; + codeGenerator.addIndirectOutput(responseTypeDefinition); return { path, requiredOutputPaths: [responseTypeDefinition.path], diff --git a/src/oas3/codegen/ts/schema.ts b/src/oas3/codegen/ts/schema.ts index 51f54d0e..2e5549bb 100644 --- a/src/oas3/codegen/ts/schema.ts +++ b/src/oas3/codegen/ts/schema.ts @@ -16,10 +16,10 @@ import { ComponentRef, isArraySchema, isBooleanSchema, - isComponentRef, isNumberSchema, isObjectSchema, isOneOfSchema, + isSchemaComponentRef, isStringSchema, NumberSchema, ObjectSchema, @@ -33,7 +33,7 @@ export function applySchema( schema: Schema, path: OutputPath ): CodeGenerationOutput { - if (isComponentRef(schema)) { + if (isSchemaComponentRef(schema)) { return applyComponentRefSchema(codeGenerator, schema, path); } if (isBooleanSchema(schema)) { @@ -350,7 +350,7 @@ export function applyOneOfSchema( itemPath, objectDiscriminatorConfig ); - } else if (isComponentRef(itemSchema)) { + } else if (isSchemaComponentRef(itemSchema)) { itemOutput = applyComponentRefSchema( codeGenerator, itemSchema, diff --git a/src/oas3/specification/componentRef.ts b/src/oas3/specification/componentRef.ts index 1675eaff..0ce68d78 100644 --- a/src/oas3/specification/componentRef.ts +++ b/src/oas3/specification/componentRef.ts @@ -12,3 +12,41 @@ export function isComponentRef(anyValue: any): anyValue is ComponentRef { } return true; } + +export function isSchemaComponentRef(anyValue: any): anyValue is ComponentRef { + const value = anyValue as ComponentRef; + if (!isComponentRef(value)) { + return false; + } + return value.$ref.startsWith('#/components/schemas/'); +} + +export function isResponseComponentRef( + anyValue: any +): anyValue is ComponentRef { + const value = anyValue as ComponentRef; + if (!isComponentRef(value)) { + return false; + } + return value.$ref.startsWith('#/components/responses/'); +} + +export function isParameterComponentRef( + anyValue: any +): anyValue is ComponentRef { + const value = anyValue as ComponentRef; + if (!isComponentRef(value)) { + return false; + } + return value.$ref.startsWith('#/components/parameters/'); +} + +export function isSecuritySchemesComponentRef( + anyValue: any +): anyValue is ComponentRef { + const value = anyValue as ComponentRef; + if (!isComponentRef(value)) { + return false; + } + return value.$ref.startsWith('#/components/securitySchemes/'); +} diff --git a/src/oas3/specification/request.ts b/src/oas3/specification/request.ts index 4cf0cf84..b82fb5ae 100644 --- a/src/oas3/specification/request.ts +++ b/src/oas3/specification/request.ts @@ -1,6 +1,6 @@ import {isSchema, Schema} from './schema'; import {isResponseByStatusCodeMap, ResponseByStatusCodeMap} from './response'; -import {ComponentRef, isComponentRef} from './componentRef'; +import {ComponentRef, isParameterComponentRef} from './componentRef'; type PermissionsBySecurityName = { [securityName: string]: string[]; @@ -145,7 +145,7 @@ export function isRequest(anyValue: any): anyValue is Request { return false; } const invalidParameter = value.parameters?.find( - p => !isRequestParameter(p) && !isComponentRef(p) + p => !isRequestParameter(p) && !isParameterComponentRef(p) ); if (invalidParameter) { return false; diff --git a/src/oas3/specification/response.ts b/src/oas3/specification/response.ts index 3ff732fa..bc0dc3b5 100644 --- a/src/oas3/specification/response.ts +++ b/src/oas3/specification/response.ts @@ -1,5 +1,5 @@ import {isSchema, Schema} from './schema'; -import {ComponentRef, isComponentRef} from './componentRef'; +import {ComponentRef, isResponseComponentRef} from './componentRef'; export type ResponseBodyContent = { schema: Schema; @@ -53,7 +53,7 @@ export function isResponseByStatusCodeMap( const value = anyValue as ResponseByStatusCodeMap; for (const statusCode in value) { const responseOrRef = value[statusCode]; - if (!isComponentRef(responseOrRef) && !isResponse(responseOrRef)) { + if (!isResponseComponentRef(responseOrRef) && !isResponse(responseOrRef)) { return false; } }