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

[#606] test case missing type deserializer #608

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
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
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());
}
}
Loading