asyncapi / parser-js

AsyncAPI parser for Javascript (browser-compatible too).
Apache License 2.0
119 stars 97 forks source link

Reference resolves to more than one schema #866

Closed nilkanth987 closed 5 months ago

nilkanth987 commented 1 year ago

Describe the bug

Referencing same object multiple times in the message with $id in the referencing object causing issue.

How to Reproduce

asyncapi: 2.6.0
info:
  title: Test API
  version: 0.0.21
defaultContentType: application/json
channels:
  /test:
    publish:
      message:
        $ref: "#/components/messages/testMessage"
components:
  messages:
    testMessage:
      name: testMessage
      payload:
        type: object
        $id: testMessage
        properties:
          location:
            type: object
            properties:
              from:
                $ref: "#/components/schemas/location"
              to:
                $ref: "#/components/schemas/location"
        examples:
          - location:
              from:
                $ref: "#/components/schemas/location/examples/0"
              to:
                $ref: "#/components/schemas/location/examples/0"
  schemas:
    location:
      type: object
      $id: location
      properties:
        lat:
          type: number
        lng:
          type: number
      examples:
        - lat: 101
          lng: 102

When you run the below cmd

asyncapi generate models typescript asyncapi.yml -o public/docs/generated/ts

It produces the below output

Input is not a correct AsyncAPI document so it cannot be processed.
asyncapi.yml
 27:13  error  asyncapi2-schema-examples  reference "location" resolves to more than one schema  components.messages.testMessage.payload.examples[0]
 42:11  error  asyncapi2-schema-examples  schema with key or id "location" already exists        components.schemas.location.examples[0]

✖ 2 problems (2 errors, 0 warnings, 0 infos, 0 hints)

_NOTE : When you remove $id from location schema it works but it will create model with AnonymousSchema_1 name._

Expected behavior

It should ignore the previous id and override the object

github-actions[bot] commented 1 year ago

Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Please check out our contributors guide and the instructions about a basic recommended setup useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

nilkanth987 commented 1 year ago

Update: If I comment the examples then it works. I think the examples parsing is causing issues.

jonaslagoni commented 1 year ago

You cannot use references for values in examples i.e. this is not valid:

    examples:
      - location:
          from:
            $ref: "#/components/schemas/location/examples/0"
          to:
            $ref: "#/components/schemas

You have to use static values 🙂

nilkanth987 commented 1 year ago

@jonaslagoni , Its very cumbersome to write same examples again. Also the asyncapi extension in the VS code accepts the code and displays properly.

Screenshot 2023-09-20 at 2 24 31 PM

Its working when I reference an object 1 time in the example but only when I reference the object twice in the examples it throws error.

jonaslagoni commented 1 year ago

Then the extension is bugged, it does not validate before resolving references.

I would highly recommend you to create a feature request in the spec 😄 Its not the first time hearing this use case, so its good one 👍

nilkanth987 commented 1 year ago

@jonaslagoni , I created a feature request, check and let me know if something is missing. Should I close this issue?

jonaslagoni commented 1 year ago

As its not an issue with Modelina but the spec itself, yes please do 🙂

nilkanth987 commented 1 year ago

@jonaslagoni , I checked by replacing the references with static values but it still gives me error. Here is the example

asyncapi: 2.6.0
info:
  title: Test API
  version: 0.0.21
defaultContentType: application/json
channels:
  /test:
    publish:
      message:
        $ref: "#/components/messages/testMessage"
components:
  messages:
    testMessage:
      name: testMessage
      payload:
        type: object
        $id: testMessage
        properties:
          location:
            type: object
            properties:
              from:
                $ref: "#/components/schemas/location"
              to:
                $ref: "#/components/schemas/location"
        examples:
          - location:
              from:
                lat: 101
                lng: 102
              to:
                lat: 101
                lng: 102
  schemas:
    location:
      type: object
      $id: location
      properties:
        lat:
          type: number
        lng:
          type: number
      examples:
        - lat: 101
          lng: 102

Here I have replaced the references in examples with the static values and I receive the below error

Input is not a correct AsyncAPI document so it cannot be processed.
asyncapi.yml
 27:13  error  asyncapi2-schema-examples  reference "location" resolves to more than one schema  components.messages.testMessage.payload.examples[0]
 44:11  error  asyncapi2-schema-examples  schema with key or id "location" already exists        components.schemas.location.examples[0]

If I remove the examples from location schema then only 1 error comes

Input is not a correct AsyncAPI document so it cannot be processed.
asyncapi.yml
 27:13  error  asyncapi2-schema-examples  reference "location" resolves to more than one schema  components.messages.testMessage.payload.examples[0]

I think there is some bug in example validation which throws error when the schema1 has multiple references to schema2. Let me know if you need more clarification.

jonaslagoni commented 1 year ago

Yep, but it's parser related 🙂 Moving the issue

github-actions[bot] commented 1 year ago

Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Please check out our contributors guide and the instructions about a basic recommended setup useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

github-actions[bot] commented 9 months ago

This issue has been automatically marked as stale because it has not had recent activity :sleeping:

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience :heart: