Skip to content

fix: recognize @XmlJavaTypeAdapter in JaxbConverterFactory#4702

Open
amitmishra11 wants to merge 1 commit into
lysine-dev:trunkfrom
amitmishra11:fix/jaxb-xml-java-type-adapter
Open

fix: recognize @XmlJavaTypeAdapter in JaxbConverterFactory#4702
amitmishra11 wants to merge 1 commit into
lysine-dev:trunkfrom
amitmishra11:fix/jaxb-xml-java-type-adapter

Conversation

@amitmishra11

Copy link
Copy Markdown

Summary

Fixes #3349

JaxbConverterFactory only checked for @XmlRootElement when deciding
whether to return a converter for a given type. Classes annotated with
@XmlJavaTypeAdapter (the standard JAXB pattern for handling immutable
types or types without a no-arg constructor) were not recognised, so the
factory returned null and Retrofit fell through to subsequent converter
factories or produced a misleading error.

The fix is minimal: extract an isJaxbAnnotated() predicate that
accepts a class if it carries either @XmlRootElement or
@XmlJavaTypeAdapter, and call it from both requestBodyConverter and
responseBodyConverter. The same change is applied to the jaxb3
(jakarta namespace) variant.

Changes

  • retrofit-converters/jaxb/src/main/java/.../JaxbConverterFactory.java
    • Import XmlJavaTypeAdapter
    • Add isJaxbAnnotated() helper
    • Use it in requestBodyConverter and responseBodyConverter
  • retrofit-converters/jaxb3/src/main/java/.../JaxbConverterFactory.java
    • Same changes, using jakarta.xml.bind imports
  • retrofit-converters/jaxb/src/test/java/.../ImmutablePoint.java (new)
    • Test fixture: immutable class with @XmlJavaTypeAdapter and no @XmlRootElement
  • retrofit-converters/jaxb/src/test/java/.../JaxbConverterFactoryTest.java
    • Two new tests asserting the factory returns a non-null converter for ImmutablePoint

Testing

./gradlew :retrofit-converters:jaxb:test passes (12 tests, 0 failures).

…axb and jaxb3

JaxbConverterFactory only checked for @XmlRootElement when deciding
whether to create a converter for a given type. Classes that are
annotated with @XmlJavaTypeAdapter (the JAXB pattern for immutable or
no-arg-constructor-free types) were not recognised, causing the factory
to return null and Retrofit to skip JAXB entirely for those types.

Add an isJaxbAnnotated() helper that accepts a class if it carries
either @XmlRootElement or @XmlJavaTypeAdapter, and update both the
javax.xml.bind (jaxb) and jakarta.xml.bind (jaxb3) factories to use
it.

Also add ImmutablePoint as a test fixture and two new tests to confirm
that the factory returns a non-null converter for a type bearing only
@XmlJavaTypeAdapter.

Fixes lysine-dev#3349
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

JaxbConverterFactory doesn't recognize @XmlJavaTypeAdapter

1 participant