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

Correct validation for @Serializable(as = ...) and @Deserializable(as = ...) #651

Merged
merged 1 commit into from
Nov 7, 2023
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
Expand Up @@ -21,14 +21,14 @@ import io.micronaut.serde.annotation.SerdeImport;

@SerdeImport(
value = SQSEvent.class,
mixin = SQSEventMixin.class
mixin = SQSEventMixin.class
)
@SerdeImport(
value = SQSEvent.SQSMessage.class,
mixin = SQSEventMixin.SQSMessageMixin.class
mixin = SQSEventMixin.SQSMessageMixin.class
)
class AddMixin {

}
''')
def event = new SQSEvent()
Expand Down Expand Up @@ -78,7 +78,7 @@ public class Test implements HttpStatusInfo {
@Override public String name() {
return status.getReason();
}
@Override public int code() {
@Override public int code() {
return status.getCode();
}
}
Expand All @@ -89,7 +89,7 @@ abstract class TestMixin {
TestMixin(int code) {
this.status = HttpStatus.valueOf(code);
}

@JsonValue
abstract int code();
}
Expand Down Expand Up @@ -139,12 +139,13 @@ class Test implements HttpStatusInfo {
@Override public String name() {
return status.getReason();
}
@Override public int code() {
@Override public int code() {
return status.getCode();
}
}

@Serdeable.Deserializable(as = Another.class)
// Skip validation for this mixin: the deserializable-as cannot be validated for a mixin
@Serdeable.Deserializable(validate = false, as = Another.class)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The validation for the mixin needs to be disabled

interface TestMixin {
@JsonValue
int code();
Expand All @@ -160,7 +161,7 @@ class Another implements HttpStatusInfo {
@Override public String name() {
return status.getReason();
}
@Override public int code() {
@Override public int code() {
return status.getCode();
}
}
Expand Down Expand Up @@ -242,7 +243,7 @@ public class Test {
public String getName() {
return name;
}

public int getQuantity() {
return quantity;
}
Expand All @@ -251,7 +252,7 @@ public class Test {
abstract class TestMixin {
@JsonProperty("n")
abstract String getName();

@JsonProperty("qty")
abstract int getQuantity();
}
Expand Down Expand Up @@ -292,11 +293,11 @@ public class Test {
public String getName() {
return name;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}
Expand All @@ -305,10 +306,10 @@ public class Test {
abstract class TestMixin {
@JsonProperty("n")
private String name;

@JsonProperty("qty")
public abstract int getQuantity();

@JsonProperty("qty")
public abstract void setQuantity(int quantity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,6 @@ record Test(

then:
def e = thrown(RuntimeException)
e.message.contains("A method annotated with AnyGetter must return a Map")
e.message.contains("A field annotated with AnyGetter must be a Map")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactoring corrected the use-case

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@ import io.micronaut.serde.jackson.JsonCompileSpec

class JsonSerializeDeserializeSpec extends JsonCompileSpec {

void 'test errors'() {
when:
buildContext('test.Test', """
package test;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.micronaut.serde.annotation.Serdeable;
import java.util.LinkedList;
import java.util.List;
import java.time.LocalDate;

@Serdeable.Deserializable(as = LinkedList.class)
@Serdeable.Serializable(as = LocalDate.class)
public interface Test {}

""")
then:
def e = thrown(RuntimeException)
e.message.contains "Type to serialize as [java.time.LocalDate], must be a subtype of the annotated type: test.Test"
}

void 'test json deserialize on collection'() {
given:
def context = buildContext('test.Test', """
Expand Down
Loading
Loading