Skip to content

Commit

Permalink
[#4334]fix invocation arguments not same with swagger arguments probl…
Browse files Browse the repository at this point in the history
…em (#4335)
  • Loading branch information
liubao68 authored May 11, 2024
1 parent ba06fef commit f99d371
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.apache.servicecomb.demo.springmvc;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -29,7 +28,6 @@
import org.apache.servicecomb.demo.controller.Controller;
import org.apache.servicecomb.demo.controller.Person;
import org.apache.servicecomb.demo.springmvc.client.CodeFirstRestTemplateSpringmvc;
import org.apache.servicecomb.demo.springmvc.client.ThirdSvc.ThirdSvcClient;
import org.apache.servicecomb.foundation.common.LegacyPropertyFactory;
import org.apache.servicecomb.foundation.common.utils.BeanUtils;
import org.apache.servicecomb.foundation.vertx.client.http.HttpClients;
Expand Down Expand Up @@ -80,7 +78,7 @@ public static void main(String[] args) throws Exception {
LOGGER.error("-------------- test failed -------------");
}
TestMgr.summary();
LOGGER.info("-------------- last time updated checks(maybe more/less): 1341 -------------");
LOGGER.info("-------------- last time updated checks(maybe more/less): 1344 -------------");
}

private static void changeTransport(String microserviceName, String transport) {
Expand Down Expand Up @@ -218,18 +216,9 @@ private static void testAllTransport(String microserviceName) {
testController();
testSpringMvcDefaultValuesAllTransport(templateUrlWithServiceName, microserviceName);
testSpringMvcDefaultValuesJavaPrimitiveAllTransport(templateUrlWithServiceName, microserviceName);
testThirdService();
}
}

private static void testThirdService() {
ThirdSvcClient client = BeanUtils.getContext().getBean(ThirdSvcClient.class);

Date date = new Date();
ResponseEntity<Date> responseEntity = client.responseEntity(date);
TestMgr.check(date, responseEntity.getBody());
}

private static void testControllerRest(RestTemplate template, String microserviceName) {
String prefix = "cse://" + microserviceName;

Expand Down Expand Up @@ -340,7 +329,6 @@ private static void testController() {
}



private static void testSpringMvcDefaultValuesRest(RestTemplate template, String microserviceName) {
String cseUrlPrefix = "cse://" + microserviceName + "/SpringMvcDefaultValues/";
String result = template.getForObject(cseUrlPrefix + "/query?d=10", String.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.springmvc.client;

import java.util.Date;

import org.apache.servicecomb.demo.CategorizedTestCase;
import org.apache.servicecomb.demo.TestMgr;
import org.apache.servicecomb.demo.springmvc.client.ThirdSvc.ThirdSvcClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

@Component
public class TestThirdSvc implements CategorizedTestCase {
private ThirdSvcClient client;

@Autowired
public void setThirdSvcClient(ThirdSvcClient client) {
this.client = client;
}

@Override
public void testAllTransport() throws Exception {
testResponseEntity();

testDifferentParameterName();
}

private void testDifferentParameterName() {
String result = client.getAuthorization("test", "param", "auth");
TestMgr.check("testparamauth", result);
}

private void testResponseEntity() {
Date date = new Date();
ResponseEntity<Date> responseEntity = client.responseEntity(date);
TestMgr.check(date, responseEntity.getBody());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import io.swagger.v3.oas.annotations.headers.Header;
import io.swagger.v3.oas.annotations.media.Content;
Expand All @@ -51,6 +54,12 @@ public interface ThirdSvcClient {
@Header(name = "h2", schema = @Schema(implementation = String.class))})
@RequestMapping(path = "/responseEntity", method = RequestMethod.POST)
ResponseEntity<Date> responseEntity(@RequestAttribute("date") Date date);

@GetMapping(value = "/getAuthorization", produces = {"application/json"})
String getAuthorization(
@RequestHeader(value = "test") String test,
@RequestParam(value = "param") String param,
@RequestHeader("Authorization") String authorization);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ public ResponseEntity<Date> responseEntity(InvocationContext c1, @RequestAttribu
return new ResponseEntity<>(date, headers, HttpStatus.ACCEPTED);
}

@GetMapping(value = "/getAuthorization", produces = {"application/json"})
public String getAuthorization(
@RequestHeader(value = "test") String test,
@RequestParam(value = "param") String param,
@Parameter(description = "Authorization header", required = true, in = ParameterIn.HEADER,
name = "Authorization") @RequestHeader("Authorization") String authorization) {
return test + param + authorization;
}

@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = Date.class))
, description = "",
headers = {@Header(name = "h1", schema = @Schema(implementation = String.class)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ protected void processPendingBodyParameter(RequestBody parameter) {

@Override
protected ArgumentMapper createKnownParameterMapper(int providerParamIdx, String invocationArgumentName) {
return new ConsumerArgumentSame(invocationArgumentName, invocationArgumentName);
return new ConsumerArgumentSame(this.providerMethod.getParameters()[providerParamIdx].getName(),
invocationArgumentName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void add_add() {
Map<String, Object> arguments = new HashMap<>();
arguments.put("x", 1);
arguments.put("y", 2);
arguments.put("x-z", 3);
arguments.put("z", 3);
SwaggerInvocation invocation = new SwaggerInvocation();

Map<String, Object> result = mapper.invocationArgumentToSwaggerArguments(invocation, arguments);
Expand All @@ -70,7 +70,7 @@ public void add_addBeanParam() {
Map<String, Object> arguments = new HashMap<>();
arguments.put("x", 1);
arguments.put("y", 2);
arguments.put("x-z", 3);
arguments.put("z", 3);
SwaggerInvocation invocation = new SwaggerInvocation();

Map<String, Object> result = mapper.invocationArgumentToSwaggerArguments(invocation, arguments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
import org.apache.servicecomb.swagger.invocation.schemas.SpringmvcAddV2;
import org.apache.servicecomb.swagger.invocation.schemas.SpringmvcAddWrapperV2;
import org.apache.servicecomb.swagger.invocation.schemas.models.AddWrapperV2;

import io.swagger.v3.oas.models.OpenAPI;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import io.swagger.v3.oas.models.OpenAPI;

@SuppressWarnings("unchecked")
public class TestSpringmvcV2V2 {
@Test
Expand All @@ -48,7 +48,7 @@ public void add_add() {
Map<String, Object> arguments = new HashMap<>();
arguments.put("x", 1);
arguments.put("y", 2);
arguments.put("x-z", 3);
arguments.put("z", 3);
SwaggerInvocation invocation = new SwaggerInvocation();

Map<String, Object> result = mapper.invocationArgumentToSwaggerArguments(invocation, arguments);
Expand All @@ -70,7 +70,7 @@ public void add_addWrapper() {
Map<String, Object> arguments = new HashMap<>();
arguments.put("x", 1);
arguments.put("y", 2);
arguments.put("x-z", 3);
arguments.put("z", 3);
SwaggerInvocation invocation = new SwaggerInvocation();

Map<String, Object> result = mapper.invocationArgumentToSwaggerArguments(invocation, arguments);
Expand Down

0 comments on commit f99d371

Please sign in to comment.