Generate code from OpenAPI spec including examples and descriptions

Viewed 700

Suppose I have an OpenAPI spec .yaml file with the following properties:

.... 
My_Model:
  description: 'Model to do some work'
  type: object
....

When I generate it to Kotlin code, using openapi-generator, it is adding the description into Javadoc part:

/**
 * Model to do some work
 */

data class MyModel(
....

What I want from generator is to add the description into swagger annotation also (or only):

....
@Schema(description = "Model to do some work")
data class MyModel(
....

Is there any setting or another generator library/software supporting this? The same case is true not only for description, but also for examples. Need to work for fields too.

1 Answers

Yes, you can use @Schema annotation in a class level and a property level:

@Schema(name = "MyDto", description = "MyDto Model")
class MyDto {
    @Schema(description = "Prop Description", example = "Prop Example")
    var prop: String? = null
    
    @Schema(description = "Text Description", example = "Text Example")
    var text: String? = null
   
    @Schema(description = "Something Description", example = "Something Example")
    var something: String? = null
}

Dependency for springdoc-openapi-kotlin:

Using Gradle:

implementation("org.springdoc:springdoc-openapi-kotlin:1.5.9")

Using Maven:

<dependency>
  <groupId>org.springdoc</groupId>
  <artifactId>springdoc-openapi-kotlin</artifactId>
  <version>1.5.9</version>
</dependency>

Reference:

How to document a Kotlin/Spring application with Springdoc and OpenAI:

Related