简体   繁体   中英

Generate code from OpenAPI spec including examples and descriptions

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.

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM