swagger-parser icon indicating copy to clipboard operation
swagger-parser copied to clipboard

swagger-parser `10.0.3` version is not compatible with `typescript` version `4.4.2`

Open jjtang1985 opened this issue 3 years ago • 6 comments

Description

Dependabot created a PR for updating typescript version to 4.4.2, which failed due to the error below:

TypeError: Class is not a constructor

    at resolve (/.../node_modules/@apidevtools/json-schema-ref-parser/lib/index.js:162:18)

How to reproduce

  1. update typescript version to 4.4.2.
  2. run the following code and see errors
  async resolve(document); // typed as `OpenAPIV3.Document`

Expected behaviour

The type error is fixed.

Additional information

It's working with typescript version 4.3.5.

jjtang1985 avatar Sep 02 '21 11:09 jjtang1985

Typescript tightened the rules for the this pointer see here: https://github.com/Microsoft/TypeScript-wiki/blob/main/Breaking-Changes.md#typescript-44

As a consequence a this is undefined here in the parser: https://github.com/APIDevTools/json-schema-ref-parser/blob/f6886ab9ee576754791f2a194b140a637dd98a87/lib/index.js#L162

I guess in the mentioned place the usage of this should be adjusted to mathc the ES6 export standard mentioned in the TS changelog. However, this could be a breaking change because user could rely on the usage of this within the method when they fiddle around with the context.

FrankEssenberger avatar Oct 19 '21 14:10 FrankEssenberger

Has anyone figured out a work around?

milesj avatar Nov 04 '21 00:11 milesj

To fix this after updating to Angular 13 and TS 4.4, I just changed from importing an individual method to the entire object:

import { validate } from '@apidevtools/swagger-parser'; to import $SwaggerParser from '@apidevtools/swagger-parser';

And then use $SwaggerParser.validate instead of validate. Should work for the other methods also.

nspire909 avatar Nov 10 '21 02:11 nspire909

Can somebody send a pull request if you've figured the problem out?

philsturgeon avatar May 10 '22 21:05 philsturgeon

We used the following work around in our project:

export async function resolveBound(
  document: OpenAPIV3.Document
): Promise<$Refs> {
  return resolve.bind(SwaggerParser)(document);

This sets the this pointer to the SwaggerParser and the old code works. Either you would have to wrap the public facing methods with something similar or you need to adjust the code that it does not use the this pointer the way it does currently.

FrankEssenberger avatar May 12 '22 05:05 FrankEssenberger

@FrankEssenberger thanks frank. I'd really appreciate it if you could find some time to send a pull request moving your workaround upatream as a solution. I am technically maintaining this project but I've never used it in my life, and I've already put about 10 hours into try fix the release process and various dependencies that are causing troubles, unpaid, just as a favor to the previous owner. If you've got tooling which uses this tool it would be awesome to help me get it fixed. 🙌🏻

philsturgeon avatar May 12 '22 07:05 philsturgeon