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_r270617934
 
 

 ##########
 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)
 
 Review comment:
   maybe  instanceof is better ?

----------------------------------------------------------------
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