[ 
https://issues.apache.org/jira/browse/SCB-206?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16589539#comment-16589539
 ] 

ASF GitHub Bot commented on SCB-206:
------------------------------------

liubao68 commented on a change in pull request #882: [SCB-206] Support setting 
produces and consumes by @Api
URL: 
https://github.com/apache/incubator-servicecomb-java-chassis/pull/882#discussion_r212156567
 
 

 ##########
 File path: 
swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/generator/core/processor/annotation/ApiProcessor.java
 ##########
 @@ -17,18 +17,69 @@
 
 package org.apache.servicecomb.swagger.generator.core.processor.annotation;
 
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
 import org.apache.servicecomb.swagger.generator.core.ClassAnnotationProcessor;
 import org.apache.servicecomb.swagger.generator.core.SwaggerGenerator;
 import org.springframework.util.StringUtils;
 
 import io.swagger.annotations.Api;
+import io.swagger.models.Swagger;
 
 public class ApiProcessor implements ClassAnnotationProcessor {
   @Override
   public void process(Object annotation, SwaggerGenerator swaggerGenerator) {
     Api api = (Api) annotation;
 
     setTags(api, swaggerGenerator);
+    // except for @Api, @RequestMapping can also set consumes and produces
+    // @RequestMapping takes HIGHER priority than @Api for legacy reason
+    processConsumes(api, swaggerGenerator.getSwagger());
+    processProduces(api, swaggerGenerator.getSwagger());
+  }
+
+  private void processProduces(Api api, Swagger swagger) {
+    List<String> validProducesList = getValidStringList(api.produces());
+    if (isBlank(swagger.getProduces()) && !validProducesList.isEmpty()) {
+      swagger.setProduces(validProducesList);
+    }
+  }
+
+  private void processConsumes(Api api, Swagger swagger) {
+    List<String> validConsumesList = getValidStringList(api.consumes());
+    if (isBlank(swagger.getConsumes()) && !validConsumesList.isEmpty()) {
+      swagger.setConsumes(validConsumesList);
+    }
+  }
+
+  /**
+   * Split {@link Api#consumes} and {@link Api#produces}, and filter empty 
items.
+   */
+  private List<String> getValidStringList(String rawString) {
+    return Stream.of(rawString.split(","))
+        .filter(s -> !StringUtils.isEmpty(s))
+        .map(String::trim)
+        .collect(Collectors.toList());
+  }
+
+  /**
+   * @return true if {@code stringList} is empty or each element of {@code 
stringList} is empty;
+   * otherwise false.
+   */
+  private boolean isBlank(List<String> stringList) {
+    boolean isEmpty = null == stringList || stringList.isEmpty();
+    if (isEmpty) {
+      return true;
+    }
+
+    for (String s : stringList) {
+      if (StringUtils.isEmpty(s)) {
 
 Review comment:
   seems s can never be empty

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


> Support @Api to specify produces and consumes
> ---------------------------------------------
>
>                 Key: SCB-206
>                 URL: https://issues.apache.org/jira/browse/SCB-206
>             Project: Apache ServiceComb
>          Issue Type: Improvement
>          Components: Java-Chassis
>            Reporter: YaoHaishi
>            Assignee: YaoHaishi
>            Priority: Trivial
>             Fix For: java-chassis-1.1.0
>
>
> At present, ServiceComb only process the `tags` of 
> io.swagger.annotations.Api. If user specify produces and consumes in `@Api`, 
> nothing will happen. 
> The support to these two fields should be added if necessary. The hierarchy 
> relationship of `SwaggerDefinition`, `Api`, `ApiOperation` is that `Api` 
> overrides `SwaggerDefinition`, `ApiOperation` overrides `Api`.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to