redoc icon indicating copy to clipboard operation
redoc copied to clipboard

Two $ref containing "~1" (escaped slash) cause "Invalid reference token:"

Open pocka opened this issue 2 years ago • 5 comments

Describe the bug Redoc shows an error screen when,

  • there are two $ref pointing external file (I'm not sure about more than two)
  • both of them include ~1, escaped slash character, in its JSON pointer tokens
  • both of them referencing different files (or JSON pointer tokens?)

This does not happen if the value of two $refs references are same or the path does not include ~1. The error message for

$ref: "./responses.yml#/StringResponse/application~1json/schema"

is

Something went wrong...

Invalid reference token: application/json-schema 

(Reproduction CodeSandbox)

Expected behavior Docs appears without an error, or shows an error that a user can fix or work-around the problem.

Minimal reproducible OpenAPI snippet(if possible)

# oas.yml
openapi: "3.1.0"

info:
  title: "Redoc JSON pointer parsing bug"
  version: "0.0.1"

paths:
  /foo:
    get:
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: "./responses.yml#/StringResponse/application~1json/schema"

  /bar:
    get:
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: "./responses.yml#/NumberResponse/application~1json/schema"
                # Using this ref instead of the NumberResponse works
                #$ref: "./responses.yml#/StringResponse/application~1json/schema"
                # Using this (no slash in pointer tokens) ref instead of the NumberResponse works
                #$ref: "./responses.yml#/ObjectResponse/application-json/schema"
# responses.yml
StringResponse:
  application/json:
    schema:
      type: string

NumberResponse:
  application/json:
    schema:
      type: number

ObjectResponse:
  application-json:
    schema:
      type: object
      properties:
        foo:
          type: string

Screenshots None.

Additional context None.

pocka avatar Jun 13 '23 07:06 pocka

Hi @pocka, thank you for the reporting issue. we'll check that.

AlexVarchuk avatar Jun 13 '23 10:06 AlexVarchuk

Hi @pocka, after small investigation, we know where is the problem. It is hard to fix. We should decode $ref before get it. But if we cover current case we broke others. Before we fix that, please, avoid using escape characters in ref. You can do it like that:

# openapi.yaml
...
  responses:
        "200":
          $ref: "./responses.yml#/StringResponse"
# responses.yml
StringResponse:
  content:
    application/json:
      schema:
        type: string

AlexVarchuk avatar Jun 15 '23 14:06 AlexVarchuk

Hi, I'm also running into this issue (IntelliJ IDEA changed their openapi editor preview to use redocly instead of swagger-ui).

For my use case I am $referencing a path as follows:

  /health:
    $ref: cbss/common/v1/common-v1.yaml#/paths/~1health

So I don't immediately see any workaround for avoiding the ~1 as paths need to start with a slash? Except for upgrading to OpenAPI 3.1 which supports named reusable path objects.

jpraet avatar Aug 31 '23 10:08 jpraet

@AlexVarchuk I think it should be fixed in openapi-core package we use. Let's discuss it.

I raised the priority to p1.

RomanHotsiy avatar Sep 03 '23 04:09 RomanHotsiy