-
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
Provide a public API for resolving the type of a field #549
Conversation
This can be useful for extensions to reuse field type determination logic.
b319c32
to
abfe42f
Compare
@wilkinsona this is what we talked about in the chat the other day. Is that going into the right direction? 🤔 The Codacy complaint is not valid I think - and I just moved the code around. But if you want me to change the enum comparison from |
@mduesterhoeft Thanks for the PR and sorry for the annoyance with Codacy. It's been irritating me for a little while now. Your comment has given me the nudge that I needed to remove the integration. |
@wilkinsona no worries - I always struggle with myself if such code analysis is useful or not. I guess sometimes it is - sometimes it is rather annoying. |
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.
Thanks again the PR. On the whole, things look good. I left a handful of relatively minor comments. Could you take a look at those when you have a moment please?
@@ -0,0 +1,67 @@ | |||
/* | |||
* Copyright 2014-2016 the original author or authors. |
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.
This should be 2014-2018.
* payloads. | ||
* | ||
* @author Mathias Düsterhöft | ||
*/ |
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.
This is new public API so it should be annotated with @since
. I think I'd like to sneak this into 2.0.x, so it should be @since 2.0.3
.
* @param contentType the content type of the payload | ||
* @return the {@link FieldTypeResolver} | ||
*/ | ||
static FieldTypeResolver create(byte[] content, MediaType contentType) { |
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.
I think a name like forContent
may read a little better. What do you think?
|
||
@Test | ||
public void returnJsonFieldTypeResolver() { | ||
assertThat(FieldTypeResolver.create("{\"field\": \"value\"}".getBytes(), |
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.
Could you use ExpectedException
here please? I'd like to move to AssertJ's assertThatExceptionOfType
, but until that change is made across the whole codebase I'd prefer to make consistent use of ExceptedException
.
94337bf
to
fdbb513
Compare
fdbb513
to
61fe2fd
Compare
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.
Thanks again. Sorry, but in getting ready to merge this, I noticed that things aren't quite as close as I'd initially thought. I've left a few more comments. Could you take a look again when you have a moment please?
* | ||
* @author Mathias Düsterhöft | ||
*/ | ||
final class ContentTypeHandlerFactory { |
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.
I think this could be replaced with a static forContent
method on ContentHandler
.
* @param fieldDescriptor the field descriptor | ||
* @return the type of the field | ||
*/ | ||
Object determineFieldType(FieldDescriptor fieldDescriptor); |
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.
For consistency with the other Resolver
classes in REST Docs, this method should be named resolveFieldType
.
@@ -144,30 +147,4 @@ private boolean isEmpty(Object object) { | |||
return ((List<?>) object).isEmpty(); | |||
} | |||
|
|||
@Override | |||
public Object determineFieldType(FieldDescriptor fieldDescriptor) { |
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.
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 class JsonContentHandlerTests { | ||
|
||
@Rule | ||
public ExpectedException thrown = ExpectedException.none(); | ||
|
||
@Test | ||
public void typeForFieldWithNullValueMustMatch() { |
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.
Given my comment above, I think these tests should remain here.
d447fe0
to
aa2041a
Compare
aa2041a
to
bc96ffd
Compare
@wilkinsona I refactored to be able to keep the separation of concerns of I have the feeling that now |
static ContentHandler forContent(byte[] content, MediaType contentType) { | ||
|
||
try { | ||
return new JsonContentHandler(content); |
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.
Thanks for your patience, @mduesterhoeft.
I agree that it's not ideal. having a Looking back at the discussion in Gitter, it's the logic that's currently in the content handler that you're interested in, so what's exposed is what you want. However, the internal structure isn't quite right. I'll need to take another look at this to see how best it can be achieved. It may be that we need to take a different approach. |
* gh-549: Polish "Provide a public API for resolving the type of a field" Provide a public API for resolving the type of a field Rework JsonFieldTypeResolver to only discover the field types
Thanks very much for your contribution, @mduesterhoeft. In the end I reworked |
This can be useful for extensions to reuse field type determination logic.