Skip to content

Commit

Permalink
[#606] test case missing type deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
bmarwell committed Jul 17, 2023
1 parent 9d3c4f3 commit 68872e3
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 6 deletions.
18 changes: 18 additions & 0 deletions src/test/java16/org/eclipse/yasson/records/CarId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2023, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

package org.eclipse.yasson.records;

import java.util.UUID;

record CarId(UUID value) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2023, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

package org.eclipse.yasson.records;

import jakarta.json.bind.annotation.JsonbProperty;
import jakarta.json.bind.annotation.JsonbTypeDeserializer;
import jakarta.json.bind.serializer.DeserializationContext;
import jakarta.json.bind.serializer.JsonbDeserializer;
import jakarta.json.stream.JsonParser;

import java.lang.reflect.Type;
import java.util.UUID;

public record CarWithUuidDeserializer(
@JsonbProperty("car.id") @JsonbTypeDeserializer(CarUuidDeserializer.class) CarId carId,
@JsonbProperty("colour") String colour
) {
public static class CarUuidDeserializer implements JsonbDeserializer<CarId> {

@Override
public CarId deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) {
String givenString = parser.getString();
var carUuid = UUID.fromString(givenString);

return new CarId(carUuid);
}
}
}


30 changes: 24 additions & 6 deletions src/test/java16/org/eclipse/yasson/records/RecordTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -19,6 +19,9 @@
import org.eclipse.yasson.internal.properties.Messages;
import org.junit.jupiter.api.Test;

import java.util.Locale;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

Expand Down Expand Up @@ -102,11 +105,26 @@ public void testRecordParameterOptionality() {
String json = Jsonbs.defaultJsonb.toJson(car);
assertEquals(expected, json);

String toDeserialize = "{}";
String expectedDefaultValues = "{\"color\":null,\"somePrimitive\":0,\"type\":\"typeDefaultValue\"}";
CarCreatorOptionalTest deserialized = Jsonbs.defaultJsonb.fromJson(toDeserialize, CarCreatorOptionalTest.class);
json = Jsonbs.nullableJsonb.toJson(deserialized);
assertEquals(expectedDefaultValues, json);
}

@Test
public void record_with_type_deserializer() {
UUID id = UUID.randomUUID();
String given = String.format(
Locale.ROOT,
"""
{
"car.id": "%s",
"colour": "green"
}
""",
id);

// when
CarWithUuidDeserializer car = Jsonbs.defaultJsonb.fromJson(given, CarWithUuidDeserializer.class);

// then
assertEquals(id, car.carId().value());
assertEquals("green", car.colour());
}
}

0 comments on commit 68872e3

Please sign in to comment.