From b6854bbfb321b172e7d2e3fb690831bbe5252351 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 1 Jun 2015 21:17:40 +0200 Subject: [PATCH 1/6] Hacking. --- pom.xml | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/pom.xml b/pom.xml index 0571be5f4..98cca1080 100644 --- a/pom.xml +++ b/pom.xml @@ -59,12 +59,12 @@ UTF-8 4.0.9.RELEASE - 1.1.2 - 2.4.3 + 1.1.3 + 2.4.6 1.0 - 2.1.0 + 2.1.1 0.9.1 - 1.7.10 + 1.7.12 1.2.1 true 1.6 @@ -88,7 +88,7 @@ spring41 - 4.1.5.RELEASE + 4.1.6.RELEASE @@ -130,7 +130,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 2.10.1 + 2.10.3 @@ -167,7 +167,7 @@ org.springframework.data.build spring-data-build-resources - 1.5.2.RELEASE + 1.6.0.RELEASE provided zip @@ -305,7 +305,7 @@ maven-antrun-plugin - 1.7 + 1.8 @@ -355,7 +355,7 @@ org.apache.maven.plugins maven-assembly-plugin - 2.4 + 2.5.4 static @@ -377,7 +377,7 @@ org.codehaus.mojo wagon-maven-plugin - 1.0-beta-5 + 1.0 ${project.build.directory} @@ -593,7 +593,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.2 + 3.3 ${source.level} ${source.level} @@ -620,7 +620,7 @@ org.apache.maven.plugins maven-jar-plugin - 2.5 + 2.6 true @@ -643,7 +643,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 2.10.1 + 2.10.3 true
${project.name}
@@ -661,7 +661,7 @@ org.apache.maven.plugins maven-deploy-plugin - 2.8.1 + 2.8.2 true From 240e676e06b7a4381b4b45ad0a744b9bba2056c3 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 19 Jun 2015 16:52:58 +0200 Subject: [PATCH 2/6] #360 - Tweaked TypeConstrainedMappingJackson2HttpMessageConverter to reject all types not matching the configured one. We now also override canRead(Type, Class, MediaType) to make sure we can opt out of being used for generic type matching. --- ...edMappingJackson2HttpMessageConverter.java | 22 ++++++++++++++++--- ...gJackson2HttpMessageConverterUnitTest.java | 15 ++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverter.java b/src/main/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverter.java index 6c8373767..06c4aa2d5 100644 --- a/src/main/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverter.java +++ b/src/main/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,6 +15,8 @@ */ package org.springframework.hateoas.mvc; +import java.lang.reflect.Type; + import org.springframework.http.MediaType; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.util.Assert; @@ -24,7 +26,7 @@ /** * Extension of {@link MappingJackson2HttpMessageConverter} to constrain the ability to read and write HTTP message * based on the target type. Useful in case the {@link ObjectMapper} about to be configured has customizations that - * sholny be applied to object trees of a certain base type. + * shall only be applied to object trees of a certain base type. * * @author Oliver Gierke */ @@ -49,7 +51,21 @@ public TypeConstrainedMappingJackson2HttpMessageConverter(Class type) { */ @Override public boolean canRead(Class clazz, MediaType mediaType) { - return type.isAssignableFrom(clazz) && super.canRead(clazz, mediaType); + return type.isAssignableFrom(clazz) && super.canRead(mediaType); + } + + /* + * (non-Javadoc) + * @see org.springframework.http.converter.json.MappingJackson2HttpMessageConverter#canRead(java.lang.reflect.Type, java.lang.Class, org.springframework.http.MediaType) + */ + @Override + public boolean canRead(Type type, Class contextClass, MediaType mediaType) { + + if (type instanceof Class) { + return canRead((Class) type, mediaType); + } + + return super.canRead(type, contextClass, mediaType); } /* diff --git a/src/test/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverterUnitTest.java b/src/test/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverterUnitTest.java index 636fe979b..5c3698ffa 100644 --- a/src/test/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverterUnitTest.java +++ b/src/test/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverterUnitTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,7 @@ import org.springframework.hateoas.Resource; import org.springframework.hateoas.ResourceSupport; import org.springframework.http.MediaType; +import org.springframework.http.converter.GenericHttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter; /** @@ -66,4 +67,16 @@ public void canWriteTypeIfAssignableToConfiguredType() { assertThat(converter.canWrite(ResourceSupport.class, MediaType.APPLICATION_JSON), is(true)); assertThat(converter.canWrite(Resource.class, MediaType.APPLICATION_JSON), is(true)); } + + /** + * @see #360 + */ + @Test + public void doesNotSupportAnythingButTheConfiguredClassForCanReadWithContextClass() { + + GenericHttpMessageConverter converter = new TypeConstrainedMappingJackson2HttpMessageConverter( + ResourceSupport.class); + + assertThat(converter.canRead(String.class, Object.class, MediaType.APPLICATION_JSON), is(false)); + } } From a4ec1027687c74307c5888a835f8d67efccbd7ec Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 24 Jun 2015 17:22:02 +0200 Subject: [PATCH 3/6] #360 - Improved TypeConstrainedHttpMessageConverter on generics matching. We now locally resolve the generic type using Jackson's JavaType in canRead(Type, Class, MediaType) to avoid having to call the non-generic method as this caused us having to tweak it's implementation to avoid the super.canRead(Class, MediaType) check as this in turn forwards to the generic method (which then would've caused a stack overflow. --- ...ConstrainedMappingJackson2HttpMessageConverter.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverter.java b/src/main/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverter.java index 06c4aa2d5..1f1e9ba4e 100644 --- a/src/main/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverter.java +++ b/src/main/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverter.java @@ -51,7 +51,7 @@ public TypeConstrainedMappingJackson2HttpMessageConverter(Class type) { */ @Override public boolean canRead(Class clazz, MediaType mediaType) { - return type.isAssignableFrom(clazz) && super.canRead(mediaType); + return type.isAssignableFrom(clazz) && super.canRead(clazz, mediaType); } /* @@ -60,12 +60,8 @@ public boolean canRead(Class clazz, MediaType mediaType) { */ @Override public boolean canRead(Type type, Class contextClass, MediaType mediaType) { - - if (type instanceof Class) { - return canRead((Class) type, mediaType); - } - - return super.canRead(type, contextClass, mediaType); + return this.type.isAssignableFrom(getJavaType(type, contextClass).getRawClass()) + && super.canRead(type, contextClass, mediaType); } /* From 4dff808b9dc461a4eaa23418741d64688b60ffe4 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 25 Jun 2015 16:09:04 +0200 Subject: [PATCH 4/6] #360 - Improved test cases for TypeConstrainedHttpMessageConverter. --- ...gJackson2HttpMessageConverterUnitTest.java | 39 +++++++++---------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/test/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverterUnitTest.java b/src/test/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverterUnitTest.java index 5c3698ffa..df1aab341 100644 --- a/src/test/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverterUnitTest.java +++ b/src/test/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverterUnitTest.java @@ -17,13 +17,12 @@ import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; +import static org.springframework.http.MediaType.*; import org.junit.Test; import org.springframework.hateoas.Resource; import org.springframework.hateoas.ResourceSupport; -import org.springframework.http.MediaType; import org.springframework.http.converter.GenericHttpMessageConverter; -import org.springframework.http.converter.HttpMessageConverter; /** * Unit tests for {@link TypeConstrainedMappingJackson2HttpMessageConverter}. @@ -41,42 +40,42 @@ public void rejectsNullType() { } /** - * @see #219 + * @see #219, #360 */ @Test public void canReadTypeIfAssignableToConfiguredType() { - HttpMessageConverter converter = new TypeConstrainedMappingJackson2HttpMessageConverter( + GenericHttpMessageConverter converter = new TypeConstrainedMappingJackson2HttpMessageConverter( ResourceSupport.class); - assertThat(converter.canRead(Object.class, MediaType.APPLICATION_JSON), is(false)); - assertThat(converter.canRead(ResourceSupport.class, MediaType.APPLICATION_JSON), is(true)); - assertThat(converter.canRead(Resource.class, MediaType.APPLICATION_JSON), is(true)); + assertCanRead(converter, Object.class, false); + assertCanRead(converter, ResourceSupport.class, true); + assertCanRead(converter, Resource.class, true); } /** - * @see #219 + * @see #219, #360 */ @Test public void canWriteTypeIfAssignableToConfiguredType() { - HttpMessageConverter converter = new TypeConstrainedMappingJackson2HttpMessageConverter( + GenericHttpMessageConverter converter = new TypeConstrainedMappingJackson2HttpMessageConverter( ResourceSupport.class); - assertThat(converter.canWrite(Object.class, MediaType.APPLICATION_JSON), is(false)); - assertThat(converter.canWrite(ResourceSupport.class, MediaType.APPLICATION_JSON), is(true)); - assertThat(converter.canWrite(Resource.class, MediaType.APPLICATION_JSON), is(true)); + assertCanWrite(converter, Object.class, false); + assertCanWrite(converter, ResourceSupport.class, true); + assertCanWrite(converter, Resource.class, true); } - /** - * @see #360 - */ - @Test - public void doesNotSupportAnythingButTheConfiguredClassForCanReadWithContextClass() { + private static void assertCanRead(GenericHttpMessageConverter converter, Class type, boolean expected) { - GenericHttpMessageConverter converter = new TypeConstrainedMappingJackson2HttpMessageConverter( - ResourceSupport.class); + assertThat(converter.canRead(type, APPLICATION_JSON), is(expected)); + assertThat(converter.canRead(type, type, APPLICATION_JSON), is(expected)); + } + + private static void assertCanWrite(GenericHttpMessageConverter converter, Class type, boolean expected) { - assertThat(converter.canRead(String.class, Object.class, MediaType.APPLICATION_JSON), is(false)); + assertThat(converter.canWrite(type, APPLICATION_JSON), is(expected)); + assertThat(converter.canWrite(type, type, APPLICATION_JSON), is(expected)); } } From 67cc7ab7c24c319133aec491ad4bd3d38499d9e7 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 26 Jun 2015 08:50:30 +0200 Subject: [PATCH 5/6] #360 - Removed test call to method only available on Spring 4.2. --- ...peConstrainedMappingJackson2HttpMessageConverterUnitTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverterUnitTest.java b/src/test/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverterUnitTest.java index df1aab341..13e0481cb 100644 --- a/src/test/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverterUnitTest.java +++ b/src/test/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverterUnitTest.java @@ -76,6 +76,5 @@ private static void assertCanRead(GenericHttpMessageConverter converter, private static void assertCanWrite(GenericHttpMessageConverter converter, Class type, boolean expected) { assertThat(converter.canWrite(type, APPLICATION_JSON), is(expected)); - assertThat(converter.canWrite(type, type, APPLICATION_JSON), is(expected)); } } From 4e2220a29614c7870eabc9adfd6ae8eae62428f6 Mon Sep 17 00:00:00 2001 From: Angel Aguilera Date: Mon, 13 Jul 2015 11:26:27 +0200 Subject: [PATCH 6/6] Add missing parentheses --- src/main/asciidoc/index.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/asciidoc/index.adoc b/src/main/asciidoc/index.adoc index 5a73e433e..d0f50efab 100644 --- a/src/main/asciidoc/index.adoc +++ b/src/main/asciidoc/index.adoc @@ -87,7 +87,7 @@ You can also easily access links contained in that resource: [source, java] ---- Link selfLink = new Link("http://myhost/people"); -assertThat(resource.getId(), is(selfLink); +assertThat(resource.getId(), is(selfLink)); assertThat(resource.getLink(Link.SELF), is(selfLink)); ---- [[fundamentals.obtaining-links]]