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.37k stars 2.17k forks source link

How can I add additional models (models that are not in rest) to OpenAPI.json documentation? #2858

Open duckladydinh opened 6 years ago

duckladydinh commented 6 years ago

Dear all,

I want to use Swagger to create a documentation and I want to put all available classes into it. I am using ResourceConfig and Spring Boot. How can I do that? I am looking for a way similar to that of SpringFox docket.additionalModels.

Many thanks

daniel-frak commented 4 years ago

I would also like a documented solution for this issue, even though over a year has passed since the creation of this issue.

For now I have managed to achieve this by setting components in the OpenAPI instance to a new Components() object with schemas retrieved via AnnotationsUtils.resolveSchemaFromType:

public OpenAPI openApi() {
        final Components components = new Components();
        AnnotationsUtils.resolveSchemaFromType(MyAdditionalModel.class, components, null);

        return new OpenAPI()
                .components(getAdditionalComponents())
                .info(new Info()
                        .title("Test API")
                        .version("v0.0.1"));
    }

I would appreciate input on whether or not this is a good solution and if there is any actual documentation for the OpenAPI / AnnotationUtils classes (as I couldn't find any).

frantuma commented 4 years ago

Atm there isn't a mechanism in configuration to e.g. add qualified class names or packages to be added to "schema resolution" in e.g. a JAX-RS context (which considers the schemas used in JAX-RS resources), however this can be achieved programmatically by invoking e.g. ModelConverters.getInstance().readAll(MyAdditionalModel.class); on each class needed, which returns a map of resolved schemas; these can be added for example to OpenAPI components section e.g. via openApi.getComponents().getSchemas().putAll(..)

Possibly filters are a good way to add such functionality