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

readValue() for empty YAML returns null, not empty map #173

Open
damianszczepanik opened this issue Jan 12, 2020 · 6 comments
Open

readValue() for empty YAML returns null, not empty map #173

damianszczepanik opened this issue Jan 12, 2020 · 6 comments
Labels
yaml Issue related to YAML format backend

Comments

@damianszczepanik
Copy link

When passing empty YAML file into ObjectMapper.readValue() then null is returned, not empty map.
Example:
https://github.com/damianszczepanik/silencio/blob/master/src/main/java/pl/szczepanik/silencio/processors/YAMLProcessor.java#L44
Input file:
https://github.com/damianszczepanik/silencio/blob/master/src/test/resources/yaml/empty.yaml

When passing empty JSON {} this returns empty map so I would except to have same for YAML

@cowtowncoder cowtowncoder added yaml Issue related to YAML format backend 2.11 labels Jan 13, 2020
@cowtowncoder
Copy link
Member

Case of { } is little bit different, since that is explicit Object, whereas I am not even sure what empty content for YAML should be. But I can see how it could be interpreted as empty Document.
I wonder what SnakeYAML itself sees such content as.

But it is possible that existing Jackson YAML backend is handling it before. So I hope to look into this in near future to see what is going on.

@cowtowncoder
Copy link
Member

I am not sure there is something to change here: empty Map or POJO would be serialized as

--- {}

(or without document start indicator --- if so configured)

and empty content is parsed by SnakeYAML so that there is Stream start, Stream end events, but no Document events to indicate existence of document.
So to me returning null seems like the right thing.

@cowtowncoder cowtowncoder removed the 2.11 label Jul 17, 2020
@damianszczepanik
Copy link
Author

But then it should be consistent with JSON mapper which AFAIR is not

@cowtowncoder
Copy link
Member

@damianszczepanik How so? Passing empty file to JSON will not give you equivalent of { } but either exception (if trying to bind content into POJO), or null with streaming read using JsonParser.

@rehevkor5
Copy link

Appears to me it doesn't even give null, it throws an exception:

com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input
 at [Source: (StringReader); line: 1, column: 1]

	at [email protected]/com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
	at [email protected]/com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:4688)
	at [email protected]/com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4586)
	at [email protected]/com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3548)
	at [email protected]/com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3531)
	
...

Seems to me like an empty YAML should be valid.

@cowtowncoder
Copy link
Member

@rehevkor5 this is unfortunately quite tricky an area; but I would strongly suggest that anyone who needs to produce empty documents makes sure that YAML document start/separator marker (---) is used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
yaml Issue related to YAML format backend
Projects
None yet
Development

No branches or pull requests

3 participants