Migration Swagger to OpenApi need substituteWith

106 views Asked by At

Hello I migrating my application from SwaggerFox to OpenApi. I have a lots of Value Objects like:

data class UserId(@JsonValue val value: UUID) 

In Swagger configuration i had a configuration:

override fun Docket.additionalConfiguration(): Docket =
        directModelSubstitute(type.java, String::class.java)(UserId::class, UUID::class)

and in swagger-ui I had: "userId": "string"

now when i migrated to openapi this same model looks different:

"userId": {
    "value": "string"
  }

Is there any option to configure value objects in open api?

1

There are 1 answers

0
Karthik S On

In OpenAPI, you can use the @Schema annotation to customize the representation of your models, including value objects. You can use this annotation to specify the format, example, and other properties of your model. For your specific case, you can customize the UserId class using the @Schema annotation to achieve the desired representation. For example you can use the @Schema annotation on your UserId class as follows:

import com.fasterxml.jackson.annotation.JsonValue
import io.swagger.v3.oas.annotations.media.Schema
import java.util.UUID

data class UserId(@JsonValue val value: UUID)

And in your OpenAPI configuration, you can use the @Schema annotation to configure the representation of the UserId class:

import io.swagger.v3.oas.annotations.media.Schema

@Schema(substitutes = UserId::class)
data class UserIdSchema(val value: String)


Here, @Schema(substitutes = UserId::class) indicates that UserIdSchema should be used as a substitute for UserId in the OpenAPI documentation. The UserIdSchema class has a single property value of type String, which is what you want in the OpenAPI documentation.

Note: Make sure to replace UserIdSchema with the appropriate class name that represents the OpenAPI documentation for the UserId class.