-
Notifications
You must be signed in to change notification settings - Fork 738
Provide a public API for resolving the type of a field #549
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2014-2018 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://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 org.springframework.restdocs.payload; | ||
|
||
import org.springframework.http.MediaType; | ||
|
||
/** | ||
* Public abstraction for external access to field type determination for xml and json | ||
* payloads. | ||
* | ||
* @author Mathias Düsterhöft | ||
* @since 2.0.3 | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is new public API so it should be annotated with |
||
public interface FieldTypeResolver { | ||
|
||
/** | ||
* Create a FieldTypeResolver for the given content and contentType. | ||
* @param content the payload that the {@link FieldTypeResolver} should handle | ||
* @param contentType the content type of the payload | ||
* @return the {@link FieldTypeResolver} | ||
*/ | ||
static FieldTypeResolver forContent(byte[] content, MediaType contentType) { | ||
return ContentHandler.forContent(content, contentType); | ||
} | ||
|
||
/** | ||
* Returns the type of the field that is described by the given | ||
* {@code fieldDescriptor} based on the content of the payload. | ||
* @param fieldDescriptor the field descriptor | ||
* @return the type of the field | ||
*/ | ||
Object resolveFieldType(FieldDescriptor fieldDescriptor); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,8 +32,9 @@ | |
* A {@link ContentHandler} for JSON content. | ||
* | ||
* @author Andy Wilkinson | ||
* @author Mathias Düsterhöft | ||
*/ | ||
class JsonContentHandler implements ContentHandler { | ||
class JsonContentHandler implements ContentHandler, FieldTypeResolver { | ||
|
||
private final JsonFieldProcessor fieldProcessor = new JsonFieldProcessor(); | ||
|
||
|
@@ -145,7 +146,7 @@ private boolean isEmpty(Object object) { | |
} | ||
|
||
@Override | ||
public Object determineFieldType(FieldDescriptor fieldDescriptor) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this logic should remain here. It's subtle, but there's a difference between resolving the type of a field (which is what the field type resolver does) and determining the type of a field. It's only the latter that considers the type that's specified on the descriptor and checks whether or not they match. Ideally the former would only use the path (it currently looks at whether the field is optional too). I'd like to keep the current level of separation. |
||
public Object resolveFieldType(FieldDescriptor fieldDescriptor) { | ||
if (fieldDescriptor.getType() == null) { | ||
return this.fieldTypeResolver.resolveFieldType(fieldDescriptor, | ||
readContent()); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2014-2018 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://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 org.springframework.restdocs.payload; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
import org.springframework.http.MediaType; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link FieldTypeResolver}. | ||
* | ||
* @author Mathias Düsterhöft | ||
*/ | ||
public class FieldTypeResolverTests { | ||
|
||
@Rule | ||
public ExpectedException thrownException = ExpectedException.none(); | ||
|
||
@Test | ||
public void returnJsonFieldTypeResolver() { | ||
assertThat(FieldTypeResolver.forContent("{\"field\": \"value\"}".getBytes(), | ||
MediaType.APPLICATION_JSON)).isInstanceOf(JsonContentHandler.class); | ||
} | ||
|
||
@Test | ||
public void returnXmlContentHandler() { | ||
assertThat(FieldTypeResolver.forContent("<a><b>5</b></a>".getBytes(), | ||
MediaType.APPLICATION_XML)).isInstanceOf(XmlContentHandler.class); | ||
} | ||
|
||
@Test | ||
public void throwOnInvalidContent() { | ||
this.thrownException.expect(PayloadHandlingException.class); | ||
FieldTypeResolver.forContent("some".getBytes(), MediaType.APPLICATION_XML); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wilkinsona I inlined the helper functions here because I could not keep them private in the interface. That is why I initially went for the factory class in the earlier version. This is now a little ugly. I could make
ContentHandler
an abstract class to keep the initial structure.