Skip to content

Commit

Permalink
[#4378]Fix getForObject not properly handle response type (#4381)
Browse files Browse the repository at this point in the history
  • Loading branch information
liubao68 authored Jun 25, 2024
1 parent cd5e35b commit d960aae
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.servicecomb.demo.controller;

public class PersonAlias {
private String name;

public void setName(String name) {
this.name = name;
}

public String getName() {
return this.name;
}

@Override
public String toString() {
return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,14 @@ paths:
description: check the handler is effective
schema:
type: string

/testResponseModel:
get:
operationId: testResponseModel
responses:
"200":
description: testResponseModel
schema:
$ref: '#/definitions/Person'
definitions:
Person:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import org.apache.servicecomb.demo.CategorizedTestCase;
import org.apache.servicecomb.demo.TestMgr;
import org.apache.servicecomb.demo.controller.Person;
import org.apache.servicecomb.demo.controller.PersonAlias;
import org.apache.servicecomb.provider.springmvc.reference.RestTemplateBuilder;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
Expand All @@ -32,6 +34,16 @@ public class TestControllerImpl implements CategorizedTestCase {

public void testRestTransport() throws Exception {
testQueryParamSpecial();
testResponseModel();
}

private void testResponseModel() {
Person person = restTemplate.getForObject(SERVER + "/springmvc/controller/testResponseModel", Person.class);
TestMgr.check("jack", person.getName());

PersonAlias personAlias = restTemplate.getForObject(SERVER + "/springmvc/controller/testResponseModel",
PersonAlias.class);
TestMgr.check("jack", personAlias.getName());
}

private void testQueryParamSpecial() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,11 @@ public String sayHei(@RequestHeader("name") String name) {
public String sayHello1(@RequestParam("name") String name) {
return "Hello " + name + "," + ContextUtils.getInvocationContext().getContext("k");
}

@RequestMapping(path = "/testResponseModel", method = RequestMethod.GET)
public Person testResponseModel() {
Person person = new Person();
person.setName("jack");
return person;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
public class CseRequestCallback implements RequestCallback {
private final Object requestBody;

private final RequestCallback orgCallback;
private final RequestCallback targetCallback;

private final Type responseType;

public CseRequestCallback(Object requestBody, RequestCallback orgCallback, Type responseType) {
public CseRequestCallback(Object requestBody, RequestCallback targetCallback, Type responseType) {
this.requestBody = requestBody;
this.orgCallback = orgCallback;
this.targetCallback = targetCallback;
this.responseType = responseType;
}

Expand All @@ -41,7 +41,7 @@ public CseRequestCallback(Object requestBody, RequestCallback orgCallback, Type
*/
@Override
public void doWithRequest(ClientHttpRequest request) throws IOException {
orgCallback.doWithRequest(request);
targetCallback.doWithRequest(request);
CseClientHttpRequest req = (CseClientHttpRequest) request;
req.setResponseType(overrideResponseType());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ public <T> RequestCallback httpEntityCallback(Object requestBody, Type responseT
return cseCallback;
}

@Override
public <T> RequestCallback acceptHeaderRequestCallback(Class<T> responseType) {
RequestCallback callback = super.acceptHeaderRequestCallback(responseType);
CseRequestCallback cseCallback = new CseRequestCallback(null, callback, responseType);
return cseCallback;
}

@Override
public boolean isAcceptable(String uri) {
return uri.startsWith(RestConst.URI_PREFIX) || uri.startsWith(RestConst.URI_PREFIX_NEW);
Expand Down

0 comments on commit d960aae

Please sign in to comment.