Skip to content

Node Error: t.includes is not a function #1051

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

Closed
1 task done
willsamu opened this issue Mar 13, 2023 · 4 comments
Closed
1 task done

Node Error: t.includes is not a function #1051

willsamu opened this issue Mar 13, 2023 · 4 comments
Labels
openapi-ts Relevant to the openapi-typescript library question Further information is requested

Comments

@willsamu
Copy link

Description

Parsing OpenAPI Schema exported from Stoplight fails for a valid schema. The maybeTypes uses include which only works for array or string inputs. Failed if the input is number (e.g. maybeTypes = [ 2, 4, 5 ] or maybeTypes = [ 'Record<string, never>', 1, 2, 3, 4, 5, 6, 7, 8, 9 ]).

$ npx openapi-typescript schema.yaml

file:///Users/myuser/.npm/_npx/21d31b7563e6adbc/node_modules/openapi-typescript/dist/transform/schema-object.js:55
        if (maybeTypes.some((t) => t.includes("{")))
                                                              ^

TypeError: t.includes is not a function
    at file:///Users/myuser/.npm/_npx/21d31b7563e6adbc/node_modules/openapi-typescript/dist/transform/schema-object.js:55:63
    at Array.some (<anonymous>)
    at defaultSchemaObjectTransform (file:///Users/myuser/.npm/_npx/21d31b7563e6adbc/node_modules/openapi-typescript/dist/transform/schema-object.js:55:49)
    at transformSchemaObject (file:///Users/myuser/.npm/_npx/21d31b7563e6adbc/node_modules/openapi-typescript/dist/transform/schema-object.js:3:20)
    at defaultSchemaObjectTransform (file:///Users/myuser/.npm/_npx/21d31b7563e6adbc/node_modules/openapi-typescript/dist/transform/schema-object.js:116:45)
    at transformSchemaObject (file:///Users/myuser/.npm/_npx/21d31b7563e6adbc/node_modules/openapi-typescript/dist/transform/schema-object.js:3:20)
    at transformMediaTypeObject (file:///Users/myuser/.npm/_npx/21d31b7563e6adbc/node_modules/openapi-typescript/dist/transform/media-type-object.js:5:12)
    at transformRequestBodyObject (file:///Users/myuser/.npm/_npx/21d31b7563e6adbc/node_modules/openapi-typescript/dist/transform/request-body-object.js:26:31)
    at transformOperationObject (file:///Users/myuser/.npm/_npx/21d31b7563e6adbc/node_modules/openapi-typescript/dist/transform/operation-object.js:79:37)
    at transformPathItemObject (file:///Users/myuser/.npm/_npx/21d31b7563e6adbc/node_modules/openapi-typescript/dist/transform/path-item-object.js:19:35)
Name Version
openapi-typescript 6.2.0
Node.js 16.14.0
OS + version macOS 13.1

Reproduction

Did not investigate which part exactly it was in the input. However, I could fix it locally by editing the code in dist/transform/schema-object.js:
Original:

  if ("oneOf" in schemaObject && !schemaObject.oneOf.some((t) => "$ref" in t && ctx.discriminators[t.$ref])) {
      const maybeTypes = schemaObject.oneOf.map((item) => transformSchemaObject(item, { path, ctx }));
      if (maybeTypes.some((t) => t.includes("{")))
          return tsOneOf(...maybeTypes);
      return tsUnionOf(...maybeTypes);
  }

Fix:

  if ("oneOf" in schemaObject && !schemaObject.oneOf.some((t) => "$ref" in t && ctx.discriminators[t.$ref])) {
      const maybeTypes = schemaObject.oneOf.map((item) => transformSchemaObject(item, { path, ctx }));
      if (maybeTypes.some((t) => (typeof t === 'string'  || Array.isArray(t)) && t.includes("{")))
          return tsOneOf(...maybeTypes);
      return tsUnionOf(...maybeTypes);
  }

Expected result

Runs without error.

Checklist

  • I’m willing to open a PR (see CONTRIBUTING.md) - if someone can point me to the implementation in the source file
@qnp
Copy link
Contributor

qnp commented Mar 23, 2023

Exact, I ran the same issue and had the same conclusion. Another problem is if your oneOf have a const value which is 0 because of the condition if (schemaObject.const) which will be falsy even if it should not.

I'll try to address both problems in a PR

@qnp qnp mentioned this issue Mar 23, 2023
3 tasks
@mitchell-merry
Copy link
Contributor

Was this fixed? @qnp

If not, could we provide the schema that fails here please?

@drwpow
Copy link
Contributor

drwpow commented Apr 7, 2023

The diagnosis is the exact line of code failing, but we’d still need either the whole schema, or a small, individual schema object that throws this error. These kind of errors are obtuse, but they either stem from a) an invalid schema (most of the time) or b) an unexpected edge-case in the valid spec

The original message said the schema passed a validator. But we’d still need the failing schema object to fix the error properly (checking for string or array from your proposed solution may fix your individual issue, but likely wouldn’t fix any of the myriad other issues this would cause).

@drwpow drwpow added the question Further information is requested label Apr 7, 2023
@drwpow drwpow added the openapi-ts Relevant to the openapi-typescript library label May 22, 2023
@drwpow
Copy link
Contributor

drwpow commented Jul 31, 2023

Closing this issue, but if you could provide the schema this erred on I’ll take a look. I’m 99% sure though this is from an error in your schema, and openapi-typescript just doesn’t provide a helpful error on the specific invalid bit. I’d suggest Redoc’s linter on the strictest settings to find the issue.

@drwpow drwpow closed this as completed Jul 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
openapi-ts Relevant to the openapi-typescript library question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants