Skip to content

Commit 5dc4724

Browse files
committed
refactor: clean usage of OAI's ExtensionValue
Remove the definition of ExtensionValue from openapi-v3-types as it is no longer used. Remove Extendable type from openapi-spec-builder and use ISpecificationExtension from openapi3-ts instead. Fix openapi type guards to use a more correct `object` type instead of `ExtensionValue`, since the guards are not dealing with any extensions. Fix json-to-schema converter to use a different workaround for the compiler error related to accessing index properties on a type without any indexer.
1 parent c926031 commit 5dc4724

File tree

4 files changed

+14
-25
lines changed

4 files changed

+14
-25
lines changed

packages/openapi-spec-builder/src/openapi-spec-builder.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
import * as assert from 'assert';
77
import {
8-
ExtensionValue,
98
OpenApiSpec,
109
OperationObject,
1110
ResponseObject,
1211
ParameterObject,
1312
createEmptyApiSpec,
1413
RequestBodyObject,
14+
ISpecificationExtension,
1515
} from '@loopback/openapi-v3-types';
1616

1717
/**
@@ -30,11 +30,7 @@ export function anOperationSpec() {
3030
return new OperationSpecBuilder();
3131
}
3232

33-
export interface Extendable {
34-
[extension: string]: ExtensionValue;
35-
}
36-
37-
export class BuilderBase<T extends Extendable> {
33+
export class BuilderBase<T extends ISpecificationExtension> {
3834
protected _spec: T;
3935

4036
constructor(initialSpec: T) {
@@ -47,7 +43,11 @@ export class BuilderBase<T extends Extendable> {
4743
* @param key The property name starting with "x-".
4844
* @param value The property value.
4945
*/
50-
withExtension(key: string, value: ExtensionValue): this {
46+
withExtension(
47+
key: string,
48+
// tslint:disable-next-line:no-any
49+
value: any,
50+
): this {
5151
assert(
5252
key.startsWith('x-'),
5353
`Invalid extension ${key}, extension keys must be prefixed with "x-"`,

packages/openapi-v3-types/src/openapi-v3-spec-types.ts

-6
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ import {OpenAPIObject} from 'openapi3-ts';
1212
export * from 'openapi3-ts';
1313

1414
export type OpenApiSpec = OpenAPIObject;
15-
/**
16-
* Custom extensions can use arbitrary type as the value,
17-
* e.g. a string, an object or an array.
18-
*/
19-
// tslint:disable-next-line:no-any
20-
export type ExtensionValue = any;
2115

2216
/**
2317
* Create an empty OpenApiSpec object that's still a valid openapi document.

packages/openapi-v3-types/src/type-guards.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
// This file is licensed under the MIT License.
44
// License text available at https://opensource.org/licenses/MIT
55

6-
import {
7-
SchemaObject,
8-
ReferenceObject,
9-
ExtensionValue,
10-
} from './openapi-v3-spec-types';
6+
import {SchemaObject, ReferenceObject} from './openapi-v3-spec-types';
117

128
/**
139
* Type guard for OpenAPI 3.0.0 schema object
@@ -20,6 +16,6 @@ export function isSchemaObject(
2016
return !schema.hasOwnProperty('$ref');
2117
}
2218

23-
export function isReferenceObject(obj: ExtensionValue): obj is ReferenceObject {
19+
export function isReferenceObject(obj: object): obj is ReferenceObject {
2420
return obj.hasOwnProperty('$ref');
2521
}

packages/openapi-v3/src/json-to-schema.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
// License text available at https://opensource.org/licenses/MIT
55

66
import {JsonDefinition} from '@loopback/repository-json-schema';
7-
import {SchemaObject, ExtensionValue} from '@loopback/openapi-v3-types';
7+
import {SchemaObject} from '@loopback/openapi-v3-types';
88
import * as _ from 'lodash';
99

10-
export function jsonToSchemaObject(jsonDef: JsonDefinition): SchemaObject {
11-
const json = jsonDef as {[name: string]: ExtensionValue}; // gets around index signature error
10+
export function jsonToSchemaObject(json: JsonDefinition): SchemaObject {
1211
const result: SchemaObject = {};
1312
const propsToIgnore = [
1413
'anyOf',
@@ -63,7 +62,7 @@ export function jsonToSchemaObject(jsonDef: JsonDefinition): SchemaObject {
6362
case 'enum': {
6463
const newEnum = [];
6564
const primitives = ['string', 'number', 'boolean'];
66-
for (const element of json.enum) {
65+
for (const element of json.enum!) {
6766
if (primitives.includes(typeof element) || element === null) {
6867
newEnum.push(element);
6968
} else {
@@ -76,14 +75,14 @@ export function jsonToSchemaObject(jsonDef: JsonDefinition): SchemaObject {
7675
break;
7776
}
7877
case '$ref': {
79-
result.$ref = json.$ref.replace(
78+
result.$ref = json.$ref!.replace(
8079
'#/definitions',
8180
'#/components/schemas',
8281
);
8382
break;
8483
}
8584
default: {
86-
result[property] = json[property];
85+
result[property] = json[property as keyof JsonDefinition];
8786
break;
8887
}
8988
}

0 commit comments

Comments
 (0)