Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SCB-1726]fix all TODOs in demos #1548

Merged
merged 5 commits into from Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -47,6 +47,10 @@
<groupId>org.apache.servicecomb</groupId>
<artifactId>transport-rest-vertx</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>transport-highway</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>provider-jaxrs</artifactId>
Expand Down
Expand Up @@ -46,6 +46,10 @@
<groupId>org.apache.servicecomb</groupId>
<artifactId>transport-rest-vertx</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>transport-highway</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>provider-pojo</artifactId>
Expand Down
Expand Up @@ -46,6 +46,10 @@
<groupId>org.apache.servicecomb</groupId>
<artifactId>transport-rest-vertx</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>transport-highway</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>provider-springmvc</artifactId>
Expand Down
Expand Up @@ -27,6 +27,7 @@
import org.apache.servicecomb.codec.protobuf.utils.ScopedProtobufSchemaManager;
import org.apache.servicecomb.core.definition.OperationMeta;
import org.apache.servicecomb.foundation.protobuf.ProtoMapper;
import org.apache.servicecomb.foundation.protobuf.internal.ProtoConst;
import org.apache.servicecomb.foundation.protobuf.internal.ProtoUtils;

import com.fasterxml.jackson.databind.JavaType;
Expand All @@ -45,6 +46,10 @@ public class OperationProtobuf {

private ResponseRootDeserializer<Object> responseRootDeserializer;

private ResponseRootSerializer anyResponseRootSerializer;

private ResponseRootDeserializer<Object> anyResponseRootDeserializer;

public OperationProtobuf(ScopedProtobufSchemaManager scopedProtobufSchemaManager, OperationMeta operationMeta) {
this.operationMeta = operationMeta;
initRequestCodec(scopedProtobufSchemaManager, operationMeta);
Expand All @@ -63,16 +68,14 @@ public ResponseRootSerializer findResponseRootSerializer(int statusCode) {
if (Family.SUCCESSFUL.equals(Family.familyOf(statusCode))) {
return this.responseRootSerializer;
}
// TODO : WEAK handles only one response type.
throw new IllegalStateException("not implemented now, statusCode = " + statusCode);
return anyResponseRootSerializer;
}

public ResponseRootDeserializer<Object> findResponseRootDeserializer(int statusCode) {
if (Family.SUCCESSFUL.equals(Family.familyOf(statusCode))) {
return this.responseRootDeserializer;
}
// TODO : WEAK handles only one response type.
throw new IllegalStateException("not implemented now, statusCode = " + statusCode);
return anyResponseRootDeserializer;
}

public OperationMeta getOperationMeta() {
Expand Down Expand Up @@ -159,5 +162,9 @@ private void initResponseCodec(ScopedProtobufSchemaManager scopedProtobufSchemaM
}
}
}
anyResponseRootSerializer = new ResponseRootSerializer(mapper.createRootSerializer(ProtoConst.ANY,
Object.class), false, true);
anyResponseRootDeserializer = new ResponseRootDeserializer<>(
mapper.createRootDeserializer(ProtoConst.ANY, Object.class), false);
}
}
Expand Up @@ -21,6 +21,8 @@

import org.apache.servicecomb.foundation.protobuf.RootSerializer;

import io.vertx.core.json.JsonObject;

public class RequestRootSerializer {
private RootSerializer rootSerializer;

Expand All @@ -37,7 +39,11 @@ public RequestRootSerializer(RootSerializer serializer, boolean isWrapp, boolean
@SuppressWarnings("unchecked")
public byte[] serialize(Object value) throws IOException {
if (noTypesInfo && !isWrap) {
return this.rootSerializer.serialize(((Map<String, Object>) value).values().iterator().next());
Object param = ((Map<String, Object>) value).values().iterator().next();
if (param instanceof JsonObject) {
param = ((JsonObject) param).getMap();
}
return this.rootSerializer.serialize(param);
} else {
return this.rootSerializer.serialize(value);
}
Expand Down
Expand Up @@ -71,7 +71,7 @@ public class SwaggerToProtoGenerator {
// not java package
// better to be: app_${app}.mid_{microservice}.sid_{schemaId}
public SwaggerToProtoGenerator(String protoPackage, Swagger swagger) {
this.protoPackage = protoPackage;
this.protoPackage = escapePackageName(protoPackage);
this.swagger = swagger;
}

Expand All @@ -92,6 +92,21 @@ public Proto convert() {
return createProto();
}

public static String escapePackageName(String name) {
return name.replaceAll("\\-", "_");
}

public static String escapeMessageName(String name) {
return name.replaceAll("\\.", "_");
}

public static boolean isValidEnum(String name) {
if (name.contains(".") || name.contains("-")) {
return false;
}
return true;
}

private void convertDefinitions() {
if (swagger.getDefinitions() == null) {
return;
Expand Down Expand Up @@ -217,7 +232,7 @@ private String generateWrapPropertyName(String prefix, Property property) {
}

// message name cannot have . (package separator)
return prefix + StringUtils.capitalize(convertSwaggerType(adapter).replaceAll("\\.", "_"));
return prefix + StringUtils.capitalize(escapeMessageName(convertSwaggerType(adapter)));
}

private void wrapPropertyToMessage(String protoName, Object property) {
Expand All @@ -242,7 +257,12 @@ private void createEnum(String enumName, List<String> enums) {

appendLine(msgStringBuilder, "enum %s {", enumName);
for (int idx = 0; idx < enums.size(); idx++) {
appendLine(msgStringBuilder, " %s =%d;", enums.get(idx), idx);
if (isValidEnum(enums.get(idx))) {
appendLine(msgStringBuilder, " %s =%d;", enums.get(idx), idx);
} else {
throw new IllegalStateException(
String.format("enum class [%s] name [%s] not supported by protobuffer.", enumName, enums.get(idx)));
}
}
appendLine(msgStringBuilder, "}");
}
Expand Down
Expand Up @@ -45,4 +45,15 @@ public void convert() throws IOException {
Assert.assertEquals(protoContent.replaceAll("\r\n", "\n"),
new ProtoToStringGenerator(proto).protoToString().replaceAll("\r\n", "\n"));
}

@Test
public void testEscape() {
Assert.assertEquals("hello_my_service", SwaggerToProtoGenerator.escapeMessageName("hello.my.service"));
Assert.assertEquals("hello_my_service", SwaggerToProtoGenerator.escapeMessageName("hello_my_service"));
Assert.assertEquals("hello.my_service", SwaggerToProtoGenerator.escapePackageName("hello.my-service"));
Assert.assertEquals("hello.test.test", SwaggerToProtoGenerator.escapePackageName("hello.test.test"));
Assert.assertEquals(false, SwaggerToProtoGenerator.isValidEnum("hello.test.test"));
Assert.assertEquals(false, SwaggerToProtoGenerator.isValidEnum("hello.my-service"));
Assert.assertEquals(true, SwaggerToProtoGenerator.isValidEnum("My_ENum"));
}
}

This file was deleted.

Expand Up @@ -287,7 +287,7 @@ public void afterRegistryInstance(MicroserviceInstanceRegisterTask microserviceI
status = SCBStatus.UP;
triggerEvent(EventType.AFTER_REGISTRY);
EventManager.unregister(this);
LOGGER.info("ServiceComb is ready.");
LOGGER.warn("ServiceComb is ready.");
}
}
});
Expand Down
Expand Up @@ -86,7 +86,7 @@ public class OperationConfig {
private long nanoRestRequestWaitInPoolTimeout;

@InjectProperty(keys = {
"operation.${service}.transport", // Deprecated
"operation${op-priority}.transport", // Deprecated
"references.transport${op-priority}"
})
private String transport;
Expand Down
4 changes: 4 additions & 0 deletions coverage-reports/pom.xml
Expand Up @@ -106,6 +106,10 @@
<groupId>org.apache.servicecomb</groupId>
<artifactId>transport-rest-servlet</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>transport-highway</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>common-protobuf</artifactId>
Expand Down
46 changes: 46 additions & 0 deletions demo/demo-jaxrs/jaxrs-client/pom.xml
Expand Up @@ -50,5 +50,51 @@
<properties>
<demo.main>org.apache.servicecomb.demo.jaxrs.client.JaxrsClient</demo.main>
</properties>
<build>
<plugins>
<!-- Because other demos depend on this module, if add classpath, the depended module will fail dual to spring configuration files duplication.
So, we remove maven-dependency-plugin to make all integration tests run. In your applications, still need to add this to make packaged jar files run.
-->
<!--
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
-->
<plugin>
<groupId>com.github.odavid.maven.plugins</groupId>
<artifactId>mixin-maven-plugin</artifactId>
<configuration>
<mixins>
<mixin>
<groupId>org.apache.servicecomb.demo</groupId>
<artifactId>docker-run-config</artifactId>
<version>${project.version}</version>
</mixin>
</mixins>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>docker</id>
<properties>
<demo.service.name>jaxrs-server</demo.service.name>
<demo.vm.options>
-Dcse.highway.address=0.0.0.0:7070
-Dcse.rest.address=0.0.0.0:8080?sslEnabled=false
</demo.vm.options>
</properties>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>