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

Fix JSON sources to convert to Lists and other types #664

Merged
merged 14 commits into from Sep 26, 2022

Conversation

Michael1993
Copy link
Member

@Michael1993 Michael1993 commented Sep 1, 2022

Closes #654

Proposed commit message:

Fix JSON sources to convert to Java types (#654 / #664)

The initial implementation of the JSON sources for ParameterizedTest
was missing the functionality to map from JSON to more complex Java
types like List. This commit fixes that by using Jackson's
ObjectMapper and JavaType.

Closes: #654 
PR: #664

PR checklist

The following checklist shall help the PR's author, the reviewers and maintainers to ensure the quality of this project.
It is based on our contributors guidelines, especially the "writing code" section.
It shall help to check for completion of the listed points.
If a point does not apply to the given PR's changes, the corresponding entry can be simply marked as done.

Documentation (general)

  • There is documentation (Javadoc and site documentation; added or updated)
  • There is implementation information to describe why a non-obvious source code / solution got implemented
  • Site documentation has its own .adoc file in the docs folder, e.g. docs/report-entries.adoc
  • Site documentation in .adoc file references demo in src/demo/java instead of containing code blocks as text
  • Only one sentence per line (especially in .adoc files)
  • Javadoc uses formal style, while sites documentation may use informal style

Documentation (new extension)

  • The docs/docs-nav.yml navigation has an entry for the new extension
  • The package-info.java contains information about the new extension

Code

  • Code adheres to code style, naming conventions etc.
  • Successful tests cover all changes
  • There are checks which validate correct / false usage / configuration of a functionality and there are tests to verify those checks
  • Tests use AssertJ or our own PioneerAssert (which are based on AssertJ)

Contributing

  • A prepared commit message exists
  • The list of contributions inside README.md mentions the new contribution (real name optional)

Copy link
Contributor

@filiphr filiphr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for intruding, but since I was working on the Json support I wanted to chime in.

I believe that this approach is a bit strict and it has a dependency on Jackson.


Jackson has support for converting to a generic type with the help of its JavaType. This JavaType can be constructed from a java.lang.reflect.Type.

This means that new methods (or the existing methods) of the Node can look like:

<T> T toType(Type type);
Object value(Type typeHint);

Everything in the end delegates to toType(Type) which is implemented in the following way:

@Override
public <T> T toType(Type type) {
	try {
		return objectMapper.treeToValue(node, objectMapper.constructType(type));
	}
	catch (JsonProcessingException e) {
		throw new UncheckedIOException("Failed to convert to type " + type, e);
	}
}

As a final step all the places that were invoking the toType and value methods can be changed from doing Parameter#getType to Parameter#getParameterizedType.

There won't be a need to have a custom argument converter and it would be possible to map into things like Map<String, Poem> or List<Map<String, Poem>> etc.

@Michael1993 Michael1993 changed the title Add converter for List items Fix Json sources to convert to Lists and other types Sep 2, 2022
@Michael1993 Michael1993 changed the title Fix Json sources to convert to Lists and other types Fix JSON sources to convert to Lists and other types Sep 2, 2022
Copy link
Contributor

@filiphr filiphr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation looks as I would expect now @Michael1993.

I've left some comments about some more general confusions I have.

One thing that you should decide within the team is the breaking change in Node.java. From my point of view it is fine to do that change, since this is a new interface and I really doubt that someone has implemented an entire new JSON parsing on top of JUnit Pioneer without contributing it back.

@Michael1993
Copy link
Member Author

Michael1993 commented Sep 2, 2022

One thing that you should decide within the team is the breaking change in Node.java.

This PR contains no breaking changes as all classes/interfaces modified are package-private.

@filiphr
Copy link
Contributor

filiphr commented Sep 2, 2022

This PR contains no breaking changes as all classes/interfaces modified are package-private.

I didn't notice that. They used to be public when I wrote them initially

Copy link
Member

@Bukama Bukama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I understand all those reflection things it looks good to me, but I mostly trust @filiphr. When I have a look at the documentation I think there's no need in updating it. But please have a second look at it @Michael1993

@Bukama
Copy link
Member

Bukama commented Sep 6, 2022

Adding merge-ready to trigger full build

Copy link
Member

@beatngu13 beatngu13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot Michael for working on this!

I only have two "take it or leave it" comments. In addition, do you think it make sense to add an example to the docs/demos?

@Michael1993 Michael1993 merged commit d9e926e into main Sep 26, 2022
@Michael1993 Michael1993 deleted the issue/654-json-node-to-list branch September 26, 2022 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
full-build Triggers full build suite on PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Convert ArrayNode to List
5 participants