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

DATAREST-1326 - Fix ClassCastException JsonSchema for a list of Enums. #375

Open
wants to merge 2 commits into
base: main
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.data.rest.webmvc.json.JsonSchema.EnumProperty;
import org.springframework.data.rest.webmvc.json.JsonSchema.JsonSchemaProperty;
Expand All @@ -44,8 +46,9 @@
* Custom Spring Data REST Jackson serializers.
*
* @author Oliver Gierke
* @since 2.4
* @author Doug Busley
* @soundtrack Wallis Bird - I Could Be Your Man (Yeah! Wallis Bird Live 2007-2014)
* @since 2.4
*/
public class JacksonSerializers extends SimpleModule {

Expand Down Expand Up @@ -117,7 +120,14 @@ public JsonSchemaProperty customize(JsonSchemaProperty property, TypeInformation
values.add(translator.asText((Enum<?>) value));
}

return ((EnumProperty) property).withValues(values);
if (property instanceof EnumProperty) {
return ((EnumProperty) property).withValues(values);
}

property.items = Stream.of(new Object[][]{{"enum", values}, {"type", "string"}})
.collect(Collectors.toMap(data -> (String) data[0], data -> data[1]));

return property;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
*
* @author Jon Brisbin
* @author Oliver Gierke
* @author Doug Busley
*/
@JsonInclude(Include.NON_EMPTY)
public class JsonSchema {
Expand Down Expand Up @@ -333,7 +334,7 @@ public static class JsonSchemaProperty extends AbstractJsonSchemaProperty<JsonSc
public String pattern;
public Boolean uniqueItems;
public @JsonProperty("$ref") String reference;
public Map<String, String> items;
public Map<String, Object> items;

JsonSchemaProperty(String name, String title, String description, boolean required) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,23 @@

import static org.assertj.core.api.Assertions.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.StaticMessageSource;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.hateoas.mediatype.MessageResolver;

/**
* Unit tests for {@link EnumTranslator}.
*
* @author Oliver Gierke
* @author Doug Busley
*/
public class EnumTranslatorUnitTests {

Expand Down Expand Up @@ -145,6 +150,32 @@ public void doesNotResolveEnumNameAsFallbackIfConfigured() {
assertThat(configuration.fromText(MyEnum.class, "SECOND_VALUE")).isNull();
}

@Test // DATAREST-1326
@SuppressWarnings("unchecked")
public void doesNotFailOnEnumListsWithEnumTranslation() {
final TypeInformation<?> type = ClassTypeInformation.from(MyEnum.class);

JsonSchema.JsonSchemaProperty jsonSchemaProperty =
new JsonSchema.JsonSchemaProperty("foo", null, "bar", true)
.with(ClassTypeInformation.from(List.class));
JacksonSerializers.EnumTranslatingSerializer serializer = new
JacksonSerializers.EnumTranslatingSerializer(configuration);
JsonSchema.JsonSchemaProperty result = serializer.customize(jsonSchemaProperty, type);

ArrayList<String> enums = (ArrayList<String>) result.items.get("enum");

// Enum values are placed in the items object
assertThat(enums.size()).isEqualTo(2);
assertThat(enums.get(0)).isEqualTo("Translated");
assertThat(enums.get(1)).isEqualTo("Second value");

// type of the result object is array
assertThat(result.type).isEqualTo("array");

// type of the items object is string
assertThat(result.items.get("type")).isEqualTo("string");
}

static enum MyEnum {
FIRST_VALUE, SECOND_VALUE;
}
Expand Down