Skip to content

Commit

Permalink
Add tests for nested serialization / deserialization (#746)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstepanov authored Feb 1, 2024
1 parent 29047c1 commit 87cd922
Show file tree
Hide file tree
Showing 13 changed files with 216 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.micronaut.serde.bson

import io.micronaut.core.type.Argument
import io.micronaut.json.JsonMapper
import io.micronaut.serde.AbstractBasicSerdeCompileSpec
import io.micronaut.serde.AbstractBasicSerdeSpec
Expand All @@ -24,6 +25,11 @@ class BsonBinaryBasicSerdeCompileSpec extends AbstractBasicSerdeCompileSpec impl
return encodeAsBinaryDecodeJson(bean)
}

@Override
String writeJson(JsonMapper jsonMapper, Argument argument, Object bean) {
return encodeAsBinaryDecodeJson(argument, bean)
}

@Override
byte[] jsonAsBytes(String json) {
def parse = BsonDocument.parse(json)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.micronaut.serde.bson

import io.micronaut.core.type.Argument
import io.micronaut.json.JsonMapper
import io.micronaut.serde.AbstractBasicSerdeSpec
import io.micronaut.test.extensions.spock.annotation.MicronautTest
Expand All @@ -22,6 +23,11 @@ class BsonBinaryBasicSerdeSpec extends AbstractBasicSerdeSpec implements BsonBin
return encodeAsBinaryDecodeJson(bean)
}

@Override
String writeJson(JsonMapper jsonMapper, Argument argument, Object bean) {
return encodeAsBinaryDecodeJson(argument, bean)
}

@Override
byte[] jsonAsBytes(String json) {
def parse = BsonDocument.parse(json)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ trait BsonBinarySpec {
return readByteArrayAsJson(data)
}

String encodeAsBinaryDecodeJson(Argument argument, Object obj) {
def data = getBsonBinaryMapper().writeValueAsBytes(argument, obj)
return readByteArrayAsJson(data)
}

def <T> T encodeAsBinaryDecodeAsObject(BsonDocument ob, Class<T> type) {
def bytes = writeToByteArray(ob)
return bsonBinaryMapper.readValue(bytes, Argument.of(type))
Expand All @@ -45,4 +50,4 @@ trait BsonBinarySpec {
return document.toJson(JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).indentCharacters("").build())
}

}
}
2 changes: 2 additions & 0 deletions serde-jackson/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ dependencies {
testImplementation(libs.aws.lambda.events)
testImplementation(libs.micronaut.discovery)
testImplementation(projects.micronautSerdeJacksonTck)
testImplementation(mn.micronaut.http.client)
testImplementation(mn.micronaut.http.server.netty)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.micronaut.serde.jackson.annotation.http;

import io.micronaut.http.annotation.Get;
import io.micronaut.serde.ApiResponse;
import io.micronaut.serde.Dummy;

import java.util.List;

public interface Api {

@Get("/api/dummy/wrapped/list")
ApiResponse<List<Dummy>> wrappedList();

@Get("/api/dummy/wrapped/nested")
ApiResponse<ApiResponse<Dummy>> wrappedNested();

@Get("/api/dummy/wrapped/simple")
ApiResponse<Dummy> wrappedSimple();

@Get("/api/dummy/raw")
List<Dummy> simpleList();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.micronaut.serde.jackson.annotation.http;

import io.micronaut.http.client.annotation.Client;

@Client("/")
public interface ApiClient extends Api {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.micronaut.serde.jackson.annotation.http;

import io.micronaut.http.annotation.Controller;
import io.micronaut.serde.ApiResponse;
import io.micronaut.serde.Dummy;

import java.util.List;

@Controller
public class ApiController implements Api {

@Override
public ApiResponse<List<Dummy>> wrappedList() {
return new ApiResponse<>(
List.of(
new Dummy("test-1"),
new Dummy("test-2")
)
);
}

@Override
public ApiResponse<ApiResponse<Dummy>> wrappedNested() {
return new ApiResponse<>(
new ApiResponse<>(
new Dummy("test-1")
)
);
}

@Override
public ApiResponse<Dummy> wrappedSimple() {
return new ApiResponse<>(
new Dummy("test-1")
);
}

@Override
public List<Dummy> simpleList() {
return List.of(
new Dummy("test-1"),
new Dummy("test-2")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.micronaut.serde.jackson.annotation.http

import io.micronaut.context.ApplicationContext
import io.micronaut.http.client.annotation.Client
import io.micronaut.json.JsonMapper
import io.micronaut.serde.annotation.Serdeable
import io.micronaut.serde.jackson.JsonCompileSpec
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Specification

@MicronautTest
class ApiSpec extends Specification {

@Inject
ApiClient apiClient

void "test simpleList"() {
when:
var simpleList = apiClient.simpleList();
then:
simpleList[0].name() == "test-1"
simpleList[1].name() == "test-2"
}

void "test wrappedList"() {
when:
var wrappedList = apiClient.wrappedList();
then:
wrappedList.content()[0].name() == "test-1"
wrappedList.content()[1].name() == "test-2"
}

void "test wrappedNested"() {
when:
var wrappedNested = apiClient.wrappedNested();
then:
wrappedNested.content().content().name() == "test-1"
}

void "test wrappedSimple"() {
when:
var wrappedSimple = apiClient.wrappedSimple();
then:
wrappedSimple.content().name() == "test-1"
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ class OracleJdbcJsonBinaryBasicSerdeSpec extends AbstractBasicSerdeSpec {
return new String(textJsonMapper.writeValueAsBytes(object), StandardCharsets.UTF_8)
}

@Override
String writeJson(JsonMapper jsonMapper, Argument argument, Object bean) {
def bytes = jsonMapper.writeValueAsBytes(argument, bean)
def object = osonMapper.readValue(bytes, argument)
return new String(textJsonMapper.writeValueAsBytes(argument, object), StandardCharsets.UTF_8)
}

@Override
byte[] jsonAsBytes(String json) {
def object = textJsonMapper.readValue(json, OracleJsonObject)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ abstract class AbstractBasicSerdeSpec extends Specification implements JsonSpec,
jsonMatches(result, '{"name":"Test"}')
}

void "test nested"() {
when:
def bean = new ApiResponse(List.of(new Dummy("Xyz")))
def argument = Argument.of(ApiResponse, Argument.listOf(Dummy))
def result = writeJson(jsonMapper, argument, bean)

then:
jsonMatches(result, '{"content":[{"name":"Xyz"}]}')

when:
bean = jsonMapper.readValue(jsonAsBytes(result), argument)

then:
bean.content[0].name == "Xyz"
}

void "test read/write constructor args"() {
when:
def bean = new ConstructorArgs("test", 100)
Expand Down
5 changes: 5 additions & 0 deletions serde-tck/src/main/groovy/io/micronaut/serde/JsonSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
*/
package io.micronaut.serde

import io.micronaut.core.type.Argument
import io.micronaut.json.JsonMapper

trait JsonSpec {
String writeJson(JsonMapper jsonMapper, Object bean) {
new String(jsonMapper.writeValueAsBytes(bean))
}

String writeJson(JsonMapper jsonMapper, Argument argument, Object bean) {
new String(jsonMapper.writeValueAsBytes(argument, bean))
}
}
22 changes: 22 additions & 0 deletions serde-tck/src/main/java/io/micronaut/serde/ApiResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2017-2024 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;

import io.micronaut.serde.annotation.Serdeable;

@Serdeable
public record ApiResponse<T>(T content) {
}
22 changes: 22 additions & 0 deletions serde-tck/src/main/java/io/micronaut/serde/Dummy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2017-2024 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;

import io.micronaut.serde.annotation.Serdeable;

@Serdeable
public record Dummy(String name) {
}

0 comments on commit 87cd922

Please sign in to comment.