question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nested lists are not handled

See original GitHub issue

Describe the bug We have a simple DTO mapping which includes a list objects of the same data type. This is the class:

@Data
@Builder
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Schema(name = "CourseDraftNode")
public class CourseDraftNodeResponse implements Comparable<CourseDraftNodeResponse> {

    @NotNull
    private UUID key;
    @NotNull
    private OffsetDateTime createdAt;
    @NotNull
    private OffsetDateTime updatedAt;

    private UUID parentKey;
    @NotNull
    private UUID draftKey;

    @NotNull
    private CourseNodeType type;
    @NotNull
    private Integer position;
    @NotNull
    private String title;
    private String description;

    @NotNull
    private List<CourseDraftNodeItemResponse> items = new ArrayList<>();
    @NotNull
    private List<CourseDraftNodeResponse> children = new ArrayList<>();

    @Override
    public int compareTo(CourseDraftNodeResponse otherNodeResponse) {
        // Sort by parentKey nulls first and then by position
        if (Objects.isNull(parentKey) && Objects.nonNull(otherNodeResponse.getParentKey())) {
            return -1;
        } else if (Objects.nonNull(parentKey) && Objects.isNull(otherNodeResponse.getParentKey())) {
            return 1;
        } else {
            return position.compareTo(otherNodeResponse.getPosition());
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof CourseDraftNodeResponse that)) return false;
        return getKey().equals(that.getKey());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getKey());
    }
}

The list children is not included in the generated json. The list items is visible, but not children. We tried other classes and added Lists. Whenever we want to add a list with the same data type as the object to model a tree structure, the list is not present in the generated api specification.

Screenshot 2022-12-10 at 17 16 25 This is how the the class looks like if you inspect it with swagger ui.

If I change the type from List to ArrayList in the response DTO, the list is added to the generated JSON. Screenshot 2022-12-10 at 17 39 25

In the swagger UI it is displayed as a list of strings… this is really a strange behaviour. Screenshot 2022-12-10 at 17 40 28 Screenshot 2022-12-10 at 17 40 48

To Reproduce Steps to reproduce the behavior: Create a class which contains a list of the same data type as the class itself. Create a rest endpoint which returns this an object of this class. Inspect the generated JSON and the swagger UI.

  • What version of spring-boot you are using? 2.7.4

  • What modules and versions of springdoc-openapi are you using?

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.6.11</version>
</dependency>
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-security</artifactId>
    <version>1.6.11</version>
</dependency>
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-data-rest</artifactId>
    <version>1.6.11</version>
</dependency>
  • What is the actual and the expected result using OpenAPI Description (yml or json)? json

  • Provide with a sample code (HelloController) or Test that reproduces the problem

@RestController
@Tag(name = "Course Draft")
@RequestMapping("/test")
@RequiredArgsConstructor
public class TestController {
    @GetMapping
    public CourseDraftNodeResponse getCourseDraft() {
        return CourseDraftNodeResponse.builder().build();
    }
}

Expected behavior The nested list should be added to the json response. Swagger should display not string as data type but the actual object.

Additional context Lombok is used.

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
LReinachercommented, Dec 10, 2022

we are facing the same issue 😦

0reactions
LexanRedcommented, Dec 14, 2022

Annotating the list with @ArraySchema seems to fix the problem. The generated JSON looks fine.

@ArraySchema
private List<CourseNodeResponse> children = new ArrayList<>();
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to handle list with nested lists and strings elements?
I want to flat a list that has nested and not nested elements. From this solution I've tried and it works if all...
Read more >
27.5. Handling Nested Lists: the Flatten Function - SCons
SCons supports a Flatten function which takes an input Python sequence (list or tuple) and returns a flattened list containing just the individual...
Read more >
Why are more highly nested lists often not more memory and ...
I would expect more highly nested lists, being deeper and more complex than flatter lists, to take up more space (bytecount) and take...
Read more >
Froala not handling nested lists properly · Issue #1442 - GitHub
Froala is not properly maintaining the nested list. Replacing the "edit me" text causes the underlying HTML to be malformed and the edits...
Read more >
Python Nested List - Learn By Example
A list can contain any sort object, even another list (sublist), which in turn can contain sublists themselves, and so on. This is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found