Skip to content

Commit 6f9a5fd

Browse files
danielsharveytheoomoregbee
authored andcommitted
fix: Typescript tsc error using OpenApi.UpdatedSchema
Replacing return type for `generateAttributeSchema()` with OpenApi.UpdatedSchema causes tsc error, probably due to elements like `allOf`. Proposal is refactor UpdatedSchema more fully based on OpenAPI 3. Also refactor old and unused schema field `common_name`.
1 parent 58f80c9 commit 6f9a5fd

File tree

4 files changed

+81
-49
lines changed

4 files changed

+81
-49
lines changed

lib/generators.ts

+17-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SwaggerSailsModel, NameKeyMap, SwaggerRouteInfo, BlueprintActionTemplates, Defaults, MiddlewareType, SwaggerSailsController, Action2Response } from './interfaces';
2-
import { Schema, Reference, Tag } from 'swagger-schema-official';
2+
import { Reference, Tag } from 'swagger-schema-official';
33
import get from 'lodash/get';
44
import { swaggerTypes, sailAttributePropertiesMap, validationsMap, actions2Responses } from './type-formatter';
55
import assign from 'lodash/assign';
@@ -43,14 +43,14 @@ export const generateSwaggerPath = (path: string): { variables: string[]; path:
4343
* @param {Record<string, any>} attribute Sails model attribute specification as per `Model.js` file
4444
*/
4545
// eslint-disable-next-line @typescript-eslint/no-explicit-any
46-
export const generateAttributeSchema = (attribute: NameKeyMap<any>): Schema & { oneOf?: Schema[] } => {
46+
export const generateAttributeSchema = (attribute: NameKeyMap<any>): OpenApi.UpdatedSchema => {
4747
const ai = attribute || {}, sts = swaggerTypes;
4848

4949
const type = attribute.type || 'string';
5050
const columnType = get(attribute, ['autoMigrations', 'columnType']);
5151
const autoIncrement = get(attribute, ['autoMigrations', 'autoIncrement']);
5252

53-
let schema: Schema & { oneOf?: Schema[]; common_name?: string } = {};
53+
let schema: OpenApi.UpdatedSchema = {};
5454

5555
if (ai.model) {
5656
assign(schema, {
@@ -152,23 +152,21 @@ export const generateAttributeSchema = (attribute: NameKeyMap<any>): Schema & {
152152

153153
// note: required --> required[] (not here, needs to be done at model level)
154154

155-
delete schema.common_name;
156-
157155
return schema;
158156
}
159157

160158

161159
/**
162160
* Generate Swagger schema content describing specified Sails models.
163161
* @see https://swagger.io/docs/specification/data-models/
164-
* @param models parsed Sails models as per `parsers.parseModels()`
165-
* @returns
162+
* @param models parsed Sails models as per `parsers.parseModels()`
163+
* @returns
166164
*/
167-
export const generateSchemas = (models: NameKeyMap<SwaggerSailsModel>): NameKeyMap<Schema | Reference> => {
165+
export const generateSchemas = (models: NameKeyMap<SwaggerSailsModel>): NameKeyMap<OpenApi.UpdatedSchema | Reference> => {
168166
return Object.keys(models)
169167
.reduce((schemas, identiy) => {
170168
const model = models[identiy]
171-
const schema: Schema = {
169+
const schema: OpenApi.UpdatedSchema = {
172170
description: get(model, 'swagger.description', `Sails ORM Model **${model.globalId}**`),
173171
required: [],
174172
}
@@ -179,28 +177,28 @@ export const generateSchemas = (models: NameKeyMap<SwaggerSailsModel>): NameKeyM
179177
props[attributeName] = generateAttributeSchema(attribute);
180178
if (attribute.required) schema.required!.push(attributeName);
181179
return props
182-
}, {} as NameKeyMap<Schema>)
180+
}, {} as NameKeyMap<OpenApi.UpdatedSchema>)
183181

184182
if (schema.required!.length <= 0) delete schema.required;
185183

186184
schemas[model.identity] = schema;
187185

188186
return schemas
189-
}, {} as NameKeyMap<Schema | Reference>)
187+
}, {} as NameKeyMap<OpenApi.UpdatedSchema | Reference>)
190188
}
191189

192190
/**
193191
* Generate Swagger schema content describing specified Sails routes/actions.
194192
*
195193
* @see https://swagger.io/docs/specification/paths-and-operations/
196-
*
194+
*
197195
* TODO: break down this function into smaller methods and add tests separately
198-
*
199-
* @param routes
200-
* @param templates
201-
* @param defaultsValues
202-
* @param action2s
203-
* @param specification
196+
*
197+
* @param routes
198+
* @param templates
199+
* @param defaultsValues
200+
* @param action2s
201+
* @param specification
204202
*/
205203
export const generatePaths = (routes: SwaggerRouteInfo[], templates: BlueprintActionTemplates, defaultsValues: Defaults, action2s: NameKeyMap<SwaggerSailsController>, specification: Omit<OpenApi.OpenApi, 'paths'>): OpenApi.Paths => {
206204
const paths = {};
@@ -547,4 +545,4 @@ export const generatePaths = (routes: SwaggerRouteInfo[], templates: BlueprintAc
547545
}
548546

549547
return paths
550-
}
548+
}

lib/interfaces.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import { OpenApi } from '../types/openapi';
3-
import { Reference, Tag } from 'swagger-schema-official';
3+
import { Reference, Tag, ExternalDocs } from 'swagger-schema-official';
44

55
export interface SwaggerGenerator {
66
includeRoute?: (route: SwaggerRouteInfo) => boolean;
@@ -48,7 +48,7 @@ export interface SwaggerAttribute {
4848
export interface SwaggerSailsModel extends Omit<Sails.Model, 'attributes'> {
4949
attributes: Record<string, Sails.AttributeValue & {
5050
description?: string;
51-
externalDocs?: OpenApi.ExternalDoc;
51+
externalDocs?: ExternalDocs;
5252
example?: string;
5353
}>;
5454
swagger: SwaggerAttribute;
@@ -68,7 +68,7 @@ export interface SwaggerSailsRouteControllerTarget extends Sails.RouteController
6868
interface BluePrintActionTemplate {
6969
summary: string;
7070
description: string;
71-
externalDocs: OpenApi.ExternalDoc;
71+
externalDocs: ExternalDocs;
7272
parameters: Array<'primaryKeyPathParameter' | Reference | OpenApi.Parameter>;
7373
resultDescription: string;
7474
notFoundDescription?: string;
@@ -145,4 +145,4 @@ export interface SwaggerRouteInfo {
145145

146146
export interface NameKeyMap<T> {
147147
[name: string]: T;
148-
}
148+
}

lib/type-formatter.ts

+11-13
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,23 @@ import { BlueprintActionTemplates, Modifiers, Action2Response, Defaults } from '
99

1010
/**
1111
* this is used to map our sails types with the allowed type defintions based on swagger specification
12-
* @type {{integer: {common_name: string, type: string, format: string, comments: string}, long: {common_name: string, type: string, format: string, comments: string}, float: {common_name: string, type: string, format: string}, double: {common_name: string, type: string, format: string}, string: {common_name: string, type: string}, byte: {common_name: string, type: string, format: string, comments: string}, binary: {common_name: string, type: string, format: string, comments: string}, boolean: {common_name: string, type: string}, date: {common_name: string, type: string, format: string, comments: string}, datetime: {common_name: string, type: string, format: string, comments: string}, password: {common_name: string, type: string, format: string, comments: string}}}
1312
*/
1413
export const swaggerTypes = {
15-
integer: { common_name: 'integer', type: 'integer', format: 'int32', /* comments: 'signed 32 bits' */ },
16-
long: { common_name: 'long', type: 'integer', format: 'int64', /* comments: 'signed 64 bits' */ },
17-
float: { common_name: 'float', type: 'number', format: 'float' },
18-
double: { common_name: 'double', type: 'number', format: 'double' },
19-
string: { common_name: 'string', type: 'string' },
20-
byte: { common_name: 'byte', type: 'string', format: 'byte', /* comments: 'base64 encoded characters' */ },
21-
binary: { common_name: 'binary', type: 'string', format: 'binary', /* comments: 'any sequence of octets' */ },
22-
boolean: { common_name: 'boolean', type: 'boolean' },
23-
date: { common_name: 'date', type: 'string', format: 'date', /* comments: 'As defined by full-date - RFC3339' */ },
14+
integer: { type: 'integer', format: 'int32', /* comments: 'signed 32 bits' */ },
15+
long: { type: 'integer', format: 'int64', /* comments: 'signed 64 bits' */ },
16+
float: { type: 'number', format: 'float' },
17+
double: { type: 'number', format: 'double' },
18+
string: { type: 'string' },
19+
byte: { type: 'string', format: 'byte', /* comments: 'base64 encoded characters' */ },
20+
binary: { type: 'string', format: 'binary', /* comments: 'any sequence of octets' */ },
21+
boolean: { type: 'boolean' },
22+
date: { type: 'string', format: 'date', /* comments: 'As defined by full-date - RFC3339' */ },
2423
datetime: {
25-
common_name: 'dateTime',
2624
type: 'string',
2725
format: 'date-time',
2826
/* comments: 'As defined by date-time - RFC3339' */
2927
},
30-
password: { common_name: 'password', type: 'string', format: 'password', /* comments: 'A hint to UIs to obscure input' */ }
28+
password: { type: 'string', format: 'password', /* comments: 'A hint to UIs to obscure input' */ }
3129
};
3230

3331
/**
@@ -272,4 +270,4 @@ export const defaults: Defaults = {
272270
'404': { description: 'Resource not found' },
273271
'500': { description: 'Internal server error' }
274272
}
275-
};
273+
};

types/openapi.d.ts

+49-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import { Schema, Reference, Header, Tag, Security } from 'swagger-schema-official';
2+
import { Reference, Header, Tag, Security, ExternalDocs, XML, ParameterType } 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
@@ -31,14 +31,50 @@ declare namespace OpenApi {
3131
variables?: Array<string | { enum?: Array<string>; default: string; description?: string }>;
3232
}
3333

34-
export interface ExternalDoc {
35-
description?: string;
36-
url: string;
37-
}
38-
39-
export type UpdatedSchema = Schema & {
40-
oneOf?: UpdatedSchema[];
41-
items?: UpdatedSchema;
34+
export interface Discriminator {
35+
propertyName: string;
36+
mapping?: { [key: string]: string };
37+
}
38+
39+
export interface UpdatedSchema {
40+
nullable?: boolean;
41+
discriminator?: Discriminator;
42+
readOnly?: boolean;
43+
writeOnly?: boolean;
44+
xml?: XML;
45+
externalDocs?: ExternalDocs;
46+
example?: any;
47+
examples?: any[];
48+
deprecated?: boolean;
49+
50+
type?: ParameterType;
51+
allOf?: (UpdatedSchema | Reference)[];
52+
oneOf?: (UpdatedSchema | Reference)[];
53+
anyOf?: (UpdatedSchema | Reference)[];
54+
not?: UpdatedSchema | Reference;
55+
items?: UpdatedSchema | Reference;
56+
properties?: { [propertyName: string]: (UpdatedSchema | Reference) };
57+
additionalProperties?: (UpdatedSchema | Reference | boolean);
58+
description?: string;
59+
format?: string;
60+
default?: any;
61+
62+
title?: string;
63+
multipleOf?: number;
64+
maximum?: number;
65+
exclusiveMaximum?: boolean;
66+
minimum?: number;
67+
exclusiveMinimum?: boolean;
68+
maxLength?: number;
69+
minLength?: number;
70+
pattern?: string;
71+
maxItems?: number;
72+
minItems?: number;
73+
uniqueItems?: boolean;
74+
maxProperties?: number;
75+
minProperties?: number;
76+
required?: string[];
77+
enum?: any[];
4278
}
4379

4480
export interface Parameter {
@@ -100,7 +136,7 @@ declare namespace OpenApi {
100136
tags?: Array<string>;
101137
summary?: string;
102138
description?: string;
103-
externalDocs?: ExternalDoc;
139+
externalDocs?: ExternalDocs;
104140
operationId?: string;
105141
parameters: Array<Parameter>;
106142
requestBody?: RequestBody | Reference;
@@ -131,7 +167,7 @@ declare namespace OpenApi {
131167

132168

133169
export interface Components {
134-
schemas?: Map<string, Schema | Reference> | EmptyObject;
170+
schemas?: Map<string, UpdatedSchema | Reference> | EmptyObject;
135171
responses?: Map<string, Response | Reference> | EmptyObject;
136172
parameters?: Record<string, Parameter | Reference> | EmptyObject;
137173
examples?: Map<string, Example | Reference> | EmptyObject;
@@ -150,6 +186,6 @@ declare namespace OpenApi {
150186
components?: Components;
151187
security?: Array<Security>;
152188
tags?: Array<Tag>;
153-
externalDocs: ExternalDoc;
189+
externalDocs: ExternalDocs;
154190
}
155-
}
191+
}

0 commit comments

Comments
 (0)