Skip to content

Commit e35a07c

Browse files
duncanbeeverscdimascio
andauthoredJun 2, 2024··
chore: apiSpec may be const literal (#854)
Co-authored-by: Carmine DiMascio <[email protected]>
1 parent a708132 commit e35a07c

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed
 

‎src/framework/types.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,26 @@ export type SerDesMap = {
113113
[format: string]: SerDes
114114
};
115115

116+
type Primitive = undefined | null | boolean | string | number | Function
117+
118+
type Immutable<T> =
119+
T extends Primitive ? T :
120+
T extends Array<infer U> ? ReadonlyArray<U> :
121+
T extends Map<infer K, infer V> ? ReadonlyMap<K, V> : Readonly<T>
122+
123+
type DeepImmutable<T> =
124+
T extends Primitive ? T :
125+
T extends Array<infer U> ? DeepImmutableArray<U> :
126+
T extends Map<infer K, infer V> ? DeepImmutableMap<K, V> : DeepImmutableObject<T>
127+
128+
interface DeepImmutableArray<T> extends ReadonlyArray<DeepImmutable<T>> {}
129+
interface DeepImmutableMap<K, V> extends ReadonlyMap<DeepImmutable<K>, DeepImmutable<V>> {}
130+
type DeepImmutableObject<T> = {
131+
readonly [K in keyof T]: DeepImmutable<T[K]>
132+
}
133+
116134
export interface OpenApiValidatorOpts {
117-
apiSpec: OpenAPIV3.Document | string;
135+
apiSpec: DeepImmutable<OpenAPIV3.Document> | string;
118136
validateApiSpec?: boolean;
119137
validateResponses?: boolean | ValidateResponseOpts;
120138
validateRequests?: boolean | ValidateRequestOpts;

‎test/default-export.spec.ts

+16-14
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,29 @@ import { expect } from 'chai';
44
import * as request from 'supertest';
55
import * as path from 'path';
66

7+
const schema = {
8+
openapi: '3.0.0',
9+
info: { version: '1.0.0', title: 'test bug OpenApiValidator' },
10+
servers: [],
11+
paths: {
12+
'/': {
13+
get: {
14+
operationId: 'anything',
15+
'x-eov-operation-handler': 'controller-with-default',
16+
responses: { 200: { description: 'home api' } }
17+
}
18+
},
19+
},
20+
} as const;
21+
722
describe('default export resolver', () => {
823
let server = null;
924
let app = express();
1025

1126
before(async () => {
1227
app.use(
1328
OpenApiValidator.middleware({
14-
apiSpec: {
15-
openapi: '3.0.0',
16-
info: { version: '1.0.0', title: 'test bug OpenApiValidator' },
17-
paths: {
18-
'/': {
19-
get: {
20-
operationId: 'anything',
21-
// @ts-ignore
22-
'x-eov-operation-handler': 'controller-with-default',
23-
responses: { 200: { description: 'home api' } }
24-
}
25-
},
26-
},
27-
},
29+
apiSpec: schema,
2830
operationHandlers: path.join(__dirname, 'resources'),
2931
}),
3032
);

0 commit comments

Comments
 (0)
Please sign in to comment.