5

I'm using SpringDoc and trying to add a schema to OpenApi programmatically, but no success.

    @Bean
public OpenAPI customOpenAPI() {
     Schema mySchema = new Schema<Object>();
     mySchema
     .type("object")
     .$ref("#/components/schemas/MySchema")
     .name("MySchema")
     .addProperties("testStr", new StringSchema());

      return new OpenAPI()
            .servers(servers)
            .info(new Info().title(title).version(version).description(description))
            .components(new Components()
                    .addSchemas("MySchema" , mySchema)
            )
            .tags(tags);
}

The description of mySchema is not added to the list of schemas I see in the YAML file generated and if I try to ref to it:

apiResponses.entrySet().forEach(response -> response.getValue().addHeaderObject("XxX", 
                  new Header().$ref("#/components/schemas/MySchema")));

Following error is displayed in swagger UI:

Resolver error at paths./XX/v1/test/status/{entry}.get.responses.404.headers.XxX.$ref Could not resolve reference: Could not resolve pointer: /components/schemas/MySchema

Please, could you help me to understand?

Edit: I'm using version 1.3.9

RoNoWhe
  • 81
  • 1
  • 6

3 Answers3

6

It looks like your schema is being replaced by the schemas that are generated automatically. Try this:

@Bean
public OpenApiCustomiser openApiCustomiser() {
    return openApi -> {
        var mySchema = new ObjectSchema();
        mySchema.name("MySchema");

        var schemas = openApi.getComponents().getSchemas();
        schemas.put(mySchema.getName() , mySchema);
    };
}
  • Additionally, you need to use a `GlobalOpenApiCustomiser` if you have a `GroupedOpenApi` – sbstnzmr Jun 21 '22 at 13:42
  • I just faced the same issue and I resolved it by setting `springdoc.remove-broken-reference-definitions` to be true also Referenced: https://springdoc.org/#how-can-i-show-schema-definitions-even-the-schema-is-not-referenced – weikangchia Aug 01 '22 at 05:19
1

With GroupedOpenApi set via Components.addSchemas:

    @Bean
    GroupedOpenApi api() {
        return GroupedOpenApi.builder()
                .group("REST API")
                .addOpenApiCustomizer(openApi -> {
                    openApi.addSecurityItem(new SecurityRequirement().addList("Authorization"))
                            .components(new Components()
                                    .addSchemas("User", ModelConverters.getInstance().readAllAsResolvedSchema(User.class).schema)
                                    .addSchemas("UserTo", ModelConverters.getInstance().readAllAsResolvedSchema(UserTo.class).schema)
                                    .addSecuritySchemes("Authorization", new SecurityScheme()
                                            .in(SecurityScheme.In.HEADER)
                                            .type(SecurityScheme.Type.HTTP)
                                            .scheme("bearer")
                                            .name("JWT"))
                            )
                            .info(new Info().title("REST API").version("1.0").description(...));
                })
                .pathsToMatch("/api/**")
                .build();
    }
Grigory Kislin
  • 16,647
  • 10
  • 125
  • 197
  • Thanks for your answer. It’s a lot since I dont work on that project. I hope this will help others. Wondering if this was in the version I used. – RoNoWhe Dec 03 '22 at 07:19
  • Thanks! I usually write solutions for every stackoverflow readers and also for myself - use stackoverflow as a notepad:) – Grigory Kislin Dec 03 '22 at 13:44
0

Here is the explanation how to Set global Headers:

Sample code is here:

brianbro
  • 4,141
  • 2
  • 24
  • 37
  • Thanks. Anyway, I tried out this approach and it works. I was wondering what if I need to add a schema that is not for parameter/header use? – RoNoWhe May 11 '20 at 21:57
  • @R.S, where else do you intend to reuse the schemas? In the API responses? – Debargha Roy Aug 10 '20 at 05:30
  • 1
    @Debargha Roy, yes, for example. Another aspect I don’t like is that schema headers are not shown by swagger editor. – RoNoWhe Aug 12 '20 at 06:41