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

discriminator field generated does not match asyncapi spec #160

Closed
TabraizChel opened this issue Mar 31, 2023 · 6 comments
Closed

discriminator field generated does not match asyncapi spec #160

TabraizChel opened this issue Mar 31, 2023 · 6 comments
Labels
bug Something isn't working staged for release

Comments

@TabraizChel
Copy link

TabraizChel commented Mar 31, 2023

Describe the bug
If an object has a polymorphic field when the json schema is generated for the object the discriminator property follows the openapi v3 convention (discriminator field is an object) whereas openapi spec wants the discriminator property as a string. This results in an invalid async api doc.

Dependencies and versions used
springwolf-kafka version 0.10.3.
spring-boot version 3.0.5.

Code example

package com.example.demo.consumer;


import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        property = "type"
)
@JsonSubTypes(value = {
        @JsonSubTypes.Type(value = Dog.class, name = "dog"),
        @JsonSubTypes.Type(value = Cat.class, name = "cat"),
})
public interface Pet {

    public String getType();
}

\\\\\\\\\\\\\\\\\\\\\\\\\\\

package com.example.demo.consumer;

public record Cat() implements Pet {
    @Override
    public String getType() {
        return "cat";
    }
}

\\\\\\\\\\\\\\\\\\\\\\\\\\\

package com.example.demo.consumer;

public record Dog() implements Pet {
    @Override
    public String getType() {
        return "dog";
    }
}

\\\\\\\\\\\\\\\\\\\\\\\\\\\

package com.example.demo.consumer;

public record MyEvent(
  Pet pet
)  {
}

\\\\\\\\\\\\\\\\\\\\\\\\\\\

package com.example.demo.consumer;

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class Listener {
    @KafkaListener(topics = "saas-bifrost-response")
    public void process(MyEvent event) {

    }
}

This results in a Pet portion of the schema being:

      "Pet" : {
        "type" : "object",
        "properties" : {
          "type" : {
            "type" : "string",
            "exampleSetFlag" : false,
            "types" : [ "string" ]
          }
        },
        "example" : {
          "type" : "string"
        },
        "discriminator" : {
          "propertyName" : "type"
        },
        "exampleSetFlag" : true
      }

note the discriminator field. For asyncapi spec this should just be:

"discriminator": "type"

See https://www.asyncapi.com/docs/reference/specification/v2.0.0#schemaComposition.

I think this issue stems from the fact that the SchemasService.getDefinitions() returns back a map of io.swagger.v3.oas.models.media.Schema but really it should be com.asyncapi.v2.model.schema.Schema. Given that the schemas are virtually identical i propose a some kind of translation service that maps from the io.swagger.v3.oas.models.media.Schema to the com.asyncapi.v2.model.schema.Schema so that we handle cases of divergence between the 2 specs such as this one

Stack trace and error logs
If an exception has been thrown or an error was logged by springwolf.

@ctasada
Copy link
Collaborator

ctasada commented May 12, 2023

I think this issue may be related with swagger-api/swagger-core#3411

Replacing io.swagger.v3.oas.models.media.Schema by com.asyncapi.v2.model.Schema doesn't seem to be a trivial change.

@timonback timonback moved this to Backlog in Springwolf Jun 26, 2023
@fcmdeveloper1
Copy link

is there a resolution or workaround for this case?

@fcmdeveloper1
Copy link

just a question, which version of this library can work with swagger version 2, what will be the generated asyncAPI version? it will not be 2.6.0, right?

@ctasada
Copy link
Collaborator

ctasada commented Jan 28, 2024

I did a quick test with the new version in master which generates AsyncAPI v3 and has many internal changes and the issue is still present. Once we finish the clean-ups we will take a look again on how to manage inheritance in a better way.

@timonback
Copy link
Member

The change has been merged and will be part of the next release.

If you want to try and verify it in your application, use the current SNAPSHOT build as described in our README.md

Thank you for the reporting this discrepancy and motivating to switch to custom AsyncApi specific models.

@timonback
Copy link
Member

Springwolf 1.0.0 is out, which addresses this issue.

Feel free to reopen in case you encounter any issues.

@github-project-automation github-project-automation bot moved this from Backlog to Done in Springwolf Mar 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 staged for release
Projects
Status: Done
Development

No branches or pull requests

4 participants