Skip to content

Commit

Permalink
Fix subtypes without properties (#622)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstepanov committed Oct 27, 2023
1 parent ef025fc commit bf0573e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -770,4 +770,47 @@ record TypeA (String a, InterfaceB b) { }
cleanup:
context.close()
}

void "test no properties subtype"() {
given:
def context = buildContext("""
package subtypes;
import com.fasterxml.jackson.annotation.*;
import io.micronaut.core.annotation.Introspected;
import io.micronaut.serde.annotation.Serdeable;
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "validation-type")
@JsonSubTypes({
@JsonSubTypes.Type(value = NonEmptyString.class, name = "NonEmptyString"),
})
interface Validator { }
@Serdeable
class NonEmptyString implements Validator {
}
""")

def argument = argumentOf(context, 'subtypes.NonEmptyString')

when:
def bean = newInstance(context, 'subtypes.NonEmptyString')
def json = writeJson(jsonMapper, bean)

then:
json == """{"validation-type":"NonEmptyString"}"""


when:
def deser = jsonMapper.readValue(json, argument)

then:
deser
argument.isInstance(deser)

cleanup:
context.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,12 @@ public int getOrder() {
initializers.add(ctx -> initProperty(ag, ctx));
}

if (!properties.isEmpty() || !jsonGetters.isEmpty()) {
AnnotationMetadata am = new AnnotationMetadataHierarchy(introspection, definition.getAnnotationMetadata());
Optional<String> subType = am.stringValue(SerdeConfig.class, SerdeConfig.TYPE_NAME);

if (!properties.isEmpty() || !jsonGetters.isEmpty() || subType.isPresent()) {
writeProperties = new ArrayList<>(properties.size() + jsonGetters.size());
AnnotationMetadata am = new AnnotationMetadataHierarchy(introspection, definition.getAnnotationMetadata());
am.stringValue(SerdeConfig.class, SerdeConfig.TYPE_NAME).ifPresent(typeName -> {
subType.ifPresent(typeName -> {
String typeProperty = am.stringValue(SerdeConfig.class, SerdeConfig.TYPE_PROPERTY).orElse(null);
if (typeProperty != null) {
SerProperty<T, String> prop;
Expand Down

0 comments on commit bf0573e

Please sign in to comment.