Skip to content

Commit

Permalink
Support renaming constructor arguments and choosing a different const…
Browse files Browse the repository at this point in the history
…ructor by mixins (#804)
  • Loading branch information
dstepanov authored Mar 27, 2024
1 parent 1bf3404 commit ca8f472
Show file tree
Hide file tree
Showing 12 changed files with 621 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,121 @@ import spock.lang.Unroll

class JsonPropertySpec extends JsonCompileSpec {

void "simple JsonCreator"() {
given:
def context = buildContext('example.Test', '''
package example;
import com.fasterxml.jackson.annotation.*;
@io.micronaut.serde.annotation.Serdeable
class Test {
public final String foo;
@JsonCreator
public Test(@JsonProperty("foo") String foo) {
this.foo = foo;
}
}
''')
def deserialized = jsonMapper.readValue('{"foo": "42"}', typeUnderTest)

expect:
deserialized.foo == "42"

cleanup:
context.close()
}

void "static JsonCreator"() {
given:
def context = buildContext('example.Test', '''
package example;
import com.fasterxml.jackson.annotation.*;
@io.micronaut.serde.annotation.Serdeable
class Test {
public final String foo;
private Test(@JsonProperty("foo") String foo) {
this.foo = foo;
}
@JsonCreator
public static Test creator(@JsonProperty("foo") String foo) {
return new Test(foo + "xyz");
}
}
''')
def deserialized = jsonMapper.readValue('{"foo": "42"}', typeUnderTest)

expect:
deserialized.foo == "42xyz"

cleanup:
context.close()
}

void "static JsonCreator on interface"() {
given:
def context = buildContext('example.Test', '''
package example;
import com.fasterxml.jackson.annotation.*;
@io.micronaut.serde.annotation.Serdeable
interface Test {
String getFoo();
@JsonCreator
static Test creator(@JsonProperty("foo") String foo) {
return (example.Test) () -> foo + "abc";
}
}
''')
def deserialized = jsonMapper.readValue('{"foo": "42"}', typeUnderTest)

expect:
deserialized.foo == "42abc"

cleanup:
context.close()
}

void "static JsonCreator(mode = JsonCreator.Mode.DELEGATING) on interface"() {
given:
def context = buildContext('example.Test', '''
package example;
import com.fasterxml.jackson.annotation.*;
import io.micronaut.serde.annotation.Serdeable;
@Serdeable
interface Test {
String getFoo();
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
static Test creator(Data data) {
return (example.Test) () -> data.foo() + "abc";
}
}
@Serdeable
record Data(String foo) {
}
''')
def deserialized = jsonMapper.readValue('{"foo": "42"}', typeUnderTest)
expect:
deserialized.foo == "42abc"
cleanup:
context.close()
}
void "test @JsonProperty.Access.WRITE_ONLY (set only) - records"() {
given:
def context = buildContext("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,60 @@ abstract class TestMixin {
context.close()
}

void "test mixin constructor with parameter renamed"() {
def context = buildContext('mixintest.Test','''
package mixintest;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
import io.micronaut.core.annotation.Introspected;
import io.micronaut.serde.annotation.SerdeImport;
import io.micronaut.http.HttpStatus;
import io.micronaut.serde.annotation.Serdeable;
import java.util.List;
@SerdeImport(
value = Test.class,
mixin = TestMixin.class
)
class TestImport {}
public class Test {
private int code;
Test(int code) {
this.code = code;
}
public int getCode() {
return code;
}
}
abstract class TestMixin {
@JsonCreator
TestMixin(@JsonProperty("customWrite") int code) {
}
@JsonProperty("customRead")
abstract int getCode();
}
''')
def impl = argumentOf(context, 'mixintest.Test')
def bean = impl.type.newInstance(200)

expect:
writeJson(jsonMapper, bean) == '{"customRead":200}'

def read = jsonMapper.readValue('{"customWrite":200}', typeUnderTest)
read.getCode() == 200

cleanup:
context.close()
}

void "test import with interface"() {
def context = buildContext('mixintest.HttpStatusInfo','''
package mixintest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,31 +169,6 @@ class Test {
context.close()
}

void "JsonCreator with single parameter of same name"() {
given:
def context = buildContext('example.Test', '''
package example;
import com.fasterxml.jackson.annotation.*;
@io.micronaut.serde.annotation.Serdeable
class Test {
public final String foo;
@JsonCreator
public Test(String foo) {
this.foo = foo;
}
}
''')
def deserialized = jsonMapper.readValue('{"foo": "42"}', typeUnderTest)

expect:
deserialized.foo == "42"

cleanup:
context.close()
}

@PendingFeature(reason = 'single-parameter json creator. Dont think we should support this, can be done with delegating mode for JsonCreator')
void "JsonCreator with single parameter of different name"() {
given:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2017-2021 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.serde.jackson.serdeimport;

import com.amazonaws.services.lambda.runtime.serialization.util.SerializeUtil;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.type.Argument;
import io.micronaut.serde.Decoder;
import io.micronaut.serde.Encoder;
import io.micronaut.serde.util.NullableSerde;
import jakarta.inject.Singleton;
import org.joda.time.DateTime;

import java.io.IOException;

@Singleton
@Requires(classes = DateTime.class)
public class JodaDateTimeSerde implements NullableSerde<DateTime> {

@Override
public void serialize(@NonNull Encoder encoder,
@NonNull EncoderContext context,
@NonNull Argument<? extends DateTime> type,
@NonNull DateTime value) throws IOException {
encoder.encodeString(SerializeUtil.serializeDateTime(value, getClass().getClassLoader()));
}

@Override
@NonNull
public DateTime deserializeNonNull(Decoder decoder, DecoderContext decoderContext, Argument<? super DateTime> type) throws IOException {
return SerializeUtil.deserializeDateTime(DateTime.class, decoder.decodeString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2017-2023 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.serde.jackson.serdeimport;

import com.amazonaws.lambda.thirdparty.com.fasterxml.jackson.annotation.JsonCreator;
import com.amazonaws.lambda.thirdparty.com.fasterxml.jackson.annotation.JsonGetter;
import com.amazonaws.lambda.thirdparty.com.fasterxml.jackson.annotation.JsonProperty;
import com.amazonaws.services.lambda.runtime.events.models.s3.S3EventNotification;
import io.micronaut.serde.annotation.SerdeImport;

@SerdeImport(value = S3EventNotification.ResponseElementsEntity.class, mixin = ResponseElementsEntitySerde.ResponseElementsEntityMixin.class)
public class ResponseElementsEntitySerde {
private static final String X_AMZ_ID_2 = "x-amz-id-2";
private static final String X_AMZ_REQUEST_ID = "x-amz-request-id";

static abstract class ResponseElementsEntityMixin {
@JsonGetter(X_AMZ_ID_2)
abstract String getxAmzId2();

@JsonGetter(X_AMZ_REQUEST_ID)
abstract String getxAmzRequestId();

@JsonCreator
ResponseElementsEntityMixin(@JsonProperty(X_AMZ_ID_2) String xAmzId2, @JsonProperty(X_AMZ_REQUEST_ID) String xAmzRequestId) {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2017-2023 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.serde.jackson.serdeimport;

import com.amazonaws.lambda.thirdparty.com.fasterxml.jackson.annotation.JsonProperty;
import com.amazonaws.services.lambda.runtime.events.models.s3.S3EventNotification;
import io.micronaut.serde.annotation.SerdeImport;

import java.util.List;

@SerdeImport(value = S3EventNotification.class, mixin = S3EventNotificationSerde.S3EventNotificationMixin.class)
@SerdeImport(value = S3EventNotification.S3Entity.class)
@SerdeImport(value = S3EventNotification.RequestParametersEntity.class)
@SerdeImport(value = S3EventNotification.S3BucketEntity.class)
@SerdeImport(value = S3EventNotification.UserIdentityEntity.class)
@SerdeImport(value = S3EventNotification.S3EventNotificationRecord.class)
public class S3EventNotificationSerde {
/**
* Records Mixin.
*/
public interface S3EventNotificationMixin {
/**
* @return Records.
*/
@JsonProperty("Records")
List<S3EventNotification.S3EventNotificationRecord> getRecords();
}
}
Loading

0 comments on commit ca8f472

Please sign in to comment.