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

ObjectReader readValue lacks Class<T> argument #2636

Closed
robinroos opened this issue Mar 2, 2020 · 11 comments · Fixed by #2637
Closed

ObjectReader readValue lacks Class<T> argument #2636

robinroos opened this issue Mar 2, 2020 · 11 comments · Fixed by #2637
Milestone

Comments

@robinroos
Copy link
Contributor

Since ObjectMappers are mutable, I am exposing ObjectReader and ObjectWriter through a factory.

Whereas ObjectMapper.readValue(String content, Class valueType) is of known Type =valueType, the equivalent methods on ObjectReader are declared as returning T but do not take a Class valueType parameter.

Even ObjectReader.forType(Class<?> valueType) does not return a parameterized instance of ObjectReader.

The impact is that, whereas with ObjectMapper.readValue(String,Class) I do not have to typecast the returned value, when I start using ObjectReader.readValue(String) or ObjectReader.forType(Class).readValue(String) I must explicitly typecast the result.

Surely either ObjectReader.readValue should be overloaded with Class valueType (presumably this is trivially easy), or ObjectReader.forType(Class valueType) should return a fully parameterized ObjectReader (more complicated as ObjectReader is not a parameterized class)?

@robinroos
Copy link
Contributor Author

Some clarification is probably in order here.

The application is using Java 8.

The value resulting from ObjectReader.readValue() is not assigned to a variable, but passed to a method which is heavily overloaded.

@robinroos
Copy link
Contributor Author

I submitted a PR #2637 which adds the necessary method overloadings. See: #2637

@robinroos
Copy link
Contributor Author

As things stand I have (note the typecast):

return upstreamTransposer.transpose(
    (DriverDetails) downstreamReader.forType(DriverDetails.class).readValue(inputString)
);

With PR #2637 applied, I can then reduce my client code to:

return upstreamTransposer.transpose(
    downstreamReader.readValue(inputString, DriverDetails.class)
);

@robinroos
Copy link
Contributor Author

I added a Test class which covers all of the added method overloadings.

@robinroos
Copy link
Contributor Author

Noting that this just missed 2.10.3 (published yesterday), I'd appreciate it if the PR could form part of 2.10.4.

@cowtowncoder
Copy link
Member

@robinroos API changes must go in minor release, so changes would need to go in 2.11.0 anyway.

@robinroos
Copy link
Contributor Author

Ok.

@robinroos
Copy link
Contributor Author

Please add the 2.11.0 label if you are now happy for this to be included.

@cowtowncoder
Copy link
Member

Ok. I went back and forth with this a few times, but in the end I guess I can see the benefit, and that while it extends number of methods, it does not violate consistency -- partly due to existence of JsonParser taking variants.

I will merge this, make minor edits (add "since" tags), and it'll go in 2.11.0.rc1.

One question on testing: I can see why mocking is needed to get some coverage on, say, URL-taking version (without complexity from creating temporary file, URI for those etc). But I am not sure if that achieves much otherwise... since actual code is not called, right?
So just curious as to thoughts on that part.

@robinroos
Copy link
Contributor Author

Thanks @cowtowncoder.

The tests show that the overloaded methods do indeed invoke the appropriate non-overloaded method (c/o verify), and illustrate that the returned type does not additionally require type-casting.

The actual code (the pre-existing non-overloaded methods) is mocked.

@robinroos
Copy link
Contributor Author

In ObjectReaderTest, the added method testCanPassResultToOverloadedMethod() adds no value as it merely illustrates that javac can disambiguate based on <T> T.

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

Successfully merging a pull request may close this issue.

2 participants