Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deprecation check feature #40

Merged
merged 8 commits into from
Jun 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/src/main/kotlin/de/codecentric/hikaku/Hikaku.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class Hikaku(
Feature.MatrixParameters -> matches && it.matrixParameters == otherEndpoint.matrixParameters
Feature.Produces -> matches && it.produces == otherEndpoint.produces
Feature.Consumes -> matches && it.consumes == otherEndpoint.consumes
Feature.Deprecation -> matches && it.deprecated == otherEndpoint.deprecated
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class SupportedFeatures(
/** Checks supported media type of requests. */
Consumes,
/** Checks the equality of matrix parameters. */
MatrixParameters
MatrixParameters,
/** Checks the equality of deprecation. */
Deprecation
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ data class Endpoint(
val headerParameters: Set<HeaderParameter> = emptySet(),
val matrixParameters: Set<MatrixParameter> = emptySet(),
val produces: Set<String> = emptySet(),
val consumes: Set<String> = emptySet()
val consumes: Set<String> = emptySet(),
val deprecated: Boolean = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class CommandLineReporter : Reporter {
Feature.MatrixParameters -> listMatrixParameter(endpoint.matrixParameters)
Feature.Consumes -> listRequestMediaTypes(endpoint.consumes)
Feature.Produces -> listResponseMediaTypes(endpoint.produces)
Feature.Deprecation -> if (endpoint.deprecated) " Deprecated" else ""
}
}

Expand Down
3 changes: 2 additions & 1 deletion docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ There might be various ways to declare or use a feature, so check each converter
| 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)_ | ✅ _(2.3.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)_ | ❌ |
| Produces | Checks the supported media types of the response. | ✅ _(1.1.0)_ | ✅ _(1.1.0)_ | ✅ _(1.1.0)_ | ✅ _(2.0.0)_ | ✅ _(2.1.0)_ | ✅ _(2.3.0)_ |
| Consumes | Checks the supported media types of the request. | ✅ _(1.1.0)_ | ✅ _(1.1.0)_ | ✅ _(1.1.0)_ | ✅ _(2.0.0)_ | ✅ _(2.1.0)_ | ✅ _(2.3.0)_ |
| Consumes | Checks the supported media types of the request. | ✅ _(1.1.0)_ | ✅ _(1.1.0)_ | ✅ _(1.1.0)_ | ✅ _(2.0.0)_ | ✅ _(2.1.0)_ | ✅ _(2.3.0)_ |
| Deprecation | Checks deprecated endpoints are properly marked. | ✅ _(2.3.0)_ | ✅ _(2.3.0)_ | ❌ | ✅ _(2.3.0)_ | ✅ _(2.3.0)_ | ✅ _(2.3.0)_ |
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class JaxRsConverter(private val packageName: String) : AbstractEndpointConverte
Feature.HeaderParameters,
Feature.MatrixParameters,
Feature.Consumes,
Feature.Produces
Feature.Produces,
Feature.Deprecation
)

override fun convert(): Set<Endpoint> {
Expand Down Expand Up @@ -59,7 +60,8 @@ class JaxRsConverter(private val packageName: String) : AbstractEndpointConverte
headerParameters = extractHeaderParameters(method),
matrixParameters = extractMatrixParameters(method),
produces = extractProduces(resource, method),
consumes = extractConsumes(resource, method)
consumes = extractConsumes(resource, method),
deprecated = isEndpointDeprecated(method)
)

private fun extractPath(resource: Class<*>, method: Method): String {
Expand Down Expand Up @@ -173,4 +175,8 @@ class JaxRsConverter(private val packageName: String) : AbstractEndpointConverte
.map { MatrixParameter(it) }
.toSet()
}

private fun isEndpointDeprecated(method: Method) =
method.isAnnotationPresent(Deprecated::class.java)
|| method.declaringClass.isAnnotationPresent(Deprecated::class.java)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package de.codecentric.hikaku.converters.jaxrs

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

class JaxRsConverterDeprecationTest {

@Test
fun `no deprecation`() {
// given
val specification = setOf(
Endpoint(
path = "/todos",
httpMethod = GET,
deprecated = false
)
)

//when
val result = JaxRsConverter("test.jaxrs.deprecation.none").conversionResult

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

@Test
fun `deprecated class`() {
// given
val specification = setOf(
Endpoint(
path = "/todos",
httpMethod = GET,
deprecated = true
)
)

//when
val result = JaxRsConverter("test.jaxrs.deprecation.onclass").conversionResult

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

@Test
fun `deprecated function`() {
// given
val specification = setOf(
Endpoint(
path = "/todos",
httpMethod = GET,
deprecated = true
)
)

//when
val result = JaxRsConverter("test.jaxrs.deprecation.onfunction").conversionResult

//then
assertThat(result).containsExactlyInAnyOrderElementsOf(specification)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package test.jaxrs.deprecation.none

import javax.ws.rs.GET
import javax.ws.rs.Path

@Path("/todos")
class NoDeprecation {

@GET
fun todo() { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package test.jaxrs.deprecation.onclass

import javax.ws.rs.GET
import javax.ws.rs.Path

@Path("/todos")
@Deprecated("Test")
class DeprecationOnClass {

@GET
fun todo() { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package test.jaxrs.deprecation.onfunction

import javax.ws.rs.GET
import javax.ws.rs.Path

@Path("/todos")
class DeprecationOnFunction {

@GET
@Deprecated("Test")
fun todo() { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class MicronautConverter(private val packageName: String) : AbstractEndpointConv
Feature.PathParameters,
Feature.HeaderParameters,
Feature.Produces,
Feature.Consumes
Feature.Consumes,
Feature.Deprecation
)

override fun convert(): Set<Endpoint> {
Expand Down Expand Up @@ -61,7 +62,8 @@ class MicronautConverter(private val packageName: String) : AbstractEndpointConv
pathParameters = extractPathParameters(path, method),
headerParameters = extractHeaderParameters(method),
consumes = extractConsumes(resource, method),
produces = extractProduces(resource, method)
produces = extractProduces(resource, method),
deprecated = isEndpointDeprecated(method)
)
}

Expand Down Expand Up @@ -248,4 +250,8 @@ class MicronautConverter(private val packageName: String) : AbstractEndpointConv
.map { HeaderParameter(it.value, it.defaultValue.isBlank()) }
.toSet()
}

private fun isEndpointDeprecated(method: Method) =
method.isAnnotationPresent(Deprecated::class.java)
|| method.declaringClass.isAnnotationPresent(Deprecated::class.java)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package de.codecentric.hikaku.converters.micronaut

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

class MicronautConverterDeprecationTest {

@Test
fun `no deprecation`() {
// given
val specification = setOf(
Endpoint(
path = "/todos",
httpMethod = GET,
deprecated = false
)
)

//when
val result = MicronautConverter("test.micronaut.deprecation.none").conversionResult

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

@Test
fun `deprecated class`() {
// given
val specification = setOf(
Endpoint(
path = "/todos",
httpMethod = GET,
deprecated = true
)
)

//when
val result = MicronautConverter("test.micronaut.deprecation.onclass").conversionResult

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

@Test
fun `deprecated function`() {
// given
val specification = setOf(
Endpoint(
path = "/todos",
httpMethod = GET,
deprecated = true
)
)

//when
val result = MicronautConverter("test.micronaut.deprecation.onfunction").conversionResult

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

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

@Controller("/todos")
class NoDeprecation {

@Get
fun todo() { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package test.micronaut.deprecation.onclass

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

@Controller("/todos")
@Deprecated("Test")
class DeprecationOnClass {

@Get
fun todo() { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package test.micronaut.deprecation.onfunction

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

@Controller("/todos")
class DeprecationOnFunction {

@Get
@Deprecated("Test")
fun todo() { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class OpenApiConverter private constructor(private val specificationContent: Str
Feature.PathParameters,
Feature.HeaderParameters,
Feature.Produces,
Feature.Consumes
Feature.Consumes,
Feature.Deprecation
)

override fun convert(): Set<Endpoint> {
Expand Down Expand Up @@ -66,7 +67,8 @@ class OpenApiConverter private constructor(private val specificationContent: Str
pathParameters = extractPathParameters(operation),
headerParameters = extractHeaderParameters(operation),
consumes = extractConsumesMediaTypes(operation),
produces = extractProduceMediaTypes(operation)
produces = extractProduceMediaTypes(operation),
deprecated = operation?.deprecated ?: false
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package de.codecentric.hikaku.converters.openapi

import de.codecentric.hikaku.endpoints.Endpoint
import de.codecentric.hikaku.endpoints.HttpMethod.GET
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import java.nio.file.Paths

class OpenApiConverterDeprecationTest {

@Test
fun `no deprecation`() {
//given
val file = Paths.get(this::class.java.classLoader.getResource("deprecation/deprecation_none.yaml").toURI())
val implementation = setOf(
Endpoint(
path = "/todos",
httpMethod = GET,
produces = setOf("application/json"),
deprecated = false
)
)

//when
val specification = OpenApiConverter(file)

//then
assertThat(specification.conversionResult).containsExactlyInAnyOrderElementsOf(implementation)
}

@Test
fun `deprecated operation`() {
//given
val file = Paths.get(this::class.java.classLoader.getResource("deprecation/deprecation_operation.yaml").toURI())
val implementation = setOf(
Endpoint(
path = "/todos",
httpMethod = GET,
produces = setOf("application/json"),
deprecated = true
)
)

//when
val specification = OpenApiConverter(file)

//then
assertThat(specification.conversionResult).containsExactlyInAnyOrderElementsOf(implementation)
}
}
Loading