Skip to content

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

Closed

Conversation

mduesterhoeft
Copy link
Contributor

This can be useful for extensions to reuse field type determination logic.

This can be useful for extensions to reuse field type determination logic.
@mduesterhoeft
Copy link
Contributor Author

mduesterhoeft commented Sep 6, 2018

@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 == to equals I would be happy to do this.

@wilkinsona
Copy link
Member

@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.

@mduesterhoeft
Copy link
Contributor Author

@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.

Copy link
Member

@wilkinsona wilkinsona left a 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.
Copy link
Member

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
*/
Copy link
Member

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) {
Copy link
Member

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(),
Copy link
Member

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.

@wilkinsona wilkinsona added this to the 2.0.3.RELEASE milestone Oct 3, 2018
@wilkinsona wilkinsona added the type: enhancement Enhancement that adds a new feature label Oct 3, 2018
Copy link
Member

@wilkinsona wilkinsona left a 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 {
Copy link
Member

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);
Copy link
Member

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) {
Copy link
Member

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() {
Copy link
Member

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.

@mduesterhoeft
Copy link
Contributor Author

@wilkinsona I refactored to be able to keep the separation of concerns of JsonContentHandler and JsonFieldTypeResolver. I now try to just use the existing code as much as possible. What do you think?

I have the feeling that now FieldTypeResolver is not an ideal name, because compared to JsonFieldTypeResolver it is doing something different. Should we still keep the name?

static ContentHandler forContent(byte[] content, MediaType contentType) {

try {
return new JsonContentHandler(content);
Copy link
Contributor Author

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.

@wilkinsona
Copy link
Member

Thanks for your patience, @mduesterhoeft.

I have the feeling that now FieldTypeResolver is not an ideal name, because compared to JsonFieldTypeResolver it is doing something different. Should we still keep the name?

I agree that it's not ideal. having a FieldTypeResolver interface that isn't implemented by JsonFieldTypeResolver does not feel right to me. What's actually exposed is the field type determination logic from the content handlers, rather than the type resolution logic from JsonFieldTypeResolver. As mentioned earlier, there's a subtle yet important difference between the two and I think it's important that different names are used to help to maintain that distinction.

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.

wilkinsona added a commit that referenced this pull request Nov 21, 2018
* 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
@wilkinsona
Copy link
Member

Thanks very much for your contribution, @mduesterhoeft. In the end I reworked JsonFieldTypeResolver to be something that just does discovery and renamed it accordingly. This meant that your proposal could drop on top of that pretty much unchanged. All the changes are now in master and will be in the 2.0.3 release.

@wilkinsona wilkinsona changed the title Add public FieldTypeResolver. Provide a public API for resolving the type of a field Nov 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement Enhancement that adds a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants