Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions service-registry/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,9 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>swagger-generator-core</artifactId>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicated

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your suggestions

</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,14 @@ public boolean isAlwaysOverrideSchema() {
return property.get();
}

public boolean isIgnoreSwaggerDifference() {
DynamicBooleanProperty property =
DynamicPropertyFactory.getInstance()
.getBooleanProperty("servicecomb.service.registry.instance.ignoreSwaggerDifference",
false);
return property.get();
}

public boolean isPreferIpAddress() {
DynamicBooleanProperty property =
DynamicPropertyFactory.getInstance()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@

import javax.ws.rs.core.Response.Status;

import io.swagger.models.Swagger;
import org.apache.servicecomb.foundation.common.base.ServiceCombConstants;
import org.apache.servicecomb.serviceregistry.RegistryUtils;
import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
import org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse;
import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient;
import org.apache.servicecomb.serviceregistry.client.http.Holder;
import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig;
import org.apache.servicecomb.swagger.SwaggerUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -261,6 +263,27 @@ private boolean compareAndReRegisterSchema(Entry<String, String> localSchemaEntr
String scSchemaContent = srClient.getSchema(microservice.getServiceId(), scSchema.getSchemaId());
String localSchemaContent = localSchemaEntry.getValue();

//if content of local schema and service center schema is equal then return true.
if (!StringUtils.isEmpty(scSchemaContent) && !StringUtils.isEmpty(localSchemaContent)) {
Swagger scSwagger = SwaggerUtils.parseSwagger(scSchemaContent);
Swagger localSwagger = SwaggerUtils.parseSwagger(localSchemaContent);
if (scSwagger.equals(localSwagger)) {
return true;
}
}

//if the content of local schema and service center schema is different. But the value of isIgnoreSwaggerDifference is true.
if (ServiceRegistryConfig.INSTANCE.isIgnoreSwaggerDifference()) {
LOGGER.warn(
"service center schema and local schema both are different:\n service center "
+ "schema:\n[{}]\n local schema:\n[{}]\nYou have configured to ignore difference "
+ "check. It's recommended to increment microservice version before deploying when "
+ "schema change.",
scSchemaContent,
localSchemaContent);
return true;
}

LOGGER.warn(
"service center schema and local schema both are different:\n service center schema:\n[{}\n local schema:\n[{}]",
scSchemaContent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse;
import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient;
import org.apache.servicecomb.serviceregistry.client.http.Holder;
import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -702,7 +703,245 @@ public void testLocalSchemaAndServiceCenterSchemaDiff(@Mocked ServiceRegistryCli
" x-java-class: \"org.apache.servicecomb.demo.validator.Student\"]", events.get(5).getMessage());

}

Assert.assertEquals(true, isIlleagalException);
}

@Test
public void testLocalSchemaAndServiceCenterSchemaIgnoreDiff(@Mocked ServiceRegistryClient srClient,
@Mocked ServiceRegistryConfig serviceRegistryConfig) {

Microservice otherMicroservice = new Microservice();
otherMicroservice.setAppId(microservice.getAppId());
otherMicroservice.setServiceName("ms1");
otherMicroservice.addSchema("s1", "abcd");

List<GetSchemaResponse> list = new ArrayList<>();
GetSchemaResponse resp = new GetSchemaResponse();
resp.setSchemaId("s1");
resp.setSummary("c1188d709631a9038874f9efc6eb894f");
list.add(resp);
Holder<List<GetSchemaResponse>> onlineSchemasHolder = new Holder<>();
onlineSchemasHolder.setValue(list).setStatusCode(200);

new Expectations() {
{
serviceRegistryConfig.isIgnoreSwaggerDifference();
result = true;
srClient.getMicroserviceId(anyString, anyString, anyString, anyString);
result = "serviceId";
srClient.getMicroservice(anyString);
result = otherMicroservice;
srClient.getSchemas(anyString);
result = onlineSchemasHolder;
srClient.getSchema(anyString, anyString);
result = "swagger: \"2.0\"\n" +
"info:\n" +
" version: \"1.0.0\"\n" +
" title: \"swagger definition for org.apache.servicecomb.demo.jaxrs.server.RequestClientTimeOut\"\n" +
" x-java-interface: \"cse.gen.jaxrstest.jaxrs.clientreqtimeout.RequestClientTimeOutIntf\"\n" +
"basePath: \"/clientreqtimeout\"\n" +
"consumes:\n" +
"- \"application/json\"\n" +
"produces:\n" +
"- \"application/json\"\n" +
"paths:\n" +
" /sayhello:\n" +
" post:\n" +
" operationId: \"sayHello\"\n" +
" parameters:\n" +
" - in: \"body\"\n" +
" name: \"student\"\n" +
" required: false\n" +
" schema:\n" +
" $ref: \"#/definitions/Student\"\n" +
" responses:\n" +
" 200:\n" +
" description: \"response of 200\"\n" +
" schema:\n" +
" $ref: \"#/definitions/Student\"\n" +
"definitions:\n" +
" Student:\n" +
" type: \"object\"\n" +
" required:\n" +
" - \"name\"\n" +
" properties:\n" +
" name:\n" +
" type: \"string\"\n" +
" age:\n" +
" type: \"integer\"\n" +
" format: \"int32\"\n" +
" maximum: 20\n" +
" x-java-class: \"org.apache.servicecomb.demo.validator.Student\"";
}
};

microservice.addSchema("s1",
"swagger: \"2.0\"\n" +
"info:\n" +
" version: \"1.0.0\"\n" +
" title: \"swagger definition for org.apache.servicecomb.demo.jaxrs.server.RequestClientTimeOut\"\n" +
" x-java-interface: \"cse.gen.jaxrstest.jaxrs.clientreqtimeout.RequestClientTimeOutIntf\"\n" +
"basePath: \"/clientreqtimeout\"\n" +
"consumes:\n" +
"- \"application/json\"\n" +
"produces:\n" +
"- \"application/json\"\n" +
"paths:\n" +
" /sayhello:\n" +
" post:\n" +
" operationId: \"sayHello\"\n" +
" parameters:\n" +
" - in: \"body\"\n" +
" name: \"student\"\n" +
" required: false\n" +
" schema:\n" +
" $ref: \"#/definitions/Student\"\n" +
" responses:\n" +
" 200:\n" +
" description: \"response of 200\"\n" +
" schema:\n" +
" type: \"string\"\n" +
"definitions:\n" +
" Student:\n" +
" type: \"object\"\n" +
" required:\n" +
" - \"name\"\n" +
" properties:\n" +
" name:\n" +
" type: \"string\"\n" +
" age:\n" +
" type: \"integer\"\n" +
" format: \"int32\"\n" +
" maximum: 20\n" +
" x-java-class: \"org.apache.servicecomb.demo.validator.Student\"");
microservice.setEnvironment("prod");
MicroserviceRegisterTask registerTask = new MicroserviceRegisterTask(eventBus, srClient, microservice);

Boolean isIlleagalException = false;

try {
registerTask.run();
} catch (IllegalStateException exception) {
isIlleagalException = true;
}

Assert.assertEquals(false, isIlleagalException);
}

@Test
public void testLocalSchemaAndServiceCenterSchemaNoDiff(@Mocked ServiceRegistryClient srClient) {

Microservice otherMicroservice = new Microservice();
otherMicroservice.setAppId(microservice.getAppId());
otherMicroservice.setServiceName("ms1");
otherMicroservice.addSchema("s1", "abcd");

List<GetSchemaResponse> list = new ArrayList<>();
GetSchemaResponse resp = new GetSchemaResponse();
resp.setSchemaId("s1");
resp.setSummary("c1188d709631a9038874f9efc6eb894f");
list.add(resp);
Holder<List<GetSchemaResponse>> onlineSchemasHolder = new Holder<>();
onlineSchemasHolder.setValue(list).setStatusCode(200);

new Expectations() {
{
srClient.getMicroserviceId(anyString, anyString, anyString, anyString);
result = "serviceId";
srClient.getMicroservice(anyString);
result = otherMicroservice;
srClient.getSchemas(anyString);
result = onlineSchemasHolder;
srClient.getSchema(anyString, anyString);
result = "swagger: \"2.0\"\n" +
"info:\n" +
" version: \"1.0.0\"\n" +
" title: \"swagger definition for org.apache.servicecomb.demo.jaxrs.server.RequestClientTimeOut\"\n" +
" x-java-interface: \"cse.gen.jaxrstest.jaxrs.clientreqtimeout.RequestClientTimeOutIntf\"\n" +
"basePath: \"/clientreqtimeout\"\n" +
"consumes:\n" +
"- \"application/json\"\n" +
"produces:\n" +
"- \"application/json\"\n" +
"paths:\n" +
" /sayhello:\n" +
" post:\n" +
" operationId: \"sayHello\"\n" +
" parameters:\n" +
" - in: \"body\"\n" +
" name: \"student\"\n" +
" required: false\n" +
" schema:\n" +
" $ref: \"#/definitions/Student\"\n" +
" responses:\n" +
" 200:\n" +
" description: \"response of 200\"\n" +
" schema:\n" +
" type: \"string\"\n" +
"definitions:\n" +
" Student:\n" +
" type: \"object\"\n" +
" required:\n" +
" - \"name\"\n" +
" properties:\n" +
" name:\n" +
" type: \"string\"\n" +
" age:\n" +
" type: \"integer\"\n" +
" format: \"int32\"\n" +
" maximum: 20\n" +
" x-java-class: \"org.apache.servicecomb.demo.validator.Student\"";
}
};

microservice.addSchema("s1",
"swagger: \"2.0\"\n" +
"info:\n" +
" version: \"1.0.0\"\n" +
" title: \"swagger definition for org.apache.servicecomb.demo.jaxrs.server.RequestClientTimeOut\"\n" +
" x-java-interface: \"cse.gen.jaxrstest.jaxrs.clientreqtimeout.RequestClientTimeOutIntf\"\n" +
"basePath: \"/clientreqtimeout\"\n" +
"consumes:\n" +
"- \"application/json\"\n" +
"produces:\n" +
"- \"application/json\"\n" +
"paths:\n" +
" /sayhello:\n" +
" post:\n" +
" operationId: \"sayHello\"\n" +
" parameters:\n" +
" - in: \"body\"\n" +
" name: \"student\"\n" +
" required: false\n" +
" schema:\n" +
" $ref: \"#/definitions/Student\"\n" +
" responses:\n" +
" 200:\n" +
" description: \"response of 200\"\n" +
" schema:\n" +
" type: \"string\"\n" +
"definitions:\n" +
" Student:\n" +
" properties:\n" +
" name:\n" +
" type: \"string\"\n" +
" age:\n" +
" type: \"integer\"\n" +
" format: \"int32\"\n" +
" maximum: 20\n" +
" type: \"object\"\n" +
" required:\n" +
" - \"name\"\n" +
" x-java-class: \"org.apache.servicecomb.demo.validator.Student\"");
microservice.setEnvironment("prod");
MicroserviceRegisterTask registerTask = new MicroserviceRegisterTask(eventBus, srClient, microservice);
Boolean isIlleagalException = false;

try {
registerTask.run();
} catch (IllegalStateException exception) {
isIlleagalException = true;
}
Assert.assertEquals(false, isIlleagalException);
}
}