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

Exception when serializing with type via mapper.writerFor(type).write(...) #389

Closed
texhnolyzze opened this issue Mar 22, 2020 · 3 comments
Closed
Milestone

Comments

@texhnolyzze
Copy link

texhnolyzze commented Mar 22, 2020

Hello again!
We have problems with Jackson (2.97) again (not JSON, but XML). Please, tell me what am I doing wrong. Here is the simple code:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
    @JsonSubTypes.Type(value = ConcreteModel.class, name = "ConcreteModel")
})
public abstract class AbstractModel {
    
	@JacksonXmlProperty(isAttribute = true)
	public Long id;
	
	@JacksonXmlProperty(isAttribute = true)
	public String name;

}
@JacksonXmlRootElement(localName = "Concrete")
public class ConcreteModel extends AbstractModel {

	@JacksonXmlProperty(isAttribute = true)
	public String someAdditionalField;

}

I ommitted getters/setters.
Okay, now I'm doing such simple thing:

ConcreteModel concreteModel = new ConcreteModel();
concreteModel.id = 1L;
concreteModel.name = "Bob";
concreteModel.someAdditionalField = "...";
XmlMapper xmlMapper = getXmlMapper();
System.out.println(xmlMapper.writeValueAsString(concreteModel));

And it outputs:

<Concrete type="ConcreteModel" id="1" name="Bob" someAdditionalField="..."/>

Great!
Now I replaced the last line to:

System.out.println(xmlMapper.writerFor(xmlMapper.constructType(concreteModel.getClass())).writeValueAsString(concreteModel));

And I'm getting the error:

com.fasterxml.jackson.databind.JsonMappingException: No element/attribute name specified when trying to output element
	Suppressed: com.fasterxml.jackson.core.JsonGenerationException: Trying to write END_DOCUMENT when document has no root (ie. trying to output empty document).
		at com.fasterxml.jackson.dataformat.xml.util.StaxUtil.throwAsGenerationException(StaxUtil.java:47)
		at com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.close(ToXmlGenerator.java:1188)
		at com.fasterxml.jackson.databind.util.ClassUtil.closeOnFailAndThrowAsIOE(ClassUtil.java:488)
		at com.fasterxml.jackson.databind.ObjectWriter._configAndWriteValue(ObjectWriter.java:1122)
		... 32 more
	Caused by: javax.xml.stream.XMLStreamException: Trying to write END_DOCUMENT when document has no root (ie. trying to output empty document).
		at com.ctc.wstx.sw.BaseStreamWriter.throwOutputError(BaseStreamWriter.java:1584)
		at com.ctc.wstx.sw.BaseStreamWriter.reportNwfStructure(BaseStreamWriter.java:1613)
		at com.ctc.wstx.sw.BaseStreamWriter._finishDocument(BaseStreamWriter.java:1439)
		at com.ctc.wstx.sw.BaseStreamWriter.closeCompletely(BaseStreamWriter.java:921)
		at com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.close(ToXmlGenerator.java:1183)
		... 34 more
Caused by: java.lang.IllegalStateException: No element/attribute name specified when trying to output element
	at com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.handleMissingName(ToXmlGenerator.java:1218)
	at com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator._handleStartObject(ToXmlGenerator.java:533)
	at com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.writeStartObject(ToXmlGenerator.java:509)
	at com.fasterxml.jackson.core.base.GeneratorBase.writeStartObject(GeneratorBase.java:286)
	at com.fasterxml.jackson.core.JsonGenerator.writeTypePrefix(JsonGenerator.java:1459)
	at com.fasterxml.jackson.databind.jsontype.impl.TypeSerializerBase.writeTypePrefix(TypeSerializerBase.java:46)
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeWithType(BeanSerializerBase.java:600)
	at com.fasterxml.jackson.dataformat.xml.ser.XmlBeanSerializerBase.serializeWithType(XmlBeanSerializerBase.java:320)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializePolymorphic(DefaultSerializerProvider.java:452)
	... 34 more

Can you please tell me what is wrong?

@texhnolyzze texhnolyzze changed the title Exception when serializing with type via mapper.writerFor(type).write(...) Exception when serializing with type via mapper.writerFor(type).write(...) Mar 22, 2020
@texhnolyzze texhnolyzze changed the title Exception when serializing with type via mapper.writerFor(type).write(...) Exception when serializing with type via mapper.writerFor(type).write(...) Mar 22, 2020
@cowtowncoder
Copy link
Member

Hoping to look into this in near future; haven't had time yet unfortunately.

@cowtowncoder
Copy link
Member

Ok, I can reproduce this with 2.11. Very odd. Usage looks good and I am not sure why one would work other not... Will need to check deeper.

@cowtowncoder cowtowncoder added this to the 2, milestone May 22, 2020
@cowtowncoder cowtowncoder modified the milestones: 2,, 2.11.1 May 22, 2020
@cowtowncoder
Copy link
Member

Looks like XML module was missing 2 overrides for DefaultSerializerProvider, added for 2.11 branch, will be included in 2.11.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants