asyncapi / asyncapi/parser-js

JSON Schema references do not follow $id dereference behavior

Open
#403 7 comments 0 reactions 0 assignees View on GitHub
bug keep-open
Dominant language
TypeScript
Stars
144
Forks
144
Avg merge
6m
Merged PRs (30d)
11

Description

#### Describe the bug
We use the [@apidevtools/json-schema-ref-parser](https://github.com/asyncapi/parser-js/blob/f17424a0639a61fb19d2004d96ca652b9ec09f5d/package.json#L68) dependency to resolve all references, however, this tool does not follow JSON Schema specification in terms of dereferencing: https://github.com/APIDevTools/json-schema-ref-parser/issues/22#issuecomment-231783185

Based on [JSON Schema draft 7, it states that](https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-01#section-8.2):
> The "$id" keyword defines a URI for the schema, and the base URI that other URI references within the schema are resolved against. A subschema's "$id" is resolved against the base URI of its parent schema. If no parent sets an explicit base with "$id", the base URI is that of the entire document, as determined per RFC 3986 section 5 [RFC3986].

This means that given the following AsyncAPI document:
```yaml
asyncapi: '2.2.0'
info:
title: Test overriding dereferenced objects
version: '1.0.0'
channels:
test:
publish:
message:
$ref: '#/components/messages/myMessage'
components:
messages:
myMessage:
schemaFormat: application/schema+json;version=draft-07
name: MyMessage
payload:
$id: 'http://localhost.com/'
type: object
properties:
sentAt:
$ref: "/components/schemas/sentAt"
```

Check out the [studio example](https://studio.asyncapi.com/?base64=YXN5bmNhcGk6ICcyLjIuMCcKaW5mbzoKICB0aXRsZTogVGVzdCBvdmVycmlkaW5nIGRlcmVmZXJlbmNlZCBvYmplY3RzIAogIHZlcnNpb246ICcxLjAuMCcKY2hhbm5lbHM6CiAgdGVzdDoKICAgIHB1Ymxpc2g6CiAgICAgIG1lc3NhZ2U6CiAgICAgICAgJHJlZjogJyMvY29tcG9uZW50cy9tZXNzYWdlcy9teU1lc3NhZ2UnCmNvbXBvbmVudHM6CiAgbWVzc2FnZXM6CiAgICBteU1lc3NhZ2U6CiAgICAgIHNjaGVtYUZvcm1hdDogYXBwbGljYXRpb24vc2NoZW1hK2pzb247dmVyc2lvbj1kcmFmdC0wNwogICAgICBuYW1lOiBNeU1lc3NhZ2UKICAgICAgcGF5bG9hZDoKICAgICAgICAgICRpZDogJ2h0dHA6Ly9sb2NhbGhvc3QuY29tLycKICAgICAgICAgIHR5cGU6IG9iamVjdAogICAgICAgICAgcHJvcGVydGllczoKICAgICAgICAgICAgc2VudEF0OgogICAgICAgICAgICAgICRyZWY6ICIvY29tcG9uZW50cy9zY2hlbWFzL3NlbnRBdCI=) (it uses this parser library for resolving the references)

The library tries to resolve the reference at`https:///components/schemas/sentAt` when it should have tried to resolve it from `http://localhost.com/components/schemas/sentAt`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.