Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference resolves to more than one schema #866

Closed
nilkanth987 opened this issue Sep 19, 2023 · 11 comments
Closed

Reference resolves to more than one schema #866

nilkanth987 opened this issue Sep 19, 2023 · 11 comments
Labels
bug Something isn't working stale

Comments

@nilkanth987
Copy link

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 nilkanth987 added the bug Something isn't working label Sep 19, 2023
@github-actions
Copy link

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
Copy link
Author

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

@jonaslagoni
Copy link
Member

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
Copy link
Author

@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
Copy link
Member

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
Copy link
Author

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

@jonaslagoni
Copy link
Member

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

@nilkanth987
Copy link
Author

@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 nilkanth987 reopened this Sep 22, 2023
@jonaslagoni
Copy link
Member

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

@jonaslagoni jonaslagoni transferred this issue from asyncapi/modelina Oct 5, 2023
@github-actions
Copy link

github-actions bot commented Oct 5, 2023

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.

Copy link

github-actions bot commented Feb 3, 2024

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

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 ❤️

@github-actions github-actions bot added the stale label Feb 3, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working stale
Projects
None yet
Development

No branches or pull requests

2 participants