TongchengOpenSource / smart-doc

Smart-doc is a java restful api document generation tool. Smart-doc is based on interface source code analysis to generate interface documentation, completely zero-injection.
https://smart-doc-group.github.io/#/
Apache License 2.0
1.42k stars 278 forks source link

When the return type of an interface is a generic class with an inferred type "?", the generated documentation's Response-fields section cannot produce corresponding information. #67

Closed Machaing closed 3 years ago

Machaing commented 3 years ago

Expected Result

Expected: When the return type of an interface is a generic class with an inferred type "?", the generated documentation should be consistent with that of an accurately specified generic.

Current Result

The documentation generated for a generic class with an inferred type "?" is inconsistent with that of an accurately specified generic.

The following interface code mainly involves two almost identical interfaces, both returning a generic class but with different generic types. In terms of data returned when calling via Postman, the results are the same. However, the descriptions of the return fields in the generated interface documentation differ.

Code Example

  1. Interface Class

    /**
    * Test API1
    */
    @RestController
    @RequestMapping("/test")
    public class TestController {
    
    /**
     * Get user list interface 1--generic inference
     *
     * @return com.example.demo.Model.APIResult<com.example.demo.Model.TestUserResp> helloWorld
     * @author mzy
     */
    @GetMapping("1")
    public APIResult<?> test1Controller() {
    
        ArrayList<UserResp> mzy = new ArrayList<UserResp>() {{
            UserResp userResp = new UserResp();
            userResp.setId("1");
            userResp.setUserName("mzy");
            userResp.setAge(11);
            add(userResp);
        }};
    
        return APIResult.success(mzy);
    }
    
    /**
     * Get user list interface 2--explicit return type
     *
     * @return com.example.demo.Model.APIResult<com.example.demo.Model.TestUserResp> helloWorld
     * @author mzy
     */
    @GetMapping("2")
    public APIResult<List<UserResp>> test2Controller() {
    
        ArrayList<UserResp> mzy = new ArrayList<UserResp>() {{
            UserResp userResp = new UserResp();
            userResp.setId("1");
            userResp.setUserName("mzy");
            userResp.setAge(11);
            add(userResp);
        }};
    
        return APIResult.success(mzy);
    }
    }
  2. APIResult Class

    @Data
    @Builder
    public class APIResult<T> {
    
    /**
     * Call status code
     */
    private String code;
    
    /**
     * Call return message
     */
    private String msg;
    
    /**
     * Call return data instance
     */
    private T data;
    
    private static String COMMON_SUCCESS_CODE = "12";
    
    static public <T> APIResult<T> success(T data) {
        return new APIResult<>(null, COMMON_SUCCESS_CODE, data);
    }
    }
  3. UserResp Class

    @Data
    public class UserResp {
    /**
     * User id
     */
    String id;
    
    /**
     * User name
     */
    String userName;
    
    /**
     * User age
     */
    int age;
    }

Bug Impact Description

Inferred generic return classes should be considered for implementation, as they are allowed in actual calls.

Environment Used

  1. Springboot 2.4.0
  2. Jdk 1.8
  3. smart-doc-maven-plugin 1.2.2
  4. Other non-core dependencies include lombok, spring-boot-devtools, starter-web
Machaing commented 3 years ago

The comparison of the generated document interfaces is as follows: image

image

shalousun commented 3 years ago

@Machaing, please read the smart-doc documentation to gain a detailed understanding of its implementation principles and to learn at which stage of the project lifecycle smart-doc generates documentation. Of course, if you have figured out an effective solution to address the issue you raised, feel free to provide a detailed implementation plan.