Skip to content

[BUG] serialization of List of polymorphic models does not include the discriminator property #37793

Description

@weidongxu-microsoft

Describe the bug

Discussion here https://github.com/Azure/azure-sdk-for-java/pull/37789/files#r1402879273

The ChatMessageContentItem class is a polymorphic model. https://github.com/Azure/azure-sdk-for-java/blob/4cfe19f91ce3e42d9929555619e9094500195951/sdk/openai/azure-ai-openai/src/main/java/com/azure/ai/openai/models/ChatMessageContentItem.java

Cause in Jackson https://stackoverflow.com/questions/34193177/why-does-jackson-polymorphic-serialization-not-work-in-lists

The type erasure on Java generic makes Jackson not able to serialize the model correctly.

Candidate solutions:

  1. transfer to stream-style-serialization option, it won't solve Jackson, but it likely would correct the serialization as it uses azure-json
  2. modify the class, to always include the discriminator property (e.g. kind) in class variable (this be somewhat like having pass-discriminator-to-child-deserialization option; however it got another bug client, model serialized to JSON does not have discriminator property, in option "pass-discriminator-to-child-deserialization" autorest.java#2411)

Exception or Stack Trace
Add the exception log and stack trace if available

To Reproduce
Steps to reproduce the behavior:

Code Snippet
Add the code snippet that causes the issue.

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Setup (please complete the following information):

  • OS: [e.g. iOS]
  • IDE: [e.g. IntelliJ]
  • Library/Libraries: com.azure:azure-core:1.45.0
  • Java version: [e.g. 8]
  • App Server/Environment: [e.g. Tomcat, WildFly, Azure Function, Apache Spark, Databricks, IDE plugin or anything special]
  • Frameworks: [e.g. Spring Boot, Micronaut, Quarkus, etc]

If you suspect a dependency version mismatch (e.g. you see NoClassDefFoundError, NoSuchMethodError or similar), please check out Troubleshoot dependency version conflict article first. If it doesn't provide solution for the problem, please provide:

  • verbose dependency tree (mvn dependency:tree -Dverbose)
  • exception message, full stack trace, and any available logs

Additional context
Add any other context about the problem here.

Information Checklist
Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report

  • Bug Description Added
  • Repro Steps Added
  • Setup information Added

Metadata

Metadata

Assignees

Labels

Azure.Coreazure-coreClientThis issue points to a problem in the data-plane of the library.

Type

No type

Fields

No fields configured for issues without a type.

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions