diff --git a/lib/src/apigateway/openapi-gateway-lambda.test.ts b/lib/src/apigateway/openapi-gateway-lambda.test.ts index 5637375..64f8bc0 100644 --- a/lib/src/apigateway/openapi-gateway-lambda.test.ts +++ b/lib/src/apigateway/openapi-gateway-lambda.test.ts @@ -7,8 +7,7 @@ import { extendZodWithOpenApi, } from '@asteasolutions/zod-to-openapi'; import z from 'zod'; -import type { oas30, oas31 } from 'openapi3-ts'; -import { Converter } from '@apiture/openapi-down-convert'; +import { oas31 } from 'openapi3-ts'; import { AccessLogFormat, EndpointType, @@ -33,6 +32,7 @@ import { generateOperationId, getPropsWithDefaults, addLogGroupForTracing, + convertOpenapi31ToV30, } from './openapi-gateway-lambda'; extendZodWithOpenApi(z); @@ -309,20 +309,23 @@ describe('openapi-gateway-lambda', () => { }); it('openapi lint should work', async () => { + const odoc31 = testOpenpidoc31(); + const f = (): void => { - lintOpenapiDocument(testOpenpidoc30(), true); + lintOpenapiDocument(convertOpenapi31ToV30(odoc31), true); }; expect(f).not.toThrow(); }); it('openapi lint should fail', async () => { - const odoc = testOpenpidoc30(); + const odoc31 = testOpenpidoc31(); + const odoc30 = convertOpenapi31ToV30(odoc31); // fail if parameter ids in path and declaration doesn't match // @ts-ignore - odoc.paths['/users/{id}'].get.parameters[0].name = 'SOMETHING'; + odoc30.paths['/users/{id}'].get.parameters[0].name = 'SOMETHING'; const f = (): void => { - lintOpenapiDocument(odoc, true); + lintOpenapiDocument(odoc30, true); }; expect(f).toThrow(); }); @@ -744,17 +747,6 @@ const testGatewayProps = (): OpenApiGatewayLambdaProps => { }; }; -const testOpenpidoc30 = (): oas30.OpenAPIObject => { - const odoc = testOpenpidoc31(); - const converter = new Converter(odoc, { - verbose: false, - deleteExampleWithId: true, - allOfTransform: false, - }); - - return converter.convert() as oas30.OpenAPIObject; -}; - const testOpenpidoc31 = (): oas31.OpenAPIObject => { const registry = new OpenAPIRegistry(); diff --git a/lib/src/apigateway/openapi-gateway-lambda.ts b/lib/src/apigateway/openapi-gateway-lambda.ts index ca09042..3e6cc23 100644 --- a/lib/src/apigateway/openapi-gateway-lambda.ts +++ b/lib/src/apigateway/openapi-gateway-lambda.ts @@ -19,7 +19,7 @@ import { ArnFormat, ScopedAws, Size, Stack } from 'aws-cdk-lib/core'; import { ILogGroup, LogGroup, RetentionDays } from 'aws-cdk-lib/aws-logs'; import { ServicePrincipal } from 'aws-cdk-lib/aws-iam'; import { OpenAPIRegistry, OpenApiGeneratorV31, RouteConfig } from '@asteasolutions/zod-to-openapi'; -import { Converter } from '@apiture/openapi-down-convert'; +import { Converter, ConverterOptions } from '@apiture/openapi-down-convert'; import { lintOpenapiDocument } from '../utils/openapi-lint'; import { randomId } from '../utils/misc'; @@ -64,13 +64,7 @@ export class OpenApiGatewayLambda extends Construct { } // AWS Gateway only supports openapi 3.0, so downconvert doc from 3.1 to 3.0 - const converter = new Converter(openapiDoc31, { - verbose: false, - deleteExampleWithId: true, - allOfTransform: false, - }); - const openapiDoc30 = converter.convert() as oas30.OpenAPIObject; - + const openapiDoc30 = convertOpenapi31ToV30(openapiDoc31); lintOpenapiDocument(openapiDoc30, true); // The api gateway does not appends the stackname/stage into it by default @@ -327,6 +321,22 @@ export const generateOpenapiDocWithExtensions = ( return openapiDoc31; }; +// converts OpenAPI 3.1 document to OpenAPI 3.0 +export const convertOpenapi31ToV30 = ( + openapiDoc31: oas31.OpenAPIObject, + converterOpts?: ConverterOptions, +): oas30.OpenAPIObject => { + const converter = new Converter( + openapiDoc31, + converterOpts ?? { + verbose: false, + deleteExampleWithId: true, + allOfTransform: false, + }, + ); + return converter.convert() as oas30.OpenAPIObject; +}; + export const getPropsWithDefaults = ( id: string, props: OpenApiGatewayLambdaProps, diff --git a/lib/src/index.ts b/lib/src/index.ts index 31c384f..a7289ee 100644 --- a/lib/src/index.ts +++ b/lib/src/index.ts @@ -5,6 +5,7 @@ export * from './config/configs'; export { OpenApiGatewayLambda, generateOpenapiDocWithExtensions, + convertOpenapi31ToV30, } from './apigateway/openapi-gateway-lambda'; export * from './apigateway/types';