|
| 1 | +package com.papsign.ktor.openapigen |
| 2 | + |
| 3 | +import com.papsign.ktor.openapigen.annotations.Path |
| 4 | +import com.papsign.ktor.openapigen.annotations.parameters.QueryParam |
| 5 | +import com.papsign.ktor.openapigen.annotations.type.enum.StrictEnumParsing |
| 6 | +import com.papsign.ktor.openapigen.exceptions.OpenAPIBadContentException |
| 7 | +import com.papsign.ktor.openapigen.exceptions.OpenAPIRequiredFieldException |
| 8 | +import com.papsign.ktor.openapigen.route.apiRouting |
| 9 | +import com.papsign.ktor.openapigen.route.path.normal.get |
| 10 | +import com.papsign.ktor.openapigen.route.response.respond |
| 11 | +import io.ktor.application.* |
| 12 | +import io.ktor.features.* |
| 13 | +import io.ktor.http.* |
| 14 | +import io.ktor.response.* |
| 15 | +import io.ktor.server.testing.* |
| 16 | +import kotlin.test.* |
| 17 | + |
| 18 | +@StrictEnumParsing |
| 19 | +enum class StrictTestEnum { |
| 20 | + VALID, |
| 21 | + ALSO_VALID, |
| 22 | +} |
| 23 | + |
| 24 | +@Path("/") |
| 25 | +data class NullableStrictEnumParams(@QueryParam("") val type: StrictTestEnum? = null) |
| 26 | + |
| 27 | +@Path("/") |
| 28 | +data class NonNullableStrictEnumParams(@QueryParam("") val type: StrictTestEnum) |
| 29 | + |
| 30 | +class EnumStrictTestServer { |
| 31 | + |
| 32 | + companion object { |
| 33 | + // test server for nullable enums |
| 34 | + private fun Application.nullableEnum() { |
| 35 | + install(OpenAPIGen) |
| 36 | + install(StatusPages) { |
| 37 | + exception<OpenAPIBadContentException> { e -> |
| 38 | + call.respond(HttpStatusCode.BadRequest, e.localizedMessage) |
| 39 | + } |
| 40 | + } |
| 41 | + apiRouting { |
| 42 | + get<NullableStrictEnumParams, String> { params -> |
| 43 | + if (params.type != null) |
| 44 | + assertTrue { StrictTestEnum.values().contains(params.type) } |
| 45 | + respond(params.type?.toString() ?: "null") |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // test server for non-nullable enums |
| 51 | + private fun Application.nonNullableEnum() { |
| 52 | + install(OpenAPIGen) |
| 53 | + install(StatusPages) { |
| 54 | + exception<OpenAPIRequiredFieldException> { e -> |
| 55 | + call.respond(HttpStatusCode.BadRequest, e.localizedMessage) |
| 56 | + } |
| 57 | + exception<OpenAPIBadContentException> { e -> |
| 58 | + call.respond(HttpStatusCode.BadRequest, e.localizedMessage) |
| 59 | + } |
| 60 | + } |
| 61 | + apiRouting { |
| 62 | + get<NonNullableStrictEnumParams, String> { params -> |
| 63 | + assertTrue { StrictTestEnum.values().contains(params.type) } |
| 64 | + respond(params.type.toString()) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `nullable enum could be omitted and it will be null`() { |
| 72 | + withTestApplication({ nullableEnum() }) { |
| 73 | + handleRequest(HttpMethod.Get, "/").apply { |
| 74 | + assertEquals(HttpStatusCode.OK, response.status()) |
| 75 | + assertEquals("null", response.content) |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + fun `nullable enum should be parsed correctly`() { |
| 82 | + withTestApplication({ nullableEnum() }) { |
| 83 | + handleRequest(HttpMethod.Get, "/?type=VALID").apply { |
| 84 | + assertEquals(HttpStatusCode.OK, response.status()) |
| 85 | + assertEquals("VALID", response.content) |
| 86 | + } |
| 87 | + handleRequest(HttpMethod.Get, "/?type=ALSO_VALID").apply { |
| 88 | + assertEquals(HttpStatusCode.OK, response.status()) |
| 89 | + assertEquals("ALSO_VALID", response.content) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + fun `nullable enum parsing should be case-sensitive and should throw on passing wrong case`() { |
| 96 | + withTestApplication({ nullableEnum() }) { |
| 97 | + handleRequest(HttpMethod.Get, "/?type=valid").apply { |
| 98 | + assertEquals(HttpStatusCode.BadRequest, response.status()) |
| 99 | + assertEquals( |
| 100 | + "Invalid value [valid] for enum parameter of type StrictTestEnum. Expected: [VALID,ALSO_VALID]", |
| 101 | + response.content |
| 102 | + ) |
| 103 | + } |
| 104 | + handleRequest(HttpMethod.Get, "/?type=also_valid").apply { |
| 105 | + assertEquals(HttpStatusCode.BadRequest, response.status()) |
| 106 | + assertEquals( |
| 107 | + "Invalid value [also_valid] for enum parameter of type StrictTestEnum. Expected: [VALID,ALSO_VALID]", |
| 108 | + response.content |
| 109 | + ) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + fun `nullable enum parsing should not parse values outside of enum`() { |
| 116 | + withTestApplication({ nullableEnum() }) { |
| 117 | + handleRequest(HttpMethod.Get, "/?type=what").apply { |
| 118 | + assertEquals(HttpStatusCode.BadRequest, response.status()) |
| 119 | + assertEquals( |
| 120 | + "Invalid value [what] for enum parameter of type StrictTestEnum. Expected: [VALID,ALSO_VALID]", |
| 121 | + response.content |
| 122 | + ) |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + fun `non-nullable enum cannot be omitted`() { |
| 129 | + withTestApplication({ nonNullableEnum() }) { |
| 130 | + handleRequest(HttpMethod.Get, "/").apply { |
| 131 | + assertEquals(HttpStatusCode.BadRequest, response.status()) |
| 132 | + assertEquals("The field type is required", response.content) |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + fun `non-nullable enum should be parsed correctly`() { |
| 139 | + withTestApplication({ nonNullableEnum() }) { |
| 140 | + handleRequest(HttpMethod.Get, "/?type=VALID").apply { |
| 141 | + assertEquals(HttpStatusCode.OK, response.status()) |
| 142 | + assertEquals("VALID", response.content) |
| 143 | + } |
| 144 | + handleRequest(HttpMethod.Get, "/?type=ALSO_VALID").apply { |
| 145 | + assertEquals(HttpStatusCode.OK, response.status()) |
| 146 | + assertEquals("ALSO_VALID", response.content) |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + fun `non-nullable enum parsing should be case-sensitive and should throw on passing wrong case`() { |
| 153 | + withTestApplication({ nonNullableEnum() }) { |
| 154 | + handleRequest(HttpMethod.Get, "/?type=valid").apply { |
| 155 | + assertEquals(HttpStatusCode.BadRequest, response.status()) |
| 156 | + assertEquals( |
| 157 | + "Invalid value [valid] for enum parameter of type StrictTestEnum. Expected: [VALID,ALSO_VALID]", |
| 158 | + response.content |
| 159 | + ) |
| 160 | + } |
| 161 | + handleRequest(HttpMethod.Get, "/?type=also_valid").apply { |
| 162 | + assertEquals(HttpStatusCode.BadRequest, response.status()) |
| 163 | + assertEquals( |
| 164 | + "Invalid value [also_valid] for enum parameter of type StrictTestEnum. Expected: [VALID,ALSO_VALID]", |
| 165 | + response.content |
| 166 | + ) |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + fun `non-nullable enum parsing should not parse values outside of enum`() { |
| 173 | + withTestApplication({ nonNullableEnum() }) { |
| 174 | + handleRequest(HttpMethod.Get, "/?type=what").apply { |
| 175 | + assertEquals(HttpStatusCode.BadRequest, response.status()) |
| 176 | + assertEquals( |
| 177 | + "Invalid value [what] for enum parameter of type StrictTestEnum. Expected: [VALID,ALSO_VALID]", |
| 178 | + response.content |
| 179 | + ) |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments