You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/10/15 11:23:58 UTC
[incubator-servicecomb-java-chassis] 02/06: [SCB-928] support
collection format on provider side
This is an automated email from the ASF dual-hosted git repository.
liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git
commit 40d23313a00f0a4140236c0032ef92e3995119c0
Author: yaohaishi <ya...@huawei.com>
AuthorDate: Fri Oct 12 09:57:51 2018 +0800
[SCB-928] support collection format on provider side
---
.../rest/codec/param/QueryProcessorCreator.java | 19 ++++-
.../rest/codec/param/TestQueryProcessor.java | 28 ++++++--
.../property/SwaggerParamCollectionFormat.java | 62 ++++++++++++++++
.../property/SwaggerParamCollectionFormatTest.java | 84 ++++++++++++++++++++++
4 files changed, 183 insertions(+), 10 deletions(-)
diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/QueryProcessorCreator.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/QueryProcessorCreator.java
index 713711e..2e6d941 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/QueryProcessorCreator.java
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/QueryProcessorCreator.java
@@ -23,6 +23,7 @@ import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.servicecomb.common.rest.codec.RestClientRequest;
+import org.apache.servicecomb.swagger.converter.property.SwaggerParamCollectionFormat;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.type.TypeFactory;
@@ -38,18 +39,25 @@ public class QueryProcessorCreator implements ParamValueProcessorCreator {
// This configuration is used for temporary use only. Do not use it if you are sure how it works. And may be deleted in future.
private boolean emptyAsNull = DynamicPropertyFactory.getInstance()
.getBooleanProperty("servicecomb.rest.parameter.query.emptyAsNull", false).get();
+
// This configuration is used for temporary use only. Do not use it if you are sure how it works. And may be deleted in future.
private boolean ignoreDefaultValue = DynamicPropertyFactory.getInstance()
.getBooleanProperty("servicecomb.rest.parameter.query.ignoreDefaultValue", false).get();
- public QueryProcessor(String paramPath, JavaType targetType, Object defaultValue) {
+ private SwaggerParamCollectionFormat collectionFormat;
+
+ public QueryProcessor(String paramPath, JavaType targetType, Object defaultValue, String collectionFormat) {
super(paramPath, targetType, defaultValue);
+ if (StringUtils.isNoneEmpty(collectionFormat)) {
+ this.collectionFormat = SwaggerParamCollectionFormat.valueOf(collectionFormat.toUpperCase());
+ }
}
@Override
public Object getValue(HttpServletRequest request) throws Exception {
Object value = null;
- if (targetType.isContainerType()) {
+ if (targetType.isContainerType()
+ && SwaggerParamCollectionFormat.MULTI.equals(collectionFormat)) {
value = request.getParameterValues(paramPath);
} else {
value = request.getParameter(paramPath);
@@ -65,6 +73,9 @@ public class QueryProcessorCreator implements ParamValueProcessorCreator {
value = defaultValue;
}
}
+ if (null != collectionFormat) {
+ value = collectionFormat.splitParam((String) value);
+ }
}
return convertValue(value, targetType);
@@ -87,7 +98,9 @@ public class QueryProcessorCreator implements ParamValueProcessorCreator {
@Override
public ParamValueProcessor create(Parameter parameter, Type genericParamType) {
+ QueryParameter queryParameter = (QueryParameter) parameter;
JavaType targetType = TypeFactory.defaultInstance().constructType(genericParamType);
- return new QueryProcessor(parameter.getName(), targetType, ((QueryParameter) parameter).getDefaultValue());
+ return new QueryProcessor(parameter.getName(), targetType, queryParameter.getDefaultValue(),
+ queryParameter.getCollectionFormat());
}
}
diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestQueryProcessor.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestQueryProcessor.java
index 75f1ee0..ec896a2 100644
--- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestQueryProcessor.java
+++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestQueryProcessor.java
@@ -33,8 +33,8 @@ public class TestQueryProcessor {
@Mocked
HttpServletRequest request;
- private ParamValueProcessor createProcessor(String name, Class<?> type) {
- return new QueryProcessor(name, TypeFactory.defaultInstance().constructType(type), null);
+ private ParamValueProcessor createProcessor(String name, Class<?> type, String collectionFormat) {
+ return new QueryProcessor(name, TypeFactory.defaultInstance().constructType(type), null, collectionFormat);
}
@Test
@@ -46,7 +46,7 @@ public class TestQueryProcessor {
}
};
- ParamValueProcessor processor = createProcessor("name", String.class);
+ ParamValueProcessor processor = createProcessor("name", String.class, "multi");
Object value = processor.getValue(request);
Assert.assertEquals("value", value);
}
@@ -56,18 +56,32 @@ public class TestQueryProcessor {
new Expectations() {
{
request.getParameterValues("name");
- result = new String[] {"value"};
+ result = new String[] {"value", "value2"};
}
};
- ParamValueProcessor processor = createProcessor("name", String[].class);
+ ParamValueProcessor processor = createProcessor("name", String[].class, "multi");
String[] value = (String[]) processor.getValue(request);
- Assert.assertThat(value, Matchers.arrayContaining("value"));
+ Assert.assertThat(value, Matchers.arrayContaining("value", "value2"));
+ }
+
+ @Test
+ public void testGetValueOnCollectionFormatIsCsv() throws Exception {
+ new Expectations() {
+ {
+ request.getParameter("name");
+ result = "value2,value3";
+ }
+ };
+
+ ParamValueProcessor processor = createProcessor("name", String[].class, "csv");
+ String[] value = (String[]) processor.getValue(request);
+ Assert.assertThat(value, Matchers.arrayContaining("value2", "value3"));
}
@Test
public void testGetProcessorType() {
- ParamValueProcessor processor = createProcessor("name", String.class);
+ ParamValueProcessor processor = createProcessor("name", String.class, "multi");
Assert.assertEquals("query", processor.getProcessorType());
}
}
diff --git a/swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/converter/property/SwaggerParamCollectionFormat.java b/swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/converter/property/SwaggerParamCollectionFormat.java
new file mode 100644
index 0000000..b354db0
--- /dev/null
+++ b/swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/converter/property/SwaggerParamCollectionFormat.java
@@ -0,0 +1,62 @@
+/*
+ * 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.swagger.converter.property;
+
+public enum SwaggerParamCollectionFormat {
+ CSV("csv", ","),
+ SSV("ssv", " "),
+ TSV("tsv", "\t"),
+ PIPES("pipes", "\\|"),
+ MULTI("multi", null) {
+ /**
+ * In fact, {@link SwaggerParamCollectionFormat#MULTI#splitParam(String)} of {@link SwaggerParamCollectionFormat#MULTI}
+ * should never be invoked. We just override this method to ensure it does not throw exception.
+ */
+ @Override
+ public String[] splitParam(String rawParam) {
+ if (null == rawParam) {
+ return new String[0];
+ }
+ return new String[] {rawParam};
+ }
+ };
+
+ final private String collectionFormat;
+
+ final private String separator;
+
+ SwaggerParamCollectionFormat(String collectionFormat, String separator) {
+ this.collectionFormat = collectionFormat;
+ this.separator = separator;
+ }
+
+ public String getCollectionFormat() {
+ return collectionFormat;
+ }
+
+ public String getSeparator() {
+ return separator;
+ }
+
+ public String[] splitParam(String rawParam) {
+ if (null == rawParam) {
+ return new String[0];
+ }
+ return rawParam.split(separator);
+ }
+}
diff --git a/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/converter/property/SwaggerParamCollectionFormatTest.java b/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/converter/property/SwaggerParamCollectionFormatTest.java
new file mode 100644
index 0000000..48d202e
--- /dev/null
+++ b/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/converter/property/SwaggerParamCollectionFormatTest.java
@@ -0,0 +1,84 @@
+/*
+ * 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.swagger.converter.property;
+
+import org.hamcrest.Matchers;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class SwaggerParamCollectionFormatTest {
+ @Test
+ public void splitParamNormal() {
+ Assert.assertThat(SwaggerParamCollectionFormat.CSV.splitParam("a,b,c"),
+ Matchers.arrayContaining("a", "b", "c"));
+ Assert.assertThat(SwaggerParamCollectionFormat.SSV.splitParam("10 11 12"),
+ Matchers.arrayContaining("10", "11", "12"));
+ Assert.assertThat(SwaggerParamCollectionFormat.TSV.splitParam("a\tb\tc"),
+ Matchers.arrayContaining("a", "b", "c"));
+ Assert.assertThat(SwaggerParamCollectionFormat.PIPES.splitParam("a|b|c"),
+ Matchers.arrayContaining("a", "b", "c"));
+ }
+
+ @Test
+ public void splitParamMulti() {
+ Assert.assertThat(SwaggerParamCollectionFormat.MULTI.splitParam("a,b,c"),
+ Matchers.arrayContaining("a,b,c"));
+ }
+
+ @Test
+ public void splitParam_SingleElement() {
+ Assert.assertThat(SwaggerParamCollectionFormat.CSV.splitParam("a"),
+ Matchers.arrayContaining("a"));
+ Assert.assertThat(SwaggerParamCollectionFormat.SSV.splitParam("a"),
+ Matchers.arrayContaining("a"));
+ Assert.assertThat(SwaggerParamCollectionFormat.TSV.splitParam("a"),
+ Matchers.arrayContaining("a"));
+ Assert.assertThat(SwaggerParamCollectionFormat.PIPES.splitParam("a"),
+ Matchers.arrayContaining("a"));
+ Assert.assertThat(SwaggerParamCollectionFormat.MULTI.splitParam("a"),
+ Matchers.arrayContaining("a"));
+ }
+
+ @Test
+ public void splitParam_NullElement() {
+ Assert.assertThat(SwaggerParamCollectionFormat.CSV.splitParam(null),
+ Matchers.emptyArray());
+ Assert.assertThat(SwaggerParamCollectionFormat.SSV.splitParam(null),
+ Matchers.emptyArray());
+ Assert.assertThat(SwaggerParamCollectionFormat.TSV.splitParam(null),
+ Matchers.emptyArray());
+ Assert.assertThat(SwaggerParamCollectionFormat.PIPES.splitParam(null),
+ Matchers.emptyArray());
+ Assert.assertThat(SwaggerParamCollectionFormat.MULTI.splitParam(null),
+ Matchers.emptyArray());
+ }
+
+ @Test
+ public void splitParam_BlankElement() {
+ Assert.assertThat(SwaggerParamCollectionFormat.CSV.splitParam(""),
+ Matchers.arrayContaining(""));
+ Assert.assertThat(SwaggerParamCollectionFormat.SSV.splitParam(""),
+ Matchers.arrayContaining(""));
+ Assert.assertThat(SwaggerParamCollectionFormat.TSV.splitParam(""),
+ Matchers.arrayContaining(""));
+ Assert.assertThat(SwaggerParamCollectionFormat.PIPES.splitParam(""),
+ Matchers.arrayContaining(""));
+ Assert.assertThat(SwaggerParamCollectionFormat.MULTI.splitParam(""),
+ Matchers.arrayContaining(""));
+ }
+}
\ No newline at end of file