heyile commented on a change in pull request #1157:  [SCB-1225][WIP][WEAK] 
swagger generator core not depend on dynamic class
URL: 
https://github.com/apache/servicecomb-java-chassis/pull/1157#discussion_r270618231
 
 

 ##########
 File path: 
swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/SwaggerUtils.java
 ##########
 @@ -141,4 +162,225 @@ public static void correctResponses(Swagger swagger) {
       }
     }
   }
+
+  public static Map<String, Property> getBodyProperties(Swagger swagger, 
Parameter parameter) {
+    if (!(parameter instanceof BodyParameter)) {
+      return null;
+    }
+
+    Model model = ((BodyParameter) parameter).getSchema();
+    if (model instanceof RefModel) {
+      model = swagger.getDefinitions().get(((RefModel) model).getSimpleRef());
+    }
+
+    if (model instanceof ModelImpl) {
+      return model.getProperties();
+    }
+
+    return null;
+  }
+
+  public static void addDefinitions(Swagger swagger, Type paramType) {
+    Map<String, Model> models = 
ModelConverters.getInstance().readAll(paramType);
+    for (Entry<String, Model> entry : models.entrySet()) {
+      swagger.addDefinition(entry.getKey(), entry.getValue());
+    }
+  }
+
+  public static void setParameterType(Swagger swagger, Type type, 
AbstractSerializableParameter<?> parameter) {
+    addDefinitions(swagger, type);
+    Property property = ModelConverters.getInstance().readAsProperty(type);
+
+    if (isComplexProperty(property)) {
+      // cannot set a simple parameter(header, query, etc.) as complex type
+      String msg = String
+          .format("not allow complex type for %s parameter, type=%s.", 
parameter.getIn(), type.getTypeName());
+      throw new IllegalStateException(msg);
+    }
+    parameter.setProperty(property);
+  }
+
+  /**
+   * Set param type info. For {@linkplain javax.ws.rs.BeanParam BeanParam} 
scenario.
+   *
+   * @param paramType type of the swagger parameter
+   * @param parameter swagger parameter
+   */
+  public static void setParameterType(Type paramType, 
AbstractSerializableParameter<?> parameter) {
+    Property property = 
ModelConverters.getInstance().readAsProperty(paramType);
+
+    if (isComplexProperty(property)) {
+      // cannot set a simple parameter(header, query, etc.) as complex type
+      throw new IllegalArgumentException(
+          String.format(
+              "not allow such type of param:[%s], param name is [%s]",
+              property.getClass(),
+              parameter.getName()));
+    }
+    parameter.setProperty(property);
+  }
+
+  public static boolean isComplexProperty(Property property) {
+    if (RefProperty.class.isInstance(property) || 
ObjectProperty.class.isInstance(property)
+        || MapProperty.class.isInstance(property)) {
+      return true;
+    }
+
+    if (ArrayProperty.class.isInstance(property)) {
+      return isComplexProperty(((ArrayProperty) property).getItems());
+    }
+
+    return false;
+  }
+
+  public static ModelImpl getModelImpl(Swagger swagger, BodyParameter 
bodyParameter) {
+    Model model = bodyParameter.getSchema();
+    if (model instanceof ModelImpl) {
+      return (ModelImpl) model;
+    }
+
+    if (!(model instanceof RefModel)) {
+      return null;
+    }
+
+    String simpleRef = ((RefModel) model).getSimpleRef();
+    Model targetModel = swagger.getDefinitions().get(simpleRef);
+    return targetModel instanceof ModelImpl ? (ModelImpl) targetModel : null;
+  }
+
+  public static void setCommaConsumes(Swagger swagger, String commaConsumes) {
+    if (org.apache.commons.lang3.StringUtils.isEmpty(commaConsumes)) {
+      return;
+    }
+
+    setConsumes(swagger, commaConsumes.split(","));
+  }
+
+  public static void setConsumes(Operation operation, String... consumes) {
+    List<String> consumeList = convertConsumes(consumes);
+    if (!consumeList.isEmpty()) {
+      operation.setConsumes(consumeList);
+    }
+  }
+
+  public static void setConsumes(Swagger swagger, String... consumes) {
+    List<String> consumeList = convertConsumes(consumes);
+    if (!consumeList.isEmpty()) {
+      swagger.setConsumes(consumeList);
+    }
+  }
+
+  public static List<String> convertConsumes(String... consumes) {
+    return Arrays.stream(consumes)
+        .map(String::trim)
+        .filter(StringUtils::isNotEmpty)
+        .collect(Collectors.toList());
+  }
+
+  public static void setCommaProduces(Swagger swagger, String commaProduces) {
+    if (StringUtils.isEmpty(commaProduces)) {
+      return;
+    }
+
+    setProduces(swagger, commaProduces.split(","));
+  }
+
+  public static void setProduces(Operation operation, String... produces) {
+    // same to consumes
+    List<String> produceList = convertConsumes(produces);
+    if (!produceList.isEmpty()) {
+      operation.setProduces(produceList);
+    }
+  }
+
+  public static void setProduces(Swagger swagger, String... produces) {
+    // same to consumes
+    List<String> produceList = convertConsumes(produces);
+    if (!produceList.isEmpty()) {
+      swagger.setProduces(produceList);
+    }
+  }
+
+  public static boolean hasAnnotation(Class<?> cls, Class<? extends 
Annotation> annotation) {
+    if (cls.getAnnotation(annotation) != null) {
+      return true;
+    }
+
+    for (Method method : cls.getMethods()) {
+      if (method.getAnnotation(annotation) != null) {
+        return true;
+      }
+    }
+
+    return false;
+  }
+
+  public static boolean isRawJsonType(Parameter param) {
+    Object rawJson = 
param.getVendorExtensions().get(SwaggerConst.EXT_RAW_JSON_TYPE);
+    if (Boolean.class.isInstance(rawJson)) {
+      return (boolean) rawJson;
+    }
+    return false;
+  }
+
+  public static String getClassName(Map<String, Object> vendorExtensions) {
+    return getVendorExtension(vendorExtensions, SwaggerConst.EXT_JAVA_CLASS);
+  }
+
+  public static String getInterfaceName(Map<String, Object> vendorExtensions) {
+    return getVendorExtension(vendorExtensions, SwaggerConst.EXT_JAVA_INTF);
+  }
+
+  @SuppressWarnings("unchecked")
+  public static <T> T getVendorExtension(Map<String, Object> vendorExtensions, 
String key) {
+    if (vendorExtensions == null) {
+      return null;
+    }
+
+    return (T) vendorExtensions.get(key);
+  }
+
+  public static boolean isBean(Type type) {
+    if (type == null) {
+      return false;
+    }
+
+    JavaType javaType = TypeFactory.defaultInstance().constructType(type);
+    if (javaType.isContainerType()) {
+      return false;
+    }
+
+    Class<?> cls = javaType.getRawClass();
+    if (ClassUtils.isPrimitiveOrWrapper(cls)) {
+      return false;
+    }
+
+    if (cls == String.class
 
 Review comment:
   can simply 
   ```
       return cls != String.class
               && cls != Date.class
               && cls != LocalDate.class
               && cls != byte[].class
               && !Part.class.isAssignableFrom(cls);
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to