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
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);
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With