Skip to content

Commit dec0e4b

Browse files
erunionjonlucakanadgupta
authored
feat(parser): rewriting the library in typescript (#932)
## 🐳 Context Now that our fork of [@apidevtools/swagger-parser](https://npm.im/@apidevtools/swagger-parser) has fully deviated and was moved here from [our other repository](https://github.com/readmeio/openapi-parser) I have built off of JonLuca's[^1] starting work over in APIDevTools/swagger-parser#253 and rewritten the library in TS. This is all going to be the basis for a slew of forthcoming changes in order to support error levels. Because this is a full rewrite, and a lot of things have changed, this will incur a major version bump. ## 🧰 Changes * [x] Rewrote our OpenAPI parser in TS. * [x] Added in dual support for CJS and ESM. * [x] Resolved a number of test quirks and invalid false positive assertions. [^1]: Because I adapted heavily from his draft PR of this rewrite over in the swagger-parser repository I've credited him as a co-author of this work. --------- Co-authored-by: JonLuca De Caro <[email protected]> Co-authored-by: Kanad Gupta <[email protected]>
1 parent f24ed0f commit dec0e4b

File tree

82 files changed

+3083
-3184
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+3083
-3184
lines changed

.eslintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"extends": ["@readme/eslint-config", "@readme/eslint-config/typescript", "@readme/eslint-config/esm"],
3-
"root": true
3+
"root": true,
44
}

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packages/parser/test/specs/large-file-memory-leak/cloudflare-stringified.json

.vscode/settings.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"editor.defaultFormatter": "esbenp.prettier-vscode",
3+
"editor.codeActionsOnSave": {
4+
"source.fixAll": "explicit"
5+
},
6+
"editor.formatOnSave": true
7+
}

package-lock.json

+1,661-1,143
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"./packages/*"
2828
],
2929
"devDependencies": {
30-
"@readme/eslint-config": "^14.0.0",
30+
"@readme/eslint-config": "^14.2.0",
3131
"@vitest/coverage-v8": "^3.0.4",
3232
"alex": "^11.0.1",
3333
"eslint": "^8.57.0",

packages/oas-normalize/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@
7777
"eslint": "^8.57.0",
7878
"nock": "^14.0.0",
7979
"tsup": "^8.0.2",
80-
"typescript": "^5.1.6"
80+
"typescript": "^5.1.6",
81+
"vitest": "^3.0.5"
8182
},
8283
"prettier": "@readme/eslint-config/prettier"
8384
}

packages/oas-normalize/src/index.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type { Options } from './lib/types.js';
2+
import type { ParserOptions } from '@readme/openapi-parser';
23
import type { OpenAPI, OpenAPIV2, OpenAPIV3 } from 'openapi-types';
34

45
import fs from 'node:fs';
56

6-
import openapiParser from '@readme/openapi-parser';
7+
import { OpenAPIParser } from '@readme/openapi-parser';
78
import postmanToOpenAPI from '@readme/postman-to-openapi';
89
import converter from 'swagger2openapi';
910

@@ -108,9 +109,8 @@ export default class OASNormalize {
108109

109110
return schema;
110111
})
111-
.then(schema => openapiParser.bundle(schema))
112+
.then(schema => new OpenAPIParser().bundle(schema))
112113
.then(bundle => {
113-
// @ts-expect-error The typings on the parser are messed up rigth now while a rewrite is in progress.
114114
this.cache.bundle = bundle;
115115
return bundle;
116116
});
@@ -134,9 +134,8 @@ export default class OASNormalize {
134134

135135
return schema;
136136
})
137-
.then(schema => openapiParser.dereference(schema))
137+
.then(schema => new OpenAPIParser().dereference(schema))
138138
.then(dereferenced => {
139-
// @ts-expect-error The typings on the parser are messed up rigth now while a rewrite is in progress.
140139
this.cache.deref = dereferenced;
141140
return dereferenced;
142141
});
@@ -181,7 +180,7 @@ export default class OASNormalize {
181180
*/
182181
async validate(
183182
opts: {
184-
parser?: openapiParser.Options;
183+
parser?: ParserOptions;
185184
} = {},
186185
): Promise<true> {
187186
const parserOptions = opts.parser || {};
@@ -208,7 +207,7 @@ export default class OASNormalize {
208207
}
209208

210209
/**
211-
* `openapiParser.validate()` dereferences schemas at the same time as validation, mutating
210+
* `OpenAPIParser.validate()` dereferences schemas at the same time as validation, mutating
212211
* the supplied parameter in the process, and does not give us an option to disable this.
213212
* As we already have a dereferencing method on this library, and this method just needs to
214213
* tell us if the API definition is valid or not, we need to clone the schema before
@@ -217,8 +216,7 @@ export default class OASNormalize {
217216
// eslint-disable-next-line try-catch-failsafe/json-parse
218217
const clonedSchema = JSON.parse(JSON.stringify(schema));
219218

220-
// @ts-expect-error The typings on the parser are messed up rigth now while a rewrite is in progress.
221-
return openapiParser.validate(clonedSchema, parserOptions).then(() => {
219+
return new OpenAPIParser().validate(clonedSchema, parserOptions).then(() => {
222220
// The API definition, whatever its format or specification, is valid.
223221
return true;
224222
});

packages/oas-to-har/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
"jest-expect-har": "^7.1.0",
6363
"tsup": "^8.0.2",
6464
"type-fest": "^4.18.3",
65-
"typescript": "^5.2.2"
65+
"typescript": "^5.2.2",
66+
"vitest": "^3.0.5"
6667
},
6768
"prettier": "@readme/eslint-config/prettier"
6869
}

packages/oas-to-snippet/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
"oas": "file:../oas",
6363
"tsup": "^8.0.2",
6464
"type-fest": "^4.18.3",
65-
"typescript": "^5.2.2"
65+
"typescript": "^5.2.2",
66+
"vitest": "^3.0.5"
6667
},
6768
"prettier": "@readme/eslint-config/prettier"
6869
}

packages/oas/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@
105105
"@types/memoizee": "^0.4.11",
106106
"@types/node": "^22.7.6",
107107
"tsup": "^8.0.2",
108-
"typescript": "^5.4.4"
108+
"typescript": "^5.4.4",
109+
"vitest": "^3.0.5"
109110
},
110111
"prettier": "@readme/eslint-config/prettier"
111112
}

packages/oas/test/operation/index.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type * as RMOAS from '../../src/types.js';
22

33
import petstoreSpec from '@readme/oas-examples/3.0/json/petstore.json';
4-
import openapiParser from '@readme/openapi-parser';
4+
import { OpenAPIParser } from '@readme/openapi-parser';
55
import { beforeAll, describe, it, expect } from 'vitest';
66

77
import Oas from '../../src/index.js';
@@ -708,7 +708,7 @@ describe('#getSecurityWithTypes()', () => {
708708

709709
// The original API doc should still be valid.
710710
const clonedSpec = JSON.parse(JSON.stringify(spec.api));
711-
await expect(openapiParser.validate(clonedSpec)).resolves.toStrictEqual(
711+
await expect(OpenAPIParser.validate(clonedSpec)).resolves.toStrictEqual(
712712
expect.objectContaining({
713713
openapi: '3.1.0',
714714
}),

packages/oas/test/operation/lib/get-response-as-json-schema.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { HttpMethods, ResponseObject, SchemaObject } from '../../../src/types.js';
22

3-
import openapiParser from '@readme/openapi-parser';
3+
import { OpenAPIParser } from '@readme/openapi-parser';
44
import { beforeAll, describe, test, expect, it } from 'vitest';
55

66
import Oas from '../../../src/index.js';
@@ -301,7 +301,7 @@ describe('quirks', () => {
301301
});
302302

303303
// The original spec should still validate too!
304-
await expect(openapiParser.validate(cloneObject(definition))).resolves.toStrictEqual(
304+
await expect(OpenAPIParser.validate(cloneObject(definition))).resolves.toStrictEqual(
305305
expect.objectContaining({
306306
openapi: '3.0.2',
307307
}),

packages/parser/.editorconfig

-27
This file was deleted.

packages/parser/.eslintrc

+4-43
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,10 @@
11
{
2-
"root": true,
3-
"extends": [
4-
"@readme/eslint-config",
5-
"@readme/eslint-config/typescript",
6-
],
7-
"env": {
8-
"browser": true,
9-
"node": true
10-
},
112
"rules": {
3+
"camelcase": ["error", { "allow": ["OpenAPIV3_1"] }],
4+
"lines-between-class-members": "off",
5+
"no-continue": "off",
126
"no-plusplus": "off",
13-
"no-restricted-syntax": "off",
14-
"no-underscore-dangle": "off",
15-
"no-use-before-define": "off",
167
"prefer-rest-params": "off",
17-
"prefer-spread": "off"
8+
"prefer-spread": "off",
189
},
19-
"overrides": [
20-
{
21-
// The typings in this file are pretty bad right now, when we have native types we can
22-
// remove this.
23-
"files": ["lib/index.d.ts"],
24-
"rules": {
25-
"@typescript-eslint/consistent-indexed-object-style": "off",
26-
"@typescript-eslint/consistent-type-imports": "off",
27-
"@typescript-eslint/no-explicit-any": "off",
28-
"@typescript-eslint/sort-type-constituents": "off",
29-
"eslint-comments/no-unused-disable": "off",
30-
"lines-between-class-members": "off",
31-
"max-classes-per-file": "off",
32-
"quotes": "off",
33-
"typescript-sort-keys/interface": "off"
34-
}
35-
},
36-
{
37-
// These can all get removed when the library is moved over to native TS.
38-
"files": ["*.js"],
39-
"rules": {
40-
"@typescript-eslint/no-this-alias": "off",
41-
"@typescript-eslint/no-unused-vars": "off",
42-
"@typescript-eslint/no-use-before-define": "off",
43-
"@typescript-eslint/no-var-requires": "off",
44-
"eslint-comments/no-unused-disable": "off",
45-
"func-names": "off"
46-
}
47-
}
48-
]
4910
}

packages/parser/.gitattributes

-26
This file was deleted.

packages/parser/.github/dependabot.yml

-32
This file was deleted.

packages/parser/.github/workflows/ci.yml

-39
This file was deleted.

packages/parser/.github/workflows/lint-pr-title.yml

-19
This file was deleted.

0 commit comments

Comments
 (0)