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

Allow for mixins to skip validation #750

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.micronaut.serde.jackson.mixin;

import io.micronaut.serde.annotation.SerdeImport;

@SerdeImport(
value = Request.class,
mixin = RequestMixin.class
)
@SerdeImport(
value = Message.class,
mixin = MessageMixin.class
)
class AppImport {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.micronaut.serde.jackson.mixin;

import io.micronaut.serde.annotation.Serdeable;

@Serdeable
public record FooMessage<T extends Request>(
T payload
) implements Message<T> { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.micronaut.serde.jackson.mixin;

public interface Message<T extends Request> {
T payload();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.micronaut.serde.jackson.mixin;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.micronaut.serde.annotation.Serdeable;

@Serdeable(validate = false)
@JsonDeserialize(as = FooMessage.class)
public interface MessageMixin {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.micronaut.serde.jackson.mixin;

public record MyTestClass(
String name
) implements Request { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.micronaut.serde.jackson.mixin;

public interface Request {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.micronaut.serde.jackson.mixin;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "type"
)
@JsonSubTypes({
@JsonSubTypes.Type(MyTestClass.class),
})
public interface RequestMixin {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.micronaut.serde.jackson.mixin

import io.micronaut.context.ApplicationContext
import io.micronaut.serde.ObjectMapper
import spock.lang.Specification

class SerdeMixinSpec extends Specification {

void "should deserialize"() {
given:
def context = ApplicationContext.run()
expect:
def read = context.getBean(ObjectMapper).readValue('{"payload": {"type": "MyTestClass", "name": "Some name"}}', FooMessage)

read.getClass().name.endsWith 'FooMessage'
read.payload().getClass().name.endsWith 'MyTestClass'
read.payload().name() == 'Some name'

cleanup:
context.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -727,15 +727,15 @@ private void visitClassInternal(ClassElement element, VisitorContext context, bo
String serializeAs = declaredAnnotation.stringValue(SerdeConfig.SERIALIZE_AS).orElse(null);
if (serializeAs != null) {
ClassElement thatType = context.getClassElement(serializeAs).orElse(null);
if (thatType != null && !thatType.isAssignable(element)) {
if (thatType != null && !thatType.isAssignable(element) && failOnError) {
throw new ProcessingException(element, "Type to serialize as [" + serializeAs + "], must be a subtype of the annotated type: " + element.getName());
}
}

String deserializeAs = declaredAnnotation.stringValue(SerdeConfig.DESERIALIZE_AS).orElse(null);
if (deserializeAs != null) {
ClassElement thatType = context.getClassElement(deserializeAs).orElse(null);
if (thatType != null && !thatType.isAssignable(element)) {
if (thatType != null && !thatType.isAssignable(element) && failOnError) {
throw new ProcessingException(element, "Type to deserialize as [" + deserializeAs + "], must be a subtype of the annotated type: " + element.getName());
}
}
Expand Down Expand Up @@ -1065,7 +1065,10 @@ private void handleJsonIgnoreType(VisitorContext context, TypedElement beanPrope

private void resetForNewClass(ClassElement element) {
this.currentClass = element;
this.failOnError = element.booleanValue(SerdeConfig.class, "validate").orElse(true);
// TODO: Investigate why the `AliasFor` doesn't work here
this.failOnError = element.booleanValue(SerdeConfig.class, SerdeConfig.VALIDATE)
.or(() -> element.booleanValue(Serdeable.class, SerdeConfig.VALIDATE))
.orElse(true);
this.creatorMode = SerdeConfig.SerCreatorMode.PROPERTIES;
this.anyGetterMethod = null;
this.anySetterMethod = null;
Expand Down
Loading