Issue
I am using springdoc
with spring-boot
configured mostly with annotations.
I would like to expose a particular class schema which is not referenced by any service. Is it possible to do this?
In pseudocode, I am effectively trying to do this:
GroupedOpenAPI.parseAndAddClass(Class<?> clazz);
or
GroupedOpenAPI.scan("my.models.package");
=== Update ===
I managed to parse the Schema using ModelConverters.getInstance().readAll(MyClass.class);
and then tried to add it as an OpenApiCustomiser
, however it is still not visible in the UI.
Solution
In the SpringDoc, you can add a non-related class to the generated specification using OpenApiCustomiser
@Bean
public OpenApiCustomiser schemaCustomiser() {
ResolvedSchema resolvedSchema = ModelConverters.getInstance()
.resolveAsResolvedSchema(new AnnotatedType(MyClass.class));
return openApi -> openApi
.schema(resolvedSchema.schema.getName(), resolvedSchema.schema);
}
But need to be careful, because if your additional model is not referenced in any API, by default the SpringDoc automatically removes every broken reference definition.
To disable this default behavior, you need to use the following configuration property:
springdoc.remove-broken-reference-definitions=false
Answered By - VadymVL
Answer Checked By - Terry (JavaFixing Volunteer)