Ktor-OpenAPI-Generator icon indicating copy to clipboard operation
Ktor-OpenAPI-Generator copied to clipboard

Overriding a primitive converter (UUID)

Open mplokita-figure opened this issue 3 years ago • 5 comments

I'm hoping to override the default behavior of zeroing an invalid UUID string. (For example, I might prefer to throw an exception or return null.)

Does this constitute a new feature request?

primitive {
    try {
        UUID.fromString(it)
    } catch (e: IllegalArgumentException) {
        null
    } ?: UUID(0, 0)
}

mplokita-figure avatar Nov 26 '21 18:11 mplokita-figure

you can use a nullable uuid parameter, it uses 0 as a default if you don't allow it to be null.

Wicpar avatar Nov 30 '21 18:11 Wicpar

@Wicpar Won't that affect the API spec? If we would like to communicate to readers of the spec that the UUID is not optional, then is there an alternative solution?

mplokita-figure avatar Dec 01 '21 23:12 mplokita-figure

You are right, there is no way to throw an error doing that, the correct implementation would be to remove the try-catch and have an exception handler catch and build a proper error response

Wicpar avatar Dec 02 '21 09:12 Wicpar

@Wicpar As far as overriding default converter behavior, I'm assuming this is still a viable solution? Any plans to incorporate this?

https://github.com/papsign/Ktor-OpenAPI-Generator/issues/82#issuecomment-733191825

aphu-figure avatar Feb 09 '22 15:02 aphu-figure

I'll accept the PR if you make one :)

Wicpar avatar Feb 27 '22 22:02 Wicpar