Skip to content

Fix REST API response of methods with String return type - #754

Merged
asfgit merged 3 commits into
apache:masterfrom
ivanayov:fix/rest-api-empty-response
Jul 10, 2017
Merged

Fix REST API response of methods with String return type#754
asfgit merged 3 commits into
apache:masterfrom
ivanayov:fix/rest-api-empty-response

Conversation

@ivanayov

@ivanayov ivanayov commented Jul 4, 2017

Copy link
Copy Markdown
Contributor

Using the REST API from the GUI used to return "error": "no response from server" and return code 0 for methods that return String.
The problem was that the received response was not in json format, while the expected response content type is application/json.

@geomacy geomacy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some explanation of what the problem is and how this change fixes it?

@ApiResponses(value = {
@ApiResponse(code = 404, message = "Could not find task or stream")
})
@Produces(MediaType.TEXT_PLAIN + ";qs=0.9")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the qs? it's not actually a valid parameter on Content-Type, it's some sort of Apache webserver hack.

@ivanayov
ivanayov force-pushed the fix/rest-api-empty-response branch from c54775a to 299c2ab Compare July 4, 2017 15:03
…tGetConfig tests to work with the rest api fix
@ivanayov

ivanayov commented Jul 4, 2017

Copy link
Copy Markdown
Contributor Author

retest this please

@geomacy

geomacy commented Jul 4, 2017

Copy link
Copy Markdown
Contributor

I got the following:

Tests run: 181, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 21.038 sec <<< FAILURE! - in TestSuite
testGetConfigValue(org.apache.brooklyn.rest.resources.PolicyResourceTest)  Time elapsed: 0.016 sec  <<< FAILURE!
java.lang.AssertionError: expected [newval] but found ["newval"]
	at org.apache.brooklyn.rest.resources.PolicyResourceTest.testGetConfigValue(PolicyResourceTest.java:141)


Results :

Failed tests: 
  PolicyResourceTest.testGetConfigValue:141 expected [newval] but found ["newval"]

looks like it is caused by the changed response format.

@ahgittin

ahgittin commented Jul 4, 2017

Copy link
Copy Markdown
Contributor

@iyovcheva I think this breaks backwards compatibility so we need to be clear where and what it is changing and what it might impact.

Agree it makes sense to be clear are we returning JSON (e.g. wrap string in quotes) or PLAIN.

However we may also have the problem that in some places a string we return will itself be wrapped. And in other places the client might want an unwrapped string (ie not JSON).

@ivanayov

ivanayov commented Jul 5, 2017

Copy link
Copy Markdown
Contributor Author

Thanks @geomacy , I updated the test. "retest this please" triggers a rebuild.

@ivanayov

ivanayov commented Jul 5, 2017

Copy link
Copy Markdown
Contributor Author

@ahgittin This is a list of the GET requests the change impacts:

/activities/{task}/stream/{streamId}
/applications/{application}/entities/{entity}/policies/{policy}/config
/applications/{application}/entities/{entity}/policies/{policy}/config/current-state
/server/user

The one that was broken in the UI was /server/user. In this case we always expect a single username, which makes the change not harmful (if you have an example that shows the opposite, it's well appreciated). The other option here is to change it to text/plain.

For /activities/{task}/stream/{streamId} text/plain is always the logical option and we ensure that json will never be expected in this case.

The controversial options IMO are the two policies requests. In this case the returned string is already in json, so we may not need any changes.

What do you think about:

  • Keep the change for /activities/{task}/stream/{streamId}
  • Change /server/user to text/plain
  • Remove policies changes, as this was working properly before and can be risky now?

@geomacy

geomacy commented Jul 5, 2017

Copy link
Copy Markdown
Contributor

👍 +1 to your suggestions @iyovcheva

If I understand correctly, the changes to add @Produces(MediaType.TEXT_PLAIN don't change the responses, but do ensure that the Content-Type is correctly declared; and removing the policies changes avoids backward compatibility issues.

@neykov

neykov commented Jul 5, 2017

Copy link
Copy Markdown
Member

Suggest we keep the changes for PolicyConfigApi.get(...), making it work analogously to EntityConfigApi.get(...). The value there can be any object, so it needs to be encoded as JSON.

@ahgittin Backwards compatibility is debatable. If we used to return plain strings as application/json then obviously this was broken and clients should've complained. This probably got covered by the libraries used to fetch the resource, suggesting that making it proper json will actually improve compatibility with existing clients.

@geomacy

geomacy commented Jul 10, 2017

Copy link
Copy Markdown
Contributor

per @neykov's suggestion, merging this with the policy changes included.

@asfgit
asfgit merged commit 829e495 into apache:master Jul 10, 2017
asfgit pushed a commit that referenced this pull request Jul 10, 2017
Fix REST API response of methods with String return type

Using the REST API from the GUI used to return `"error": "no response from server"` and return code 0 for methods that return String.
The problem was that the received response was not in json format, while the expected response content type is `application/json`.
Map<String, Object> result = Maps.newLinkedHashMap();
for (Map.Entry<String, Object> ek : source.entrySet()) {
result.put(ek.getKey(), getStringValueForDisplay(brooklyn(), policy, ek.getValue()));
result.put(ek.getKey(), WebResourceUtils.getValueForDisplay(mapper(), getStringValueForDisplay(brooklyn(), policy, ek.getValue()), true, true));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed, fixes only needed for methods returning String. This one will get converted to json by the framework.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants