[BUG] Parsing fails for OpenAPI schema referenced multiple times

Open Beginner friendly
#1,249 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Domain
api

Research direction

Start by running the parser against packages/openapi-schema-parser/test/documents/valid-asyncapi3.yaml, as described in the reproduction steps. Inspect the OpenAPI schema parser dependency and its existing tests to confirm the repeated-reference failure. Done means the document parses without InvalidTypeError and all OpenAPI 3.0 schemas load correctly.

Written by the indexing model from the issue text.

Description

bug
Describe the bug.

An InvalidTypeError is thrown during document parsing of a valid OpenAPI 3.0 schema which uses nullable: true, even though schemaFormat was set correctly.

The error only occurs in some circumstances when a schema is referenced multiple times.

I previously reported this as https://github.com/asyncapi/openapi-schema-parser/issues/275 before the move of the openapi-schema-parser component.

The transformation of OpenAPI Schema to JSON Schema using the @openapi-contrib/openapi-schema-to-json-schema library, modifies the input OpenAPI Schema. This triggers the error when the same schema is transformed for a second time. This seems to be a bug in this version of the library; it occurs even though the cloneSchema parameter is set to true.

For the first reference, the schema with nullable gets correctly transformed from OpenAPI Schema:

type: ["string"] , nullable: true

to Json Schema:

type: ["string", "null"]

When it is then parsed again (because the same schema is also referenced from another location), the transformation fails with the InvalidTypeError because it doesn't expect a type with an array as value in an OpenAPI 3.0 Schema.

If this issue is accepted, I can create a pull request to update the @openapi-contrib/openapi-schema-to-json-schema dependency, which I verified fixes the issue (code is in this branch. I first tried a structuredClone on the input schema, but that didn't fix all occurrences of the issue.

Expected behavior

AsyncAPI being parsed without error, with all OpenAPI 3.0 schemas correctly loaded.

Screenshots
Image
How to Reproduce
  1. Load this asyncapi document in AsyncAPI studio, or run the parser directly on it
  2. This will result in an InvalidTypeError
🖥️ Device Information [optional]
  • Operating System (OS): Windows 11
  • Browser: Edge
  • Browser Version: 153
👀 Have you checked for similar open issues?
  • I checked and didn't find similar issue
🏢 Have you read the Contributing Guidelines?
Are you willing to work on this issue ?

Yes I am willing to submit a PR!

Dominant language
TypeScript
Stars
144
Forks
146
Avg merge
6m
Merged PRs (30d)
9

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from asyncapi/parser-js

All issues in asyncapi/parser-js

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.