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_r270618172
##########
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);
Review comment:
why produces use method name **convertConsumes**, maybe the method name can
be changed more appropriately
----------------------------------------------------------------
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