springdoc-openapi icon indicating copy to clipboard operation
springdoc-openapi copied to clipboard

Missing childInterestList Field in Generated OpenAPI Schema for Recursive Data Structure

Open xuxin1458-byte opened this issue 10 months ago • 1 comments

Describe the bug

When generating OpenAPI documentation for Java classes containing recursive/circular references, the generator incorrectly omits certain fields from the generated schema. Specifically, in the InterestDataVo class, the childInterestList field (of type List<InterestDataVo>) is completely missing from the generated OpenAPI documentation, despite being properly defined in the Java source code. This leads to incomplete and inaccurate API documentation, which can cause issues for client code generation and API consumers.

To Reproduce

Steps to reproduce the behavior:

  1. Spring Boot Version: 3.5.9
  2. SpringDoc OpenAPI Modules and Versions:2.8.14
  3. Actual Result in OpenAPI JSON:
    "InterestDataVo": {
      "type": "object",
      "description": "用户兴趣Vo",
      "properties": {
        "interest": {
          "$ref": "#/components/schemas/UserInterestVo",
          "description": "用户兴趣"
        }
        // Note: childInterestList field is missing
      }
    }
    

4.Expected Result in OpenAPI JSON:

"InterestDataVo": {
  "type": "object",
  "description": "用户兴趣Vo",
  "properties": {
    "interest": {
      "$ref": "#/components/schemas/UserInterestVo",
      "description": "用户兴趣"
    },
    "childInterestList": {
      "type": "array",
      "description": "用户子兴趣",
      "items": {
        "$ref": "#/components/schemas/InterestDataVo"
      }
    }
  }
}

5.Sample Code to Reproduce:

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;
import java.util.List;

/**
 * 用户兴趣Vo
 */
@Data
@Schema(description = "用户兴趣Vo")
public class InterestDataVo implements Serializable {
    @Serial
    private static final long serialVersionUID = -2458650740310623068L;
    
    @Schema(description = "用户兴趣")
    private UserInterestVo interest;
    
    @Schema(description = "用户子兴趣")
    private List<InterestDataVo> childInterestList;
}
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/interests")
@Tag(name = "兴趣管理")
public class InterestController {
    
    @GetMapping("/tree")
    @Operation(summary = "获取兴趣树")
    public InterestDataVo getInterestTree() {
        // Implementation returns an InterestDataVo with childInterestList
        return new InterestDataVo();
    }
}

Expected behavior

The OpenAPI documentation should accurately reflect all fields defined in the Java class, including recursive references. The childInterestList field should appear in the generated schema with a proper reference to handle the circular dependency, as per OpenAPI 3.0+ specifications.

Screenshots

Not applicable (this is a schema generation issue).

Additional context

This issue appears to be specific to how the OpenAPI generator handles circular/recursive references. It seems to drop fields that create recursive structures instead of properly modeling them with $ref pointers. This is particularly problematic for tree-like data structures (such as categories, organizational hierarchies, nested comments, etc.) where recursive references are fundamental to the data model.

Workaround: Currently, we have to manually edit the generated OpenAPI JSON or use post-processing scripts to add the missing fields, which is not sustainable.

Environment:

Java Version: 21

Build Tool: Maven 3.8.6

Operating System: Windows 10 / macOS 12.0

Related Issues: We haven't found any existing issues that match this specific problem in the springdoc-openapi issue tracker.

xuxin1458-byte avatar Jan 21 '26 10:01 xuxin1458-byte

I solved this problem by changing it to 2.8.15.

xuxin1458-byte avatar Jan 21 '26 10:01 xuxin1458-byte