Giter VIP home page Giter VIP logo

Comments (7)

patrickp-at-work avatar patrickp-at-work commented on August 15, 2024 1

Sure – I've done as proposed by you.

from java-spring-template.

github-actions avatar github-actions commented on August 15, 2024

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.

from java-spring-template.

derberg avatar derberg commented on August 15, 2024

@patrickp-at-work hey, please try adding the $id to schemas, to get the names of schemas that you want

from java-spring-template.

patrickp-at-work avatar patrickp-at-work commented on August 15, 2024

Thanks @derberg for your advice. I had never come across the $id syntax so far (probably because openapi 3.0 doesn't support it). I've tried $id and found that I can change the name of Pet ...

    Pet:
      type: object
      discriminator: petType
      $id: "Patty"
      properties:
        name:
          type: string
        petType:
          type: string
      required:
        - name
        - petType

... but adding it to an inline schema didn't work, despite trying for quite a while: Either still yielded "anonymous" or classes not being created at all.

    Cat:
      description: A representation of a cat
      $id: "Kitten"
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          properties:
            huntingSkill:
              type: string
              description: The measured skill for hunting
              enum:
                - clueless
                - lazy
                - adventurous
                - aggressive
          required:
            - huntingSkill

Do you have a working example? That would be really cool. Thanks in advance

from java-spring-template.

derberg avatar derberg commented on August 15, 2024

Tbh I prefer the 2nd example from your issue description, kind looks cleaner to me. To get what you want using $id you would have to do something like:

asyncapi: 2.0.0
info:
  title: Pet Service
  version: 1.0.0
  description: This service is in charge of processing pet updates
channels:
  sample/pet/updated/v1:
    publish:
      operationId: petUpdated
      summary: Pet Update
      description: Notify about Pet Update that has taken place
      message:
        $ref: '#/components/messages/PetNotificationMessage'
components:
  messages:
    PetNotificationMessage:
      headers: {}
      payload:
        $ref: '#/components/schemas/CatSchema'
  schemas:
    Pet:
      type: object
      discriminator: petType
      properties:
        name:
          type: string
        petType:
          type: string
      required:
        - name
        - petType
    CatSchema:  ## "Cat" will be used as the discriminator value
      description: A representation of a cat
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          $id: Cat
          properties:
            huntingSkill:
              type: string
              description: The measured skill for hunting
              enum:
                - clueless
                - lazy
                - adventurous
                - aggressive
          required:
            - huntingSkill
    DogSchema:  ## "Dog" will be used as the discriminator value
      description: A representation of a dog
      $id: Dogs
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          $id: Dog
          properties:
            packSize:
              type: integer
              format: int32
              description: the size of the pack the dog is from
              minimum: 0
          required:
            - packSize

Nevertheless, the code is not going to be what you expect, petType property will not be available in Cat and Dog classes.

I think this default behaviour is correct. As I understand you expect that in case of discriminator usage, it should be supported?

from java-spring-template.

patrickp-at-work avatar patrickp-at-work commented on August 15, 2024

First of all, thanks for the usage example. Secondly, thanks for the hint regarding the meaning of publish vs. subscribe in the linked issue.

the code is not going to be what you expect, petType property will not be available in Cat and Dog classes

Correct, that's what I would have expected; instead, it generates AllOfPetCat.
However, it solves the original "main" problem of this issue because there is no AnonymousSchema<X> any more.

I think this default behaviour is correct. As I understand you expect that in case of discriminator usage, it should be supported?

In fact, I rather think that the specification is unclear about that:

While composition offers model extensibility, it does not imply a hierarchy between the models.

I interpret that as a way of saying "code generators SHOULD NOT express it with inheritance".

There are are two ways to define the value of a discriminator for an inheriting instance.

That implies "When the discriminator is present, code generators MUST establish inheritance".

The spec then differentiates two examples.

  • The first one with the naming choice of ExtendedErrorModel looks like a perfect fit for composition / aggregation.
  • The second one with the Pet looks (at least to me) like a perfect fit for inheritance.

My assumption about inheritance is also re-enforced by the section that describes the discriminator:

The discriminator is the schema property name that is used to differentiate between other schema that inherit this schema. [...] When used, the value MUST be the name of this schema or any schema that inherits it.

But the spec doesn't give any hint how inheritance shall be represented, thus leaving room for interpretation (or confusion :) ).

from java-spring-template.

derberg avatar derberg commented on August 15, 2024

I think the best would be if you could open a separate issue related to discriminator in the AsyncAPI repo https://github.com/asyncapi/spec/issues

I have to admit I do not know much about this subject and better if it is reported in the repo where the spec is so a wider audience can join the discussion and drive some updates in the spec. Best is always if reporter can open a PR but first a discussion in the issue must take place.

I guess that this issue could be closed once you create the one in the spec repo?

from java-spring-template.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.