parser-js icon indicating copy to clipboard operation
parser-js copied to clipboard

Reference resolves to more than one schema

Open nilkanth987 opened this issue 10 months ago • 11 comments

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

nilkanth987 avatar Sep 19 '23 12:09 nilkanth987

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] avatar Sep 19 '23 12:09 github-actions[bot]

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

nilkanth987 avatar Sep 20 '23 06:09 nilkanth987

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 🙂

jonaslagoni avatar Sep 20 '23 08:09 jonaslagoni

@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.

nilkanth987 avatar Sep 20 '23 08:09 nilkanth987

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 👍

jonaslagoni avatar Sep 20 '23 18:09 jonaslagoni

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

nilkanth987 avatar Sep 21 '23 08:09 nilkanth987

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

jonaslagoni avatar Sep 21 '23 11:09 jonaslagoni

@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.

nilkanth987 avatar Sep 22 '23 09:09 nilkanth987

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

jonaslagoni avatar Oct 05 '23 12:10 jonaslagoni

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] avatar Oct 05 '23 12:10 github-actions[bot]

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:

github-actions[bot] avatar Feb 03 '24 00:02 github-actions[bot]