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

JsonbSerializer order priority when using inheritance / polymorphism #8925

Closed
benneq opened this issue Apr 28, 2020 · 4 comments
Closed

JsonbSerializer order priority when using inheritance / polymorphism #8925

benneq opened this issue Apr 28, 2020 · 4 comments
Assignees
Labels
kind/bug Something isn't working triage/out-of-date This issue/PR is no longer valid or relevant

Comments

@benneq
Copy link

benneq commented Apr 28, 2020

Describe the bug
When adding multiple JsonbSerializers of the same class hierarchy, the executed serializer is chosen randomly (can change with each application restart).

Expected behavior
Have a JsonbSerializer hierarchy which prioritizes the most precise matching class. So if we have a FooSerializer for Foo and a BarSerializer for Bar extends Foo, then serializing Bar should always prefer BarSerializer, because it's the most exact match in the class hierarchy.

Actual behavior
It's totally random.

To Reproduce
Steps to reproduce the behavior:

public class Foo { }

public class Bar extends Foo { }

public class FooSerializer implements JsonbSerializer<Foo> {
  public void serialize(Foo obj, JsonGenerator generator, SerializationContext ctx) {
    System.out.println("FOO SERIALIZER");
    generator.write("foo");
  }
}

public class BarSerializer implements JsonbSerializer<Bar> {
  public void serialize(Bar obj, JsonGenerator generator, SerializationContext ctx) {
    System.out.println("BAR SERIALIZER");
    generator.write("bar");
  }
}

class MyJsonbConfigCustomizer implements JsonbConfigCustomizer {
  public void customize(JsonbConfig config) {
    config.withSerializers(new FooSerializer(), new BarSerializer());
  }
}

@Path("/test")
public class TestResource {
  @GET
  public Bar test() { ... }
}

Configuration

# empty

Environment (please complete the following information):

  • Output of uname -a or ver: macOS Catalina 10.15.4, Darwin Kernel Version 19.4.0
  • Output of java -version: 11.0.7
  • GraalVM version (if different from Java):
  • Quarkus version or git rev: 1.4.1
  • Build tool (ie. output of mvnw --version or gradlew --version): Maven 3.6.3

Additional context
I'm not sure if this really belongs to Quarkus, or it's better suited for RestEasy or Yasson Bug Tracker. Already created this issue at EclipseEE JsonB BugTracker: jakartaee/jsonb-api#234

@benneq benneq added the kind/bug Something isn't working label Apr 28, 2020
@gsmet
Copy link
Member

gsmet commented Apr 28, 2020

@aguibert how are the priorities supposed to be managed?

@aguibert
Copy link
Member

The spec doesn't say anything about a priority, but it should certainly be deterministic regardless of priorities. I agree with @benneq that the most specific serializer makes sense.

@gsmet go ahead and assign to me and I can investigate further

@aguibert
Copy link
Member

Fix has been delivered in Yasson here: eclipse-ee4j/yasson#409
It will be available in the next release

@geoand
Copy link
Contributor

geoand commented Aug 3, 2021

The release that includes the fix has since been included in Quarkus

@geoand geoand closed this as completed Aug 3, 2021
@geoand geoand added the triage/out-of-date This issue/PR is no longer valid or relevant label Aug 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Something isn't working triage/out-of-date This issue/PR is no longer valid or relevant
Projects
None yet
Development

No branches or pull requests

4 participants