2

I need to expose some models which don't used directly in REST API methods.

With springfox I used Docket's additionalModels method to programmatically add models to specification:

docket.additionalModels(
  typeResolver.resolve(XModel1.class),
  typeResolver.resolve(XModel2.class)
)

How to do it with springdoc?

I've created a dummy operation with dummy-parameter which includes all required models. But I feel the approach has space for improvement.

Debargha Roy
  • 2,320
  • 1
  • 15
  • 34
aldan95
  • 21
  • 1
  • 4

2 Answers2

4

With OpenApiCustomiser , you have access to the OpenAPI Object. You can add any object/operation you want without having to add annotations on your code.

You can have a look at the documentation for more details:

Kevin Pullin
  • 13,122
  • 3
  • 24
  • 33
brianbro
  • 4,141
  • 2
  • 24
  • 37
  • 2
    Thanks. I've created customizer, with `openApi.components.schemas.putAll(ModelConverters.getInstance().read(Foo::class.java))`, but no Foo model in generated spec found. I think it is swagger issue, not springdoc – aldan95 Feb 26 '20 at 06:37
0

In Kotlin

fun components(): Components {
    val components = Components()

    val converter = ModelConverters.getInstance()

    val schema1 = converter.readAllAsResolvedSchema(XModel1::class.java)
    val schema2 = converter.readAllAsResolvedSchema(XModel2::class.java)

    schema1.referencedSchemas.forEach { s -> components.addSchemas(s.key, s.value) }
    schema2.referencedSchemas.forEach { s -> components.addSchemas(s.key, s.value) }

    return components
}

Additionally you may need to specify the property in application.yml:

springdoc:
    remove-broken-reference-definitions: false
stalen
  • 1
  • 1