Skip to content

Commit 5479f2c

Browse files
Copilotslachiewicz
andcommitted
Fix Xpp3 reader and writer for simple type associations with multiplicity=1
Co-authored-by: slachiewicz <[email protected]>
1 parent 5e2e0d7 commit 5479f2c

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

modello-plugins/modello-plugin-xpp3/src/main/java/org/codehaus/modello/plugin/xpp3/Xpp3ReaderGenerator.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -656,12 +656,37 @@ private void processField(
656656
String associationName = association.getName();
657657

658658
if (association.isOneMultiplicity()) {
659-
sc.add(tagComparison);
659+
// Check if the association type is a class in the model or a simple type
660+
boolean inModel = isClassInModel(
661+
association.getTo(), field.getModelClass().getModel());
660662

661-
sc.add("{");
662-
sc.addIndented(objectName + ".set" + capFieldName + "( parse" + association.getTo() + "( parser, strict"
663-
+ trackingArgs + " ) );");
664-
sc.add("}");
663+
if (inModel) {
664+
// It's a model class, call the parse method for that class
665+
sc.add(tagComparison);
666+
667+
sc.add("{");
668+
sc.addIndented(objectName + ".set" + capFieldName + "( parse" + association.getTo()
669+
+ "( parser, strict" + trackingArgs + " ) );");
670+
sc.add("}");
671+
} else {
672+
// It's a simple type (like String), use primitive field handling
673+
sc.add(tagComparison);
674+
675+
sc.add("{");
676+
sc.indent();
677+
678+
writePrimitiveField(
679+
field,
680+
association.getTo(),
681+
objectName,
682+
objectName,
683+
"\"" + field.getName() + "\"",
684+
"set" + capFieldName,
685+
sc);
686+
687+
sc.unindent();
688+
sc.add("}");
689+
}
665690
} else {
666691
// MANY_MULTIPLICITY
667692

modello-plugins/modello-plugin-xpp3/src/main/java/org/codehaus/modello/plugin/xpp3/Xpp3WriterGenerator.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,31 @@ private void writeClass(ModelClass modelClass, JClass jClass) throws ModelloExce
329329
String associationName = association.getName();
330330

331331
if (association.isOneMultiplicity()) {
332-
sc.add(getValueChecker(type, value, association));
332+
// Check if the association type is a class in the model or a simple type
333+
boolean inModel = isClassInModel(
334+
association.getTo(), field.getModelClass().getModel());
333335

334-
sc.add("{");
335-
sc.addIndented("write" + association.getTo() + "( (" + association.getTo() + ") " + value + ", \""
336-
+ fieldTagName + "\", serializer );");
337-
sc.add("}");
336+
if (inModel) {
337+
// It's a model class, call the write method for that class
338+
sc.add(getValueChecker(type, value, association));
339+
340+
sc.add("{");
341+
sc.addIndented("write" + association.getTo() + "( (" + association.getTo() + ") " + value
342+
+ ", \"" + fieldTagName + "\", serializer );");
343+
sc.add("}");
344+
} else {
345+
// It's a simple type (like String), write it directly as a tag
346+
sc.add(getValueChecker(type, value, association));
347+
348+
sc.add("{");
349+
sc.addIndented("serializer.startTag( NAMESPACE, " + "\"" + fieldTagName + "\" ).text( "
350+
+ getValue(association.getTo(), value, xmlFieldMetadata) + " ).endTag( NAMESPACE, "
351+
+ "\"" + fieldTagName + "\" );");
352+
sc.indent();
353+
writeLocationTracking(sc, uncapClassName, '"' + fieldTagName + '"');
354+
sc.unindent();
355+
sc.add("}");
356+
}
338357
} else {
339358
// MANY_MULTIPLICITY
340359

0 commit comments

Comments
 (0)