11import com.fasterxml.jackson.annotation.JsonInclude
2+ import com.fasterxml.jackson.annotation.JsonSubTypes
3+ import com.fasterxml.jackson.annotation.JsonTypeInfo
4+ import com.fasterxml.jackson.annotation.JsonTypeName
25import com.fasterxml.jackson.core.util.DefaultIndenter
36import com.fasterxml.jackson.core.util.DefaultPrettyPrinter
47import com.fasterxml.jackson.databind.DeserializationFeature
@@ -11,31 +14,23 @@ import com.papsign.ktor.openapigen.annotations.Path
1114import com.papsign.ktor.openapigen.annotations.Request
1215import com.papsign.ktor.openapigen.annotations.Response
1316import com.papsign.ktor.openapigen.annotations.parameters.PathParam
14- import com.papsign.ktor.openapigen.interop.OAuth2Handler
15- import com.papsign.ktor.openapigen.interop.configure
1617import com.papsign.ktor.openapigen.interop.withAPI
1718import com.papsign.ktor.openapigen.openAPIGen
1819import com.papsign.ktor.openapigen.openapi.Described
1920import com.papsign.ktor.openapigen.openapi.Server
2021import com.papsign.ktor.openapigen.route.apiRouting
2122import com.papsign.ktor.openapigen.route.info
22- import com.papsign.ktor.openapigen.route.path.auth.auth
23- import com.papsign.ktor.openapigen.route.path.auth.get
24- import com.papsign.ktor.openapigen.route.path.auth.post
25- import com.papsign.ktor.openapigen.route.path.auth.principal
2623import com.papsign.ktor.openapigen.route.path.normal.get
24+ import com.papsign.ktor.openapigen.route.path.normal.post
2725import com.papsign.ktor.openapigen.route.response.respond
2826import com.papsign.ktor.openapigen.route.route
2927import com.papsign.ktor.openapigen.route.tag
3028import io.ktor.application.application
3129import io.ktor.application.call
3230import io.ktor.application.install
33- import io.ktor.auth.Authentication
34- import io.ktor.auth.OAuthServerSettings
3531import io.ktor.features.ContentNegotiation
3632import io.ktor.features.StatusPages
3733import io.ktor.features.origin
38- import io.ktor.http.HttpMethod
3934import io.ktor.http.HttpStatusCode
4035import io.ktor.jackson.jackson
4136import io.ktor.request.host
@@ -53,8 +48,6 @@ object TestServer {
5348
5449 class ProperException (msg : String , val id : String = " proper.exception" ) : Exception(msg)
5550
56- lateinit var oauth: OAuth2Handler <APIPrincipal , Scopes >
57-
5851 @JvmStatic
5952 fun main (args : Array <String >) {
6053 embeddedServer(Netty , 8080 , " localhost" ) {
@@ -82,9 +75,9 @@ object TestServer {
8275 install(ContentNegotiation ) {
8376 jackson {
8477 enable(
85- DeserializationFeature .WRAP_EXCEPTIONS ,
86- DeserializationFeature .USE_BIG_INTEGER_FOR_INTS ,
87- DeserializationFeature .USE_BIG_DECIMAL_FOR_FLOATS
78+ DeserializationFeature .WRAP_EXCEPTIONS ,
79+ DeserializationFeature .USE_BIG_INTEGER_FOR_INTS ,
80+ DeserializationFeature .USE_BIG_DECIMAL_FOR_FLOATS
8881 )
8982
9083 enable(SerializationFeature .WRAP_EXCEPTIONS , SerializationFeature .INDENT_OUTPUT )
@@ -117,35 +110,15 @@ object TestServer {
117110
118111 val scopes = Scopes .values().asList()
119112
120- val googleOAuthProvider = OAuthServerSettings .OAuth2ServerSettings (
121- name = " google" ,
122- authorizeUrl = " https://accounts.google.com/o/oauth2/auth" ,
123- accessTokenUrl = " https://www.googleapis.com/oauth2/v3/token" ,
124- requestMethod = HttpMethod .Post ,
125-
126- clientId = " <id>" ,
127- clientSecret = " <secret>" ,
128- defaultScopes = scopes.map { it.name }
129- )
130-
131- oauth = OAuth2Handler (googleOAuthProvider, scopes, scopes, scopes, scopes) { auth ->
132- APIPrincipal (auth.tokenType, auth.accessToken)
133- }
134-
135- // auth interop
136- install(Authentication ) {
137- configure(oauth)
138- }
139-
140113 // serve OpenAPI and redirect from root
141114 routing {
142115 get(" /openapi.json" ) {
143116 val host = Server (
144- call.request.origin.scheme + " ://" + call.request.host() + if (setOf (
145- 80 ,
146- 443
147- ).contains(call.request.port())
148- ) " " else " :${call.request.port()} "
117+ call.request.origin.scheme + " ://" + call.request.host() + if (setOf (
118+ 80 ,
119+ 443
120+ ).contains(call.request.port())
121+ ) " " else " :${call.request.port()} "
149122 )
150123 application.openAPIGen.api.servers.add(0 , host)
151124 call.respond(application.openAPIGen.api)
@@ -160,15 +133,25 @@ object TestServer {
160133 apiRouting {
161134
162135 get<StringParam , StringResponse >(
163- info(" String Param Endpoint" , " This is a String Param Endpoint" ),
164- example = StringResponse (" Hi" )
136+ info(" String Param Endpoint" , " This is a String Param Endpoint" ),
137+ example = StringResponse (" Hi" )
165138 ) { params ->
166139 respond(StringResponse (params.a))
167140 }
168141
142+ route(" sealed" ) {
143+ post<Unit , Base , Base >(
144+ info(" Sealed class Endpoint" , " This is a Sealed class Endpoint" ),
145+ exampleRequest = Base .A (" Hi" ),
146+ exampleResponse = Base .A (" Hi" )
147+ ) { params, base ->
148+ respond(base)
149+ }
150+ }
151+
169152 route(" long" ).get<LongParam , LongResponse >(
170- info(" Long Param Endpoint" , " This is a String Param Endpoint" ),
171- example = LongResponse (Long .MAX_VALUE )
153+ info(" Long Param Endpoint" , " This is a String Param Endpoint" ),
154+ example = LongResponse (Long .MAX_VALUE )
172155 ) { params ->
173156 respond(LongResponse (params.a))
174157 }
@@ -177,45 +160,21 @@ object TestServer {
177160 tag(TestServer .Tags .EXAMPLE ) {
178161
179162 get<StringParam , StringResponse >(
180- info(" String Param Endpoint" , " This is a String Param Endpoint" ),
181- example = StringResponse (" Hi" )
163+ info(" String Param Endpoint" , " This is a String Param Endpoint" ),
164+ example = StringResponse (" Hi" )
182165 ) { params ->
183166 respond(StringResponse (params.a))
184167 }
185168
186169 route(" long" ).get<LongParam , LongResponse >(
187- info(" Long Param Endpoint" , " This is a String Param Endpoint" ),
188- example = LongResponse (Long .MAX_VALUE )
170+ info(" Long Param Endpoint" , " This is a String Param Endpoint" ),
171+ example = LongResponse (Long .MAX_VALUE )
189172 ) { params ->
190173 respond(LongResponse (params.a))
191174 }
192175 }
193176
194- }
195-
196- tag(TestServer .Tags .EXAMPLE ) {
197- route(" authenticated" ) {
198-
199- auth(oauth) {
200-
201- get<StringParam , StringResponse , APIPrincipal >(
202- info(
203- " Authenticated String Param Endpoint" ,
204- " This is aa authenticated String Param Endpoint"
205- ),
206- example = StringResponse (" Hi" )
207- ) { params ->
208- val p = principal()
209- respond(StringResponse (params.a + p.a + p.b))
210- }
211-
212177
213- post<Unit , StringUsable , StringUsable , APIPrincipal > { _, str ->
214- respond(str)
215- }
216-
217- }
218- }
219178 }
220179 }
221180 }.start(true )
@@ -238,12 +197,24 @@ object TestServer {
238197 @Response(" A Long Response" )
239198 data class LongResponse (val str : Long )
240199
200+ @JsonTypeInfo(use = JsonTypeInfo .Id .NAME )
201+ @JsonSubTypes(
202+ JsonSubTypes .Type (Base .A ::class , name = " a" ),
203+ JsonSubTypes .Type (Base .B ::class , name = " b" ),
204+ JsonSubTypes .Type (Base .C ::class , name = " c" )
205+ )
206+ sealed class Base {
207+ class A (val str : String ) : Base()
208+ class B (val i : Int ) : Base()
209+ class C (val l : Long ) : Base()
210+ }
211+
241212
242- enum class Tags (override val description : String ): APITag {
213+ enum class Tags (override val description : String ) : APITag {
243214 EXAMPLE (" Wow this is a tag?!" )
244215 }
245216
246- enum class Scopes (override val description : String ): Described {
217+ enum class Scopes (override val description : String ) : Described {
247218 profile(" Basic Profile scope" ), email(" Email scope" )
248219 }
249220
0 commit comments