Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions lib/src/apigateway/openapi-gateway-lambda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -33,6 +32,7 @@ import {
generateOperationId,
getPropsWithDefaults,
addLogGroupForTracing,
convertOpenapi31ToV30,
} from './openapi-gateway-lambda';

extendZodWithOpenApi(z);
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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();

Expand Down
26 changes: 18 additions & 8 deletions lib/src/apigateway/openapi-gateway-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './config/configs';
export {
OpenApiGatewayLambda,
generateOpenapiDocWithExtensions,
convertOpenapi31ToV30,
} from './apigateway/openapi-gateway-lambda';
export * from './apigateway/types';

Expand Down
Loading