Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: apiSpec may be const literal #854

Merged
merged 2 commits into from
Jun 2, 2024
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
20 changes: 19 additions & 1 deletion src/framework/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,26 @@ export type SerDesMap = {
[format: string]: SerDes
};

type Primitive = undefined | null | boolean | string | number | Function

type Immutable<T> =
T extends Primitive ? T :
T extends Array<infer U> ? ReadonlyArray<U> :
T extends Map<infer K, infer V> ? ReadonlyMap<K, V> : Readonly<T>

type DeepImmutable<T> =
T extends Primitive ? T :
T extends Array<infer U> ? DeepImmutableArray<U> :
T extends Map<infer K, infer V> ? DeepImmutableMap<K, V> : DeepImmutableObject<T>

interface DeepImmutableArray<T> extends ReadonlyArray<DeepImmutable<T>> {}
interface DeepImmutableMap<K, V> extends ReadonlyMap<DeepImmutable<K>, DeepImmutable<V>> {}
type DeepImmutableObject<T> = {
readonly [K in keyof T]: DeepImmutable<T[K]>
}

export interface OpenApiValidatorOpts {
apiSpec: OpenAPIV3.Document | string;
apiSpec: DeepImmutable<OpenAPIV3.Document> | string;
validateApiSpec?: boolean;
validateResponses?: boolean | ValidateResponseOpts;
validateRequests?: boolean | ValidateRequestOpts;
Expand Down
30 changes: 16 additions & 14 deletions test/default-export.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,29 @@ import { expect } from 'chai';
import * as request from 'supertest';
import * as path from 'path';

const schema = {
openapi: '3.0.0',
info: { version: '1.0.0', title: 'test bug OpenApiValidator' },
servers: [],
paths: {
'/': {
get: {
operationId: 'anything',
'x-eov-operation-handler': 'controller-with-default',
responses: { 200: { description: 'home api' } }
}
},
},
} as const;

describe('default export resolver', () => {
let server = null;
let app = express();

before(async () => {
app.use(
OpenApiValidator.middleware({
apiSpec: {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this json schema is removed... what is the impact of this change on passing a json schema directly in code, similar to what this test previously accepted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The as const schema is stricter in every way, so existing inline schemas should continue to type-check in the same way.

If a schema is declared as an untyped variable (and not as const) and passed here it may have looser guarantees than if it were declared inline, due to lack of excess property checks.

openapi: '3.0.0',
info: { version: '1.0.0', title: 'test bug OpenApiValidator' },
paths: {
'/': {
get: {
operationId: 'anything',
// @ts-ignore
'x-eov-operation-handler': 'controller-with-default',
responses: { 200: { description: 'home api' } }
}
},
},
},
apiSpec: schema,
operationHandlers: path.join(__dirname, 'resources'),
}),
);
Expand Down
Loading