Skip to content

Commit 8a2a4b5

Browse files
danielsharveytheoomoregbee
authored andcommitted
fix: Correct and improve OpenAPI3 Typescript interface definitions
Correct Typescript interfaces to use Record instead of Map - Map implies implementation of the Map interface. Update schema data type - 'file' not in OpenAPI 3. Update Typescript interfaces for Parameter and Header to OpenAPI 3 Remove incorrect/unneeded EmptyObject references.
1 parent b36bfdf commit 8a2a4b5

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

lib/generators.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ export const generatePaths = (routes: SwaggerRouteInfo[], templates: BlueprintAc
298298
...omit(route.swagger || {}, 'exclude')
299299
};
300300

301-
const isParam = (inType: string, name: string): boolean => !!pathEntry.parameters.find(parameter => parameter.in == inType && parameter.name == name);
301+
const isParam = (inType: string, name: string): boolean => !!pathEntry.parameters.find(parameter => 'in' in parameter && parameter.in == inType && parameter.name == name);
302302

303303
const modifiers = {
304304

@@ -516,7 +516,7 @@ export const generatePaths = (routes: SwaggerRouteInfo[], templates: BlueprintAc
516516
return;
517517
}
518518
const _in = patternVariables.indexOf(key) >= 0 ? 'path' : 'query';
519-
const isExisting = pathEntry.parameters.find(p => p.in === _in && p.name === key)
519+
const isExisting = pathEntry.parameters.find(p => 'in' in p && p.in === _in && p.name === key)
520520
if (isExisting) {
521521
return
522522
}
@@ -642,7 +642,7 @@ export const generatePaths = (routes: SwaggerRouteInfo[], templates: BlueprintAc
642642
if (route.variables) {
643643
// first resolve '$ref' parameters
644644
const resolved = pathEntry.parameters.map(parameter => {
645-
let ref = parameter['$ref' as keyof OpenApi.Parameter] as string;
645+
let ref = '$ref' in parameter && parameter.$ref;
646646
if (!ref) return parameter;
647647
const _prefix = '#/components/parameters/';
648648
if (ref.startsWith(_prefix)) {
@@ -654,7 +654,7 @@ export const generatePaths = (routes: SwaggerRouteInfo[], templates: BlueprintAc
654654

655655
// now add patternVariables that don't already exist
656656
route.variables.map(v => {
657-
const existing = resolved.find(p => p.in == 'path' && p.name == v);
657+
const existing = resolved.find(p => p && 'in' in p && p.in == 'path' && p.name == v);
658658
if (existing) return;
659659
pathEntry.parameters.push({
660660
in: 'path',

types/openapi.d.ts

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import { Reference, Header, Tag, ExternalDocs, XML, ParameterType } from 'swagger-schema-official';
2+
import { Reference, Header, Tag, ExternalDocs, XML } from 'swagger-schema-official';
33

44
// This is simply building from OpenApi Specification
55
// see: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md
66
// TODO: move this to https://github.com/DefinitelyTyped/DefinitelyTyped as openapi-spec
77

88
declare namespace OpenApi {
99

10-
type EmptyObject = Record<string, any>
11-
1210
export interface Info {
1311
title: string;
1412
version: string;
@@ -36,6 +34,8 @@ declare namespace OpenApi {
3634
mapping?: { [key: string]: string };
3735
}
3836

37+
export type DataType = 'string' | 'number' | 'integer' | 'boolean' | 'array' | 'object';
38+
3939
export interface UpdatedSchema {
4040
nullable?: boolean;
4141
discriminator?: Discriminator;
@@ -47,7 +47,7 @@ declare namespace OpenApi {
4747
examples?: any[];
4848
deprecated?: boolean;
4949

50-
type?: ParameterType;
50+
type?: DataType;
5151
allOf?: (UpdatedSchema | Reference)[];
5252
oneOf?: (UpdatedSchema | Reference)[];
5353
anyOf?: (UpdatedSchema | Reference)[];
@@ -96,7 +96,7 @@ declare namespace OpenApi {
9696

9797
export interface Encoding {
9898
contentType?: string;
99-
headers?: Map<string, Header | Reference>;
99+
headers?: Record<string, Header | Reference>;
100100
style?: string;
101101
explode?: boolean;
102102
allowReserved?: boolean;
@@ -105,8 +105,8 @@ declare namespace OpenApi {
105105
export interface MediaType {
106106
schema?: UpdatedSchema | Reference;
107107
example?: any;
108-
examples?: Map<string, Example | Reference>;
109-
encoding?: Map<string, Encoding>;
108+
examples?: Record<string, Example | Reference>;
109+
encoding?: Record<string, Encoding>;
110110

111111
}
112112

@@ -119,17 +119,17 @@ declare namespace OpenApi {
119119
export interface Link {
120120
operationRef?: string;
121121
operationId?: string;
122-
parameters?: Map<string, any>;
122+
parameters?: Record<string, any>;
123123
requestBody?: any;
124124
description?: string;
125125
server: Server;
126126
}
127127

128128
export interface Response {
129129
description: string;
130-
headers?: Map<string, Header | Reference>;
130+
headers?: Record<string, Header | Reference>;
131131
content?: Record<string, MediaType>;
132-
links?: Map<string, Link | Reference>;
132+
links?: Record<string, Link | Reference>;
133133
}
134134

135135
export interface Operation {
@@ -138,7 +138,7 @@ declare namespace OpenApi {
138138
description?: string;
139139
externalDocs?: ExternalDocs;
140140
operationId?: string;
141-
parameters: Array<Parameter>;
141+
parameters: Array<Parameter | Reference>;
142142
requestBody?: RequestBody | Reference;
143143
responses: Record<string, Response>;
144144
servers?: Array<Server>;
@@ -250,15 +250,15 @@ declare namespace OpenApi {
250250
*/
251251

252252
export interface Components {
253-
schemas?: Map<string, UpdatedSchema | Reference> | EmptyObject;
254-
responses?: Map<string, Response | Reference> | EmptyObject;
255-
parameters?: Record<string, Parameter | Reference> | EmptyObject;
256-
examples?: Map<string, Example | Reference> | EmptyObject;
257-
requestBodies?: Map<string, RequestBody | Reference> | EmptyObject;
258-
headers?: Map<string, Header | Reference> | EmptyObject;
259-
securitySchemes?: Map<string, Security | Reference> | EmptyObject;
260-
links?: Map<string, Link | Reference> | EmptyObject;
261-
callbacks?: Map<string, Path | Reference> | EmptyObject;
253+
schemas?: Record<string, UpdatedSchema | Reference>;
254+
responses?: Record<string, Response | Reference>;
255+
parameters?: Record<string, Parameter | Reference>;
256+
examples?: Record<string, Example | Reference>;
257+
requestBodies?: Record<string, RequestBody | Reference>;
258+
headers?: Record<string, Header | Reference>;
259+
securitySchemes?: Record<string, Security | Reference>;
260+
links?: Record<string, Link | Reference>;
261+
callbacks?: Record<string, Path | Reference>;
262262
}
263263

264264
export interface OpenApi {

0 commit comments

Comments
 (0)