Type-safe record wrappers to serialize as XML Attributes
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 35/100
Piste de recherche
Commencez par l’exemple SerialDemo2 dans l’issue, en particulier XmlMapper.builder().build() et l’appel à mapper.writeValue. Examinez la manière dont les composants du record LocationID et Location sont traités lorsqu’ils sont marqués avec @JacksonXmlProperty(isAttribute = true). La tâche est terminée lorsque les records sérialisent leurs valeurs UUID comme des attributs XML avec les noms d’élément et d’attribut attendus.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Is your feature request related to a problem? Please describe.
The following example code uses a trivial wrapper record (LocationID) to allow some extra type-safety when working with elements declared in an existing schema:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import java.io.IOException;
import java.util.Objects;
import java.util.UUID;
public final class SerialDemo2
{
private SerialDemo2()
{
}
record LocationID(UUID id) {
LocationID {
Objects.requireNonNull(id, "id");
}
}
@JacksonXmlRootElement(namespace = "urn:com.io7m.cardant.inventory:1")
record Location(
@JsonProperty(required = true)
@JacksonXmlProperty(isAttribute = true, localName = "id")
LocationID id,
@JsonProperty(required = false)
@JacksonXmlProperty(isAttribute = true, localName = "parent")
LocationID parent
) {
Location {
Objects.requireNonNull(id, "id");
}
}
public static void main(
final String[] args)
throws IOException
{
final var mapper =
XmlMapper.builder()
.build();
System.out.println("Expected: ");
System.out.println();
System.out.println("""
<Location xmlns="urn:com.io7m.cardant.inventory:1"
id="6e3f4213-db36-4ea3-91ba-1ce6917cbcbb"
parent="265f34b3-8c86-4a1f-b23a-bb104238bfc6"/>
""");
System.out.println("Received: ");
System.out.println();
mapper.writeValue(
System.out,
new Location(
new LocationID(UUID.randomUUID()),
new LocationID(UUID.randomUUID())
)
);
System.out.println();
}
}
The output of the program ends up as:
Expected:
<Location xmlns="urn:com.io7m.cardant.inventory:1"
id="6e3f4213-db36-4ea3-91ba-1ce6917cbcbb"
parent="265f34b3-8c86-4a1f-b23a-bb104238bfc6"/>
Received:
<Location xmlns="urn:com.io7m.cardant.inventory:1"><id xmlns=""><id>c09fb552-e36c-4213-b56a-7729cd5b7999</id></id><parent xmlns=""><id>58c399ae-0256-4dbd-a00e-0c1c01189559</id></parent></Location>
The desired output can be produced with the following definitions instead:
final class CA1LocationID
{
private final UUID id;
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
public CA1LocationID(
final UUID inId)
{
this.id = Objects.requireNonNull(inId, "id");
}
@JsonValue
@Override
public UUID id()
{
return this.id;
}
}
But it's slightly unfortunate that the definitions have to be declared in this way, because we lose a lot of the nice properties of record classes in the process.
Describe the solution you'd like
I'd like to be able to use records instead of POJOs in the above example.
Usage example
See above. 🙂
Additional context
See this thread on the mailing list: https://groups.google.com/d/msgid/jackson-user/20210911172814.62fd2925%40sunflower.int.arc7.info
- Langage dominant
- Java
- Étoiles
- 636
- Forks
- 250
- Merge moyen
- 7 h 10 min
- PR mergées (30 j)
- 8
Préparer son environnement
Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de FasterXML/jackson-dataformat-xml
-
Difficulté 2/5 1-3 heures Accessibilité débutants 85/100
-
Difficulté 4/5 3-5 jours Accessibilité débutants 55/100
-
Difficulté 4/5 3-5 jours Accessibilité débutants 45/100
FasterXML/jackson-dataformat-xml#931 · 3 commentaires ·
-
XmlMapper with `FAIL_ON_UNKNOWN_PROPERTIES` fails on whitespace-only elementPeut-être pris @arimu1 l’a pris il y a 89 jours. Ouverteto-evaluate
Difficulté 3/5 1-2 jours Accessibilité débutants 68/100
-
3.x
Difficulté 4/5 3-5 jours Accessibilité débutants 25/100
FasterXML/jackson-dataformat-xml#787 · 1 commentaire ·
Toutes les issues de FasterXML/jackson-dataformat-xml
Issues similaires
-
Cleanup: restore the interrupt flag in AiFailureAnalyzerPeut-être pris @Rishankgupta08 l’a pris aujourd’hui. Ouvertegood first issue
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
seleniumboot/selenium-boot#258 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
dense-analysis/ale#5181 ·
-
bug subsystem:agent-harness triage
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
ShaftHQ/SHAFT_ENGINE#8043 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 88/100
johanhaleby/occurrent#1505 ·
Les mainteneurs répondent en général sous 1 jour
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
Les mainteneurs répondent en général sous 1 jour