swagger-api / swagger-core

Examples and server integrations for generating the Swagger API Specification, which enables easy access to your REST API
http://swagger.io
Apache License 2.0
7.38k stars 2.18k forks source link

Missing Open API schema property of collection self type #3484

Open lwielek opened 4 years ago

lwielek commented 4 years ago

Version: io.swagger.vore.v3:swagger-core:2.1.1

The model resolver is skipping properties in the specific scenario:

The B collection property is missing in the schema definition

Below code example, that reproduces the issue. DataResponse has property buckets (List<BucketResponse>). BucketResponse has property buckets (List<BucketResponse>). The BucketResponse.buckets property is missing in the OpenAPI schema.


package swagger.model.issue;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.List;

import io.swagger.v3.core.util.Json;
import io.swagger.v3.jaxrs2.integration.XmlWebOpenApiContext;
import io.swagger.v3.oas.integration.SwaggerConfiguration;
import io.swagger.v3.oas.models.OpenAPI;

import static java.util.Collections.singleton;

public class SwaggerModelIssueTest {

    public static void main(String[] args) throws Exception {
        SwaggerConfiguration oasConfig = new SwaggerConfiguration()
                .resourcePackages(singleton("swagger.model.issue"))
                .readAllResources(true);
        OpenAPI openAPI =
                new XmlWebOpenApiContext<>().openApiConfiguration(oasConfig)
                        .init()
                        .read();
        System.out.println(Json.pretty(openAPI));
        assert(openAPI.getComponents().getSchemas().get("BucketResponse").getProperties().containsKey("buckets"));
    }

    public static class DataResponse {
        private String value;
        private List<BucketResponse> buckets;
        public String getValue() {
            return value;
        }
        public List<BucketResponse> getBuckets() {
            return buckets;
        }
    }

    public static class BucketResponse {
        private String value;
        private List<BucketResponse> buckets;
        public String getValue() {
            return value;
        }
        public List<BucketResponse> getBuckets() {
            return buckets;
        }
    }

    @Path("/data")
    @Produces(MediaType.APPLICATION_JSON)
    public static class DataResource {
        @POST
        @Path("/")
        public DataResponse getData() {
            return new DataResponse();
        }
    }
}
ali-rantakari commented 4 years ago

One workaround for this issue is to apply a Schema annotation to one of the "collection of B" properties.

miroslavvojtus commented 3 years ago

We have exactly the same issue.

@ali-rantakari What did you mean by the workaround? How should the schema annotation looks like?

miroslavvojtus commented 3 years ago

I have added @ArraySchema(schema = @Schema(implementation= B.class)) into my B class. It generated the expected doc.