This library is an extension for Springdoc OpenAPI which allows for defining OpenAPI 3.0 schema using code.
However, while it was developed with Springdoc OpenAPI in mind, you can apply this solution to any Spring Boot OpenAPI library based on Spring Core.
- Add a dependency on
springdoc-openapi-programmatic-documentation:
<dependency>
<groupId>com.danielfrak.code</groupId>
<artifactId>springdoc-openapi-programmatic-documentation</artifactId>
<version>1.0.1</version>
</dependency>- Inject a
ModelDocumentationinstance in a@configurationclass and use it to add a new schema:
import io.swagger.v3.oas.models.media.Schema;
import org.springframework.context.annotation.Configuration;
import com.danielfrak.code.config.openapi.ModelDocumentation;
import com.danielfrak.code.config.openapi.ModelDocumentation.Model;
import com.danielfrak.code.model.MyValueObject;
@Configuration
public class OpenApiConfig {
public OpenApiConfig(ModelDocumentation modelDocumentation) {
modelDocumentation
.add(new DocumentedModel()
.source(MyValueObject.class)
.implementation(String.class)
.schema(new Schema<>()
.example("Example value")
.description("Base description")));
}
}source- the class to documentimplementation- equivalent to theimplementationon@io.swagger.v3.oas.annotations.media.Schemaschema- anio.swagger.v3.oas.models.media.Schemainstance to serve as the final schema for the source class
