Skip to content

Commit

Permalink
Added support for query parameter to MicronautConverter (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
cc-jhr committed Apr 23, 2019
1 parent c7372d7 commit 4b9c96c
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ There might be various ways to declare or use a feature, so check each converter

| Feature Name | Description | [OpenApi Converter](../openapi/README.md)| [Spring Converter](../spring/README.md) | [WADL Converter](../wadl/README.md) | [RAML Converter](../raml/README.md) | [JAX-RS Converter](../jax-rs/README.md) | [Micronaut Converter](../micronaut/README.md) |
| --- | --- | --- | --- | --- | --- | --- | --- |
| QueryParameters | Name of a query parameter and whether the parameter is required or not. _Example:_ `/todos?filter=all`|_(1.0.0)_ |_(1.0.0)_ |_(1.1.0)_ |_(2.0.0)_ |_(2.1.0)_ | |
| QueryParameters | Name of a query parameter and whether the parameter is required or not. _Example:_ `/todos?filter=all`|_(1.0.0)_ |_(1.0.0)_ |_(1.1.0)_ |_(2.0.0)_ |_(2.1.0)_ | _(2.2.0)_ |
| PathParameters | Name of a path parameter. _Example:_ `/todos/{id}`|_(1.0.0)_ |_(1.0.0)_ |_(1.1.0)_ |_(2.0.0)_ |_(2.1.0)_ ||
| HeaderParameters | Name of a header parameter and whether the parameter is required or not. |_(1.1.0)_ |_(1.1.0)_ |_(1.1.0)_ |_(2.0.0)_ |_(2.1.0)_ ||
| MatrixParameters | Name of a matrix parameter and whether the parameter is required or not. _Example:_ `/todos;param=value` ||_(2.1.0)_ |_(2.1.0)_ ||_(2.1.0)_ ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import de.codecentric.hikaku.converters.ClassLocator
import de.codecentric.hikaku.converters.EndpointConverterException
import de.codecentric.hikaku.endpoints.Endpoint
import de.codecentric.hikaku.endpoints.HttpMethod
import de.codecentric.hikaku.endpoints.QueryParameter
import io.micronaut.http.annotation.*
import java.lang.reflect.Method

Expand Down Expand Up @@ -45,7 +46,8 @@ class MicronautConverter(private val packageName: String) : AbstractEndpointConv

private fun createEndpoint(resource: Class<*>, method: Method) = Endpoint(
path = extractPath(resource, method),
httpMethod = extractHttpMethod(method)
httpMethod = extractHttpMethod(method),
queryParameters = extractQueryParameters(method)
)

private fun extractPath(resource: Class<*>, method: Method): String {
Expand Down Expand Up @@ -86,4 +88,13 @@ class MicronautConverter(private val packageName: String) : AbstractEndpointConv
else -> throw IllegalStateException("Unable to determine http method. Valid annotation not found.")
}
}

private fun extractQueryParameters(method: Method): Set<QueryParameter> {
return method.parameters
.filter { it.isAnnotationPresent(QueryValue::class.java) }
.map { it.getAnnotation(QueryValue::class.java) }
.map { (it as QueryValue) }
.map { QueryParameter(it.value, it.defaultValue.isBlank()) }
.toSet()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package de.codecentric.hikaku.converters.micronaut

import de.codecentric.hikaku.endpoints.Endpoint
import de.codecentric.hikaku.endpoints.HttpMethod.GET
import de.codecentric.hikaku.endpoints.QueryParameter
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

class MicronautConverterQueryParameterTest {

@Test
fun `query parameter required`() {
//given
val specification = setOf(
Endpoint(
path = "/todos",
httpMethod = GET,
queryParameters = setOf(
QueryParameter("filter", true)
)
)
)

//when
val result = MicronautConverter("test.micronaut.queryparameter.required").conversionResult

//then
assertThat(result).containsExactlyInAnyOrderElementsOf(specification)
}

@Test
fun `query parameter optional, because a default value exists`() {
//given
val specification = setOf(
Endpoint(
path = "/todos",
httpMethod = GET,
queryParameters = setOf(
QueryParameter("filter", false)
)
)
)

//when
val result = MicronautConverter("test.micronaut.queryparameter.optional").conversionResult

//then
assertThat(result).containsExactlyInAnyOrderElementsOf(specification)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package test.micronaut.queryparameter.optional

import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.QueryValue

@Controller("/todos")
class QueryParameterTestController {

@Get
fun todos(@QueryValue("filter", defaultValue = "all") filter: String) { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package test.micronaut.queryparameter.required

import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.QueryValue

@Controller("/todos")
class QueryParameterTestController {

@Get
fun todos(@QueryValue("filter") filter: String) { }
}

0 comments on commit 4b9c96c

Please sign in to comment.