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

feat: add subtype module (2.17) #229

Open
wants to merge 2 commits into
base: 2.16
Choose a base branch
from
Open

Conversation

black-06
Copy link

Adds a module that allows subtype to be registered without annotating the parent class.

It is implemented on SPI.

See FasterXML/jackson-databind#2104

Its original version is https://github.com/black-06/jackson-modules-dynamic-subtype.

I removed the custom ServiceLoader and always use the standard library,
which means that pojo always needs a no-parameter constructor.

@@ -33,6 +33,7 @@ not datatype, data format, or JAX-RS provider modules.
<module>mrbean</module>
<module>osgi</module>
<module>paranamer</module>
<module>subtype</module>
Copy link
Member

Choose a reason for hiding this comment

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

I'll probably want to change the name, "subtype" is too generic. But let me think about better name for a while...

Copy link
Author

Choose a reason for hiding this comment

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

It's a difficult thing for me to give it a name...

Copy link
Member

Choose a reason for hiding this comment

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

True🥲🥲

Registering modules.

```
ObjectMapper mapper = new ObjectMapper().registerModule(new DynamicSubtypeModule());
Copy link
Member

Choose a reason for hiding this comment

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

I think current name is SubtypeModule.

2. provide a non-argument constructor (SPI require it).

```java
import io.github.black.jackson.JsonSubType;
Copy link
Member

Choose a reason for hiding this comment

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

Needs to change to new package.

@RunWith(value = Parameterized.class)
public class WithJsonSubTypesTest<T extends WithJsonSubTypesTest.Parent> {

private final ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();
Copy link
Member

Choose a reason for hiding this comment

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

Let's not use this method but instead explicitly register module.

this.foo = foo;
}

@SuppressWarnings("unused") // jackson require it
Copy link
Member

Choose a reason for hiding this comment

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

We could instead just use public String foo to avoid having to add getter and setter

import static org.junit.Assert.assertTrue;

/**
* test work without {@link JsonSubTypes}
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure I understand -- why would we test functioning without module? And isn't module actually register with the mapper. Or what is being tested here? Could probably use longer explanation of reasoning.

Copy link
Member

Choose a reason for hiding this comment

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

Probably test against Jackson polymorphic type handling working as expected. If so, does not seem necessary

Copy link
Author

Choose a reason for hiding this comment

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

I actually wanted to test whether @JsonSubType worked with @JsonSubTypes

Comment on lines 53 to 54
List<NamedType> list1 = SubtypeModule.findSubtypes(a.getRawType(), a::getAnnotation);
List<NamedType> list2 = subtypes.getOrDefault(a.getRawType(), Collections.emptyList());
Copy link
Member

Choose a reason for hiding this comment

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

Can we rename these list1 and list2 to something less generic?

Copy link
Author

Choose a reason for hiding this comment

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

Comment on lines +56 to +57
if (list1.isEmpty()) return list2;
if (list2.isEmpty()) return list1;
Copy link
Member

Choose a reason for hiding this comment

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

If the two lists are null-safe, no need for empty checking.

Comment on lines +58 to +61
List<NamedType> list = new ArrayList<>(list1.size() + list2.size());
list.addAll(list1);
list.addAll(list2);
return list;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
List<NamedType> list = new ArrayList<>(list1.size() + list2.size());
list.addAll(list1);
list.addAll(list2);
return list;
List<NamedType> list = new ArrayList<>(list1.size() + list2.size());
list1.addAll(list2);
return list;

Comment on lines +58 to +61
List<NamedType> list = new ArrayList<>(list1.size() + list2.size());
list.addAll(list1);
list.addAll(list2);
return list;
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to have duplication? If so, can we have these as set first, then return as list?

import static org.junit.Assert.assertTrue;

/**
* test work without {@link JsonSubTypes}
Copy link
Member

Choose a reason for hiding this comment

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

Probably test against Jackson polymorphic type handling working as expected. If so, does not seem necessary

subtypes.remove(parent);
}

private static <S> List<NamedType> findSubtypes(Class<S> clazz, Function<Class<JsonSubType>, JsonSubType> getter) {
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason for keeping it static? if not maybe like...

Suggested change
private static <S> List<NamedType> findSubtypes(Class<S> clazz, Function<Class<JsonSubType>, JsonSubType> getter) {
private <S> List<NamedType> _findSubtypes(Class<S> clazz, Function<Class<JsonSubType>, JsonSubType> getter) {

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface JsonSubType {
Copy link
Member

Choose a reason for hiding this comment

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

Name sounds like it's part of @JsonSubTypes of annotations module. This may confuse users quite. If module name changes as per https://github.com/FasterXML/jackson-modules-base/pull/229/files#r1390329649, can we change name here also?

* more than one type name should be associated with the same type.
*
* @return subtype name array
* @since 2.12
Copy link
Member

Choose a reason for hiding this comment

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

Version mismatch, can remove.

Suggested change
* @since 2.12

... then add @since 2.16 to the class

Comment on lines 25 to 27
* Therefore, we can {@link #unregisterType} a class and then module will reload this class's subclasses.
*/
public class SubtypeModule extends Module {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Therefore, we can {@link #unregisterType} a class and then module will reload this class's subclasses.
*/
public class SubtypeModule extends Module {
* Therefore, we can {@link #unregisterType} a class and then module will reload this class's subclasses.
*
* @since 2.16
*/
public class SubtypeModule extends Module {

@cowtowncoder cowtowncoder changed the title feat: add subtype module feat: add subtype module (2.17) Nov 16, 2023
@cowtowncoder
Copy link
Member

Quick note: due to timing, decided that I will not try to get this in Jackson 2.16 -- so will need to be re-based to 2.17.
This because there's a bit of work to be done here.

For example, I think that naming of @JsonSubType should change as the general rule is that only jackson-annotations has annotations with name starting with @Json (and for legacy reasons, some in jackson-databind).
Modules can provide annotations that start with @Jackson prefix (or anything else that makes sense).

But I need to read the PR in bit more thought to make sure my feedback is relevant.

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

Successfully merging this pull request may close these issues.

3 participants