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
26 changes: 23 additions & 3 deletions core/src/main/java/io/servicecomb/core/definition/SchemaMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,21 @@
import java.util.List;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.servicecomb.core.Handler;
import io.servicecomb.core.exception.ExceptionUtils;
import io.servicecomb.swagger.generator.core.utils.ClassUtils;
import io.servicecomb.foundation.common.utils.ReflectUtils;

import io.servicecomb.swagger.generator.core.utils.ClassUtils;
import io.swagger.models.HttpMethod;
import io.swagger.models.Operation;
import io.swagger.models.Path;
import io.swagger.models.Swagger;

public class SchemaMeta extends CommonService<OperationMeta> {
private static final Logger LOGGER = LoggerFactory.getLogger(SchemaMeta.class);

// 如果要生成class,使用这个package
private String packageName;

Expand Down Expand Up @@ -80,9 +84,25 @@ private void initOperations() {
throw ExceptionUtils.operationIdInvalid(getSchemaId(), strPath);
}

// io.servicecomb.swagger.engine.SwaggerEnvironment.createConsumer(Class<?>, Class<?>)
// io.servicecomb.swagger.engine.SwaggerEnvironment.createProducer(Object, Swagger)
// had make sure that consumer/swagger or producer/swagger can work
//
// in this place, do not throw exception when method not exists
// eg:
// swagger interface is a.b.c, and consumer interface is a.b.c too.
// version 1, there are the same
// version 2, producer add a new operation, that means swagger have more operation than consumer interface a.b.c
// interface a.b.c in consumer process is the old interface
// so for swagger, can not do any valid check here
// only need to save found method, that's enough.
Method method = ReflectUtils.findMethod(swaggerIntf, operation.getOperationId());
if (method == null) {
throw ExceptionUtils.operationNotExist(getSchemaId(), operation.getOperationId());
LOGGER.warn("method {} not found in swagger interface {}, schemaId={}",
operation.getOperationId(),
swaggerIntf.getName(),
getSchemaId());
continue;
}

String httpMethod = operationEntry.getKey().name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public class ExceptionUtils {

private static final String CSE_SCHEMA_OPERATION_ID_INVALID = "cse.schema.operation.id.invalid";

private static final String CSE_SCHEMA_OPERATION_NOT_EXIST = "cse.schema.operation.not.exist";

private static final String CSE_HANDLER_REF_NOT_EXIST = "cse.handler.ref.not.exist";

private static final String CSE_PRODUCER_OPERATION_NOT_EXIST = "cse.producer.operation.not.exist";
Expand All @@ -43,8 +41,6 @@ public class ExceptionUtils {
static {
ERROR_DESC_MGR.register(CSE_HANDLER_REF_NOT_EXIST, "Handler not exist, id=%s");
ERROR_DESC_MGR.register(CSE_SCHEMA_OPERATION_ID_INVALID, "OperationId is invalid, schemaId=%s, path=%s");
ERROR_DESC_MGR.register(CSE_SCHEMA_OPERATION_NOT_EXIST,
"Operation not exist in interface, schemaId=%s, operation=%s");
ERROR_DESC_MGR.register(CSE_PRODUCER_OPERATION_NOT_EXIST,
"Producer operation not exist, schemaId=%s, operationName=%s");
ERROR_DESC_MGR.register(CSE_LB_NO_AVAILABLE_ADDRESS,
Expand Down Expand Up @@ -83,12 +79,6 @@ public static CseException operationIdInvalid(String schemaId, String path) {
path);
}

public static CseException operationNotExist(String schemaId, String operationName) {
return createCseException(CSE_SCHEMA_OPERATION_NOT_EXIST,
schemaId,
operationName);
}

public static CseException handlerRefNotExist(String id) {
return createCseException(CSE_HANDLER_REF_NOT_EXIST, id);
}
Expand Down
3 changes: 0 additions & 3 deletions core/src/test/java/io/servicecomb/core/TestException.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ public void testExceptionUtils() {
oExeception = ExceptionUtils.operationIdInvalid("cse.double.error", "what path are you talking about");
Assert.assertEquals("cse.schema.operation.id.invalid", oExeception.getCode());

oExeception = ExceptionUtils.operationNotExist("cse.double.error", "what path are you talking about");
Assert.assertEquals("cse.schema.operation.not.exist", oExeception.getCode());

oExeception = ExceptionUtils.handlerRefNotExist("cse.double.error");
Assert.assertEquals("cse.handler.ref.not.exist", oExeception.getCode());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2017 Huawei Technologies Co., Ltd
*
* Licensed 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 io.servicecomb.core.definition;

import org.junit.Assert;
import org.junit.Test;

import io.servicecomb.swagger.generator.core.SwaggerConst;
import io.servicecomb.swagger.generator.core.unittest.UnitTestSwaggerUtils;
import io.swagger.models.Swagger;
import mockit.Mocked;

public class TestSchemaMeta {
static interface V1 {
void a();
}

static interface V2 extends V1 {
void b();
}

@Test
public void testMethodNotExist(@Mocked OperationMeta operationMeta) {
Swagger swagger = UnitTestSwaggerUtils.generateSwagger(V2.class).getSwagger();
// make swagger have more operations than interface
swagger.getInfo().setVendorExtension(SwaggerConst.EXT_JAVA_INTF, V1.class.getName());

MicroserviceMeta microserviceMeta = new MicroserviceMeta("app:ms");
SchemaMeta schemaMeta = new SchemaMeta(swagger, microserviceMeta, "schemaId");
Assert.assertEquals(1, schemaMeta.getOperations().size());
Assert.assertNotNull(schemaMeta.findOperation("a"));
}
}