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/19 01:44:40 UTC
[incubator-servicecomb-java-chassis] 03/03: [SCB-675] add generate
proto from swagger unit test
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 d2325c4f3a57d02b65af684a853ed6bae03c3089
Author: wujimin <wu...@huawei.com>
AuthorDate: Thu Oct 11 10:14:09 2018 +0800
[SCB-675] add generate proto from swagger unit test
---
common/common-protobuf/pom.xml | 9 +
.../converter/TestSwaggerToProtoGenerator.java | 191 +++++++++++++++++++++
.../internal/converter/model/ProtoSchema.java | 98 +++++++++++
.../protobuf/internal/converter/model/Ref1.java | 21 +++
.../protobuf/internal/converter/model/Ref2.java | 21 +++
5 files changed, 340 insertions(+)
diff --git a/common/common-protobuf/pom.xml b/common/common-protobuf/pom.xml
index aa70364..6fe4651 100644
--- a/common/common-protobuf/pom.xml
+++ b/common/common-protobuf/pom.xml
@@ -51,5 +51,14 @@
<artifactId>log4j</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.servicecomb</groupId>
+ <artifactId>swagger-generator-springmvc</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.servicecomb</groupId>
+ <artifactId>foundation-test-scaffolding</artifactId>
+ </dependency>
</dependencies>
</project>
diff --git a/common/common-protobuf/src/test/java/org/apache/servicecomb/codec/protobuf/internal/converter/TestSwaggerToProtoGenerator.java b/common/common-protobuf/src/test/java/org/apache/servicecomb/codec/protobuf/internal/converter/TestSwaggerToProtoGenerator.java
new file mode 100644
index 0000000..4b604a4
--- /dev/null
+++ b/common/common-protobuf/src/test/java/org/apache/servicecomb/codec/protobuf/internal/converter/TestSwaggerToProtoGenerator.java
@@ -0,0 +1,191 @@
+/*
+ * 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.codec.protobuf.internal.converter;
+
+import org.apache.servicecomb.codec.protobuf.internal.converter.model.ProtoSchema;
+import org.apache.servicecomb.foundation.test.scaffolding.model.Color;
+import org.apache.servicecomb.swagger.generator.core.SwaggerGenerator;
+import org.apache.servicecomb.swagger.generator.core.SwaggerGeneratorContext;
+import org.apache.servicecomb.swagger.generator.springmvc.SpringmvcSwaggerGeneratorContext;
+import org.junit.Assert;
+import org.junit.Test;
+
+import io.protostuff.compiler.model.Proto;
+import io.swagger.models.Swagger;
+import io.vertx.core.json.Json;
+
+public class TestSwaggerToProtoGenerator {
+ static String protoContent = "syntax = \"proto3\";\n"
+ + "import \"google/protobuf/empty.proto\";\n"
+ + "import \"google/protobuf/any.proto\";\n"
+ + "package a.b;\n"
+ + "\n"
+ + "message Empty {\n"
+ + "}\n"
+ + "\n"
+ + "message User {\n"
+ + " string name = 1;\n"
+ + " repeated User friends = 2;\n"
+ + "}\n"
+ + "\n"
+ + "message Ref1 {\n"
+ + " Ref2 ref = 1;\n"
+ + "}\n"
+ + "\n"
+ + "message Ref2 {\n"
+ + " Ref1 ref = 1;\n"
+ + "}\n"
+ + "\n"
+ + "message baseRequestWrap {\n"
+ + " bool boolValue = 1;\n"
+ + " int32 iValue = 2;\n"
+ + " int64 lValue = 3;\n"
+ + " float fValue = 4;\n"
+ + " double dValue = 5;\n"
+ + " string sValue = 6;\n"
+ + " repeated int32 iArray = 7;\n"
+ + " Enum_2610aa5dc6cd086cf20168892802c9c765a557f4951557340ad9f0982c53e055 color = 8;\n"
+ + " int64 localDate = 9;\n"
+ + " int64 date = 10;\n"
+ + " Empty empty = 11;\n"
+ + "}\n"
+ + "\n"
+ + "message baseResponseWrap444 {\n"
+ + " Enum_2610aa5dc6cd086cf20168892802c9c765a557f4951557340ad9f0982c53e055 response = 1;\n"
+ + "}\n"
+ + "\n"
+ + "message baseResponseWrap200 {\n"
+ + " int32 response = 1;\n"
+ + "}\n"
+ + "\n"
+ + "message bytesRequestWrap {\n"
+ + " bytes value = 1;\n"
+ + "}\n"
+ + "\n"
+ + "message bytesResponseWrap200 {\n"
+ + " bytes response = 1;\n"
+ + "}\n"
+ + "\n"
+ + "message colorBodyRequestWrap {\n"
+ + " Enum_2610aa5dc6cd086cf20168892802c9c765a557f4951557340ad9f0982c53e055 color = 1;\n"
+ + "}\n"
+ + "\n"
+ + "message colorBodyResponseWrap200 {\n"
+ + " Enum_2610aa5dc6cd086cf20168892802c9c765a557f4951557340ad9f0982c53e055 response = 1;\n"
+ + "}\n"
+ + "\n"
+ + "message listObjRequestWrap {\n"
+ + " repeated google.protobuf.Any objs = 1;\n"
+ + "}\n"
+ + "\n"
+ + "message listObjResponseWrap200 {\n"
+ + " repeated google.protobuf.Any response = 1;\n"
+ + "}\n"
+ + "\n"
+ + "message listUserRequestWrap {\n"
+ + " repeated User users = 1;\n"
+ + "}\n"
+ + "\n"
+ + "message listUserResponseWrap200 {\n"
+ + " repeated User response = 1;\n"
+ + "}\n"
+ + "\n"
+ + "message mapObjRequestWrap {\n"
+ + " map<string, google.protobuf.Any> objs = 1;\n"
+ + "}\n"
+ + "\n"
+ + "message mapObjResponseWrap200 {\n"
+ + " map<string, google.protobuf.Any> response = 1;\n"
+ + "}\n"
+ + "\n"
+ + "message mapUserRequestWrap {\n"
+ + " map<string, User> users = 1;\n"
+ + "}\n"
+ + "\n"
+ + "message mapUserResponseWrap200 {\n"
+ + " map<string, User> response = 1;\n"
+ + "}\n"
+ + "\n"
+ + "message userWrapInProtobufRequestWrap {\n"
+ + " User user = 1;\n"
+ + " int32 ivalue = 2;\n"
+ + "}\n"
+ + "\n"
+ + "enum Enum_2610aa5dc6cd086cf20168892802c9c765a557f4951557340ad9f0982c53e055 {\n"
+ + " RED = 0;\n"
+ + " YELLOW = 1;\n"
+ + " BLUE = 2;\n"
+ + "}\n"
+ + "\n"
+ + "service MainService {\n"
+ + " //scb:{\"argTypeName\":\"baseRequestWrap\",\"argWrapped\":true,\"responses\":{\"200\":{\"typeName\":\"baseResponseWrap200\",\"wrapped\":true},\"444\":{\"typeName\":\"baseResponseWrap444\",\"wrapped\":true}}}\n"
+ + " rpc base (baseRequestWrap) returns (baseResponseWrap200);\n"
+ + "\n"
+ + " //scb:{\"argTypeName\":\"bytesRequestWrap\",\"argWrapped\":true,\"responses\":{\"200\":{\"typeName\":\"bytesResponseWrap200\",\"wrapped\":true}}}\n"
+ + " rpc bytes (bytesRequestWrap) returns (bytesResponseWrap200);\n"
+ + "\n"
+ + " //scb:{\"argTypeName\":\"colorBodyRequestWrap\",\"argWrapped\":true,\"responses\":{\"200\":{\"typeName\":\"colorBodyResponseWrap200\",\"wrapped\":true}}}\n"
+ + " rpc colorBody (colorBodyRequestWrap) returns (colorBodyResponseWrap200);\n"
+ + "\n"
+ + " //scb:{\"argTypeName\":\"listObjRequestWrap\",\"argWrapped\":true,\"responses\":{\"200\":{\"typeName\":\"listObjResponseWrap200\",\"wrapped\":true}}}\n"
+ + " rpc listObj (listObjRequestWrap) returns (listObjResponseWrap200);\n"
+ + "\n"
+ + " //scb:{\"argTypeName\":\"listUserRequestWrap\",\"argWrapped\":true,\"responses\":{\"200\":{\"typeName\":\"listUserResponseWrap200\",\"wrapped\":true}}}\n"
+ + " rpc listUser (listUserRequestWrap) returns (listUserResponseWrap200);\n"
+ + "\n"
+ + " //scb:{\"argTypeName\":\"mapObjRequestWrap\",\"argWrapped\":true,\"responses\":{\"200\":{\"typeName\":\"mapObjResponseWrap200\",\"wrapped\":true}}}\n"
+ + " rpc mapObj (mapObjRequestWrap) returns (mapObjResponseWrap200);\n"
+ + "\n"
+ + " //scb:{\"argTypeName\":\"mapUserRequestWrap\",\"argWrapped\":true,\"responses\":{\"200\":{\"typeName\":\"mapUserResponseWrap200\",\"wrapped\":true}}}\n"
+ + " rpc mapUser (mapUserRequestWrap) returns (mapUserResponseWrap200);\n"
+ + "\n"
+ + " //scb:{\"argTypeName\":\"google.protobuf.Empty\",\"argWrapped\":false,\"responses\":{\"200\":{\"typeName\":\"google.protobuf.Empty\",\"wrapped\":false}}}\n"
+ + " rpc noParamVoid (google.protobuf.Empty) returns (google.protobuf.Empty);\n"
+ + "\n"
+ + " //scb:{\"argTypeName\":\"google.protobuf.Any\",\"argWrapped\":false,\"responses\":{\"200\":{\"typeName\":\"google.protobuf.Any\",\"wrapped\":false}}}\n"
+ + " rpc obj (google.protobuf.Any) returns (google.protobuf.Any);\n"
+ + "\n"
+ + " //scb:{\"argTypeName\":\"Ref1\",\"argWrapped\":false,\"responses\":{\"200\":{\"typeName\":\"Ref2\",\"wrapped\":false}}}\n"
+ + " rpc ref (Ref1) returns (Ref2);\n"
+ + "\n"
+ + " //scb:{\"argTypeName\":\"User\",\"argWrapped\":false,\"responses\":{\"200\":{\"typeName\":\"User\",\"wrapped\":false}}}\n"
+ + " rpc user (User) returns (User);\n"
+ + "\n"
+ + " //scb:{\"argTypeName\":\"userWrapInProtobufRequestWrap\",\"argWrapped\":true,\"responses\":{\"200\":{\"typeName\":\"User\",\"wrapped\":false}}}\n"
+ + " rpc userWrapInProtobuf (userWrapInProtobufRequestWrap) returns (User);\n"
+ + "}\n";
+
+ @Test
+ public void convert() {
+ SwaggerGeneratorContext context = new SpringmvcSwaggerGeneratorContext();
+ SwaggerGenerator swaggerGenerator = new SwaggerGenerator(context, ProtoSchema.class);
+ Swagger swagger = swaggerGenerator.generate();
+
+ SwaggerToProtoGenerator generator = new SwaggerToProtoGenerator("a.b", swagger);
+ Proto proto = generator.convert();
+
+ Assert.assertEquals(protoContent, new ProtoToStringGenerator(proto).protoToString());
+ }
+
+ public static void main(String[] args) {
+ String json = Json.encode(Color.BLUE);
+ System.out.println(json);
+ System.out.println(Json.decodeValue(json, Color.class));
+ System.out.println(Json.decodeValue("2", Color.class));
+ System.out.println(Json.mapper.convertValue("BLUE", Color.class));
+ }
+}
diff --git a/common/common-protobuf/src/test/java/org/apache/servicecomb/codec/protobuf/internal/converter/model/ProtoSchema.java b/common/common-protobuf/src/test/java/org/apache/servicecomb/codec/protobuf/internal/converter/model/ProtoSchema.java
new file mode 100644
index 0000000..3b046ec
--- /dev/null
+++ b/common/common-protobuf/src/test/java/org/apache/servicecomb/codec/protobuf/internal/converter/model/ProtoSchema.java
@@ -0,0 +1,98 @@
+/*
+ * 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.codec.protobuf.internal.converter.model;
+
+
+import java.time.LocalDate;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.servicecomb.foundation.test.scaffolding.model.Color;
+import org.apache.servicecomb.foundation.test.scaffolding.model.Empty;
+import org.apache.servicecomb.foundation.test.scaffolding.model.User;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+import io.swagger.annotations.ApiResponse;
+import io.swagger.annotations.ApiResponses;
+
+@RequestMapping(path = "/")
+public class ProtoSchema {
+ @ApiResponses(value = {@ApiResponse(code = 444, response = Color.class, message = "xxx")})
+ @GetMapping(path = "/base")
+ public int base(boolean boolValue, int iValue, long lValue, float fValue, double dValue, String sValue, int[] iArray,
+ Color color,
+ LocalDate localDate, Date date, @RequestBody Empty empty) {
+ return 0;
+ }
+
+ @GetMapping(path = "/bytes")
+ public byte[] bytes(@RequestBody byte[] value) {
+ return null;
+ }
+
+ @GetMapping(path = "/colorBody")
+ public Color colorBody(@RequestBody Color color) {
+ return null;
+ }
+
+ @GetMapping(path = "/obj")
+ public Object obj(@RequestBody Object value) {
+ return null;
+ }
+
+ @GetMapping(path = "/user")
+ public User user(@RequestBody User user) {
+ return null;
+ }
+
+ @GetMapping(path = "/userWrapInProtobuf")
+ public User userWrapInProtobuf(@RequestBody User user, int ivalue) {
+ return null;
+ }
+
+ @GetMapping(path = "/listObj")
+ public List<Object> listObj(@RequestBody List<Object> objs) {
+ return null;
+ }
+
+ @GetMapping(path = "/listUser")
+ public List<User> listUser(@RequestBody List<User> users) {
+ return null;
+ }
+
+ @GetMapping(path = "/mapUser")
+ public Map<String, User> mapUser(@RequestBody Map<String, User> users) {
+ return null;
+ }
+
+ @GetMapping(path = "/mapObj")
+ public Map<String, Object> mapObj(@RequestBody Map<String, Object> objs) {
+ return null;
+ }
+
+ @GetMapping(path = "/ref")
+ public Ref2 ref(@RequestBody Ref1 ref) {
+ return null;
+ }
+
+ @GetMapping(path = "/noParamVoid")
+ public void noParamVoid() {
+ }
+}
diff --git a/common/common-protobuf/src/test/java/org/apache/servicecomb/codec/protobuf/internal/converter/model/Ref1.java b/common/common-protobuf/src/test/java/org/apache/servicecomb/codec/protobuf/internal/converter/model/Ref1.java
new file mode 100644
index 0000000..c637bf5
--- /dev/null
+++ b/common/common-protobuf/src/test/java/org/apache/servicecomb/codec/protobuf/internal/converter/model/Ref1.java
@@ -0,0 +1,21 @@
+/*
+ * 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.codec.protobuf.internal.converter.model;
+
+public class Ref1 {
+ public Ref2 ref;
+}
diff --git a/common/common-protobuf/src/test/java/org/apache/servicecomb/codec/protobuf/internal/converter/model/Ref2.java b/common/common-protobuf/src/test/java/org/apache/servicecomb/codec/protobuf/internal/converter/model/Ref2.java
new file mode 100644
index 0000000..f4b9d9e
--- /dev/null
+++ b/common/common-protobuf/src/test/java/org/apache/servicecomb/codec/protobuf/internal/converter/model/Ref2.java
@@ -0,0 +1,21 @@
+/*
+ * 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.codec.protobuf.internal.converter.model;
+
+public class Ref2 {
+ public Ref1 ref;
+}