Search code examples
spring-bootswaggerswagger-2.0springdoc

Include extra enum in openapi file generated by springdoc


I'm working on a Spring Boot v3 + springdoc 2.1.0 server. I'm defining some models that are not returned via a Spring controller, but the schema needs to be kept in sync with clients. In order to achieve that, I'm adding them to the OpenApi bean:

  @Bean
  public OpenAPI customOpenAPI() {
    Components components = new Components();
    getModelsNotUsedInControllersToGenerate()
        .forEach(
            model ->
                components.addSchemas(
                    model.getSimpleName(),
                    ModelConverters.getInstance().readAllAsResolvedSchema(model).schema));
    return new OpenAPI()
        .components(components)
        .info(new Info().title("API"));
  }

This worked great for the Records that I added using Components.addSchemas. However, when I add an enum, the openapi.json creates a schema that just references itself:

      "DocumentType": {
        "$ref": "#/components/schemas/DocumentType"
      },

Does anyone know how to properly add extra enums to OpenApi?


Solution

  • You need add referencedSchemas too.

    For example:

    ResolvedSchema resolvedSchema = ModelConverters.getInstance().readAllAsResolvedSchema(model);
    components.addSchemas(model.getSimpleName(), resolvedSchema.schema);
    resolvedSchema.referencedSchemas.forEach(components::addSchemas);