From 0732493e5cd51a6d52f5852dddc61e33f3befeae Mon Sep 17 00:00:00 2001 From: Alex Sandau Date: Mon, 13 Aug 2018 14:10:25 +0200 Subject: [PATCH 1/2] Add HEAD to HTTP Method --- .../openapi/gradle/OpenApi20Generator.kt | 2 + .../restdocs/openapi/gradle/ResourceModel.kt | 4 +- .../openapi/gradle/OpenApi20GeneratorTest.kt | 94 +++++++++++++++++++ 3 files changed, 99 insertions(+), 1 deletion(-) diff --git a/restdocs-openapi-gradle-plugin/src/main/kotlin/com/epages/restdocs/openapi/gradle/OpenApi20Generator.kt b/restdocs-openapi-gradle-plugin/src/main/kotlin/com/epages/restdocs/openapi/gradle/OpenApi20Generator.kt index 28a803ec..c337ca16 100644 --- a/restdocs-openapi-gradle-plugin/src/main/kotlin/com/epages/restdocs/openapi/gradle/OpenApi20Generator.kt +++ b/restdocs-openapi-gradle-plugin/src/main/kotlin/com/epages/restdocs/openapi/gradle/OpenApi20Generator.kt @@ -141,6 +141,8 @@ internal object OpenApi20Generator { HTTPMethod.PUT -> path.put(resourceModels2Operation(it.value)) HTTPMethod.DELETE -> path.delete(resourceModels2Operation(it.value)) HTTPMethod.PATCH -> path.patch(resourceModels2Operation(it.value)) + HTTPMethod.HEAD -> path.head(resourceModels2Operation(it.value)) + HTTPMethod.OPTIONS -> path.options(resourceModels2Operation(it.value)) } } diff --git a/restdocs-openapi-gradle-plugin/src/main/kotlin/com/epages/restdocs/openapi/gradle/ResourceModel.kt b/restdocs-openapi-gradle-plugin/src/main/kotlin/com/epages/restdocs/openapi/gradle/ResourceModel.kt index aad12188..5f1cf9f6 100644 --- a/restdocs-openapi-gradle-plugin/src/main/kotlin/com/epages/restdocs/openapi/gradle/ResourceModel.kt +++ b/restdocs-openapi-gradle-plugin/src/main/kotlin/com/epages/restdocs/openapi/gradle/ResourceModel.kt @@ -95,5 +95,7 @@ internal enum class HTTPMethod { POST, PUT, DELETE, - PATCH + PATCH, + HEAD, + OPTIONS } diff --git a/restdocs-openapi-gradle-plugin/src/test/kotlin/com/epages/restdocs/openapi/gradle/OpenApi20GeneratorTest.kt b/restdocs-openapi-gradle-plugin/src/test/kotlin/com/epages/restdocs/openapi/gradle/OpenApi20GeneratorTest.kt index 81b817d6..7c664108 100644 --- a/restdocs-openapi-gradle-plugin/src/test/kotlin/com/epages/restdocs/openapi/gradle/OpenApi20GeneratorTest.kt +++ b/restdocs-openapi-gradle-plugin/src/test/kotlin/com/epages/restdocs/openapi/gradle/OpenApi20GeneratorTest.kt @@ -75,6 +75,34 @@ class OpenApi20GeneratorTest { thenPathParametersExist(openapi, api) } + @Test + fun `should convert resource with head http method`() { + val api = givenHeadResourceModel() + + val openapi = OpenApi20Generator.generate(api) + println(Json.pretty().writeValueAsString(openapi)) + + thenHeadRequestExist(openapi, api) + } + + @Test + fun `should convert resource with options http method`() { + val api = givenOptionsResourceModel() + + val openapi = OpenApi20Generator.generate(api) + println(Json.pretty().writeValueAsString(openapi)) + + thenOptionsRequestExist(openapi, api) + } + + private fun thenOptionsRequestExist(openapi: Swagger, api: List) { + then(openapi.getPath(api.get(0).request.path).options).isNotNull() + } + + private fun thenHeadRequestExist(openapi: Swagger, api: List) { + then(openapi.getPath(api.get(0).request.path).head).isNotNull() + } + private fun thenPathParametersExist(openapi: Swagger, api: List) { val path = openapi.paths.getValue(api[0].request.path).get then(path.parameters.firstOrNull()).isNotNull @@ -272,6 +300,30 @@ class OpenApi20GeneratorTest { ) } + private fun givenHeadResourceModel(): List { + return listOf( + ResourceModel( + operationId = "testHead", + privateResource = false, + deprecated = false, + request = headRequest(), + response = getProductDummyResponse() + ) + ) + } + + private fun givenOptionsResourceModel(): List { + return listOf( + ResourceModel( + operationId = "testOptions", + privateResource = false, + deprecated = false, + request = optionsRequest(), + response = getProductDummyResponse() + ) + ) + } + private fun postProduct200Response(example: String): ResponseModel { return ResponseModel( status = 200, @@ -341,6 +393,16 @@ class OpenApi20GeneratorTest { ) } + private fun getProductDummyResponse(): ResponseModel { + return ResponseModel( + status = 200, + contentType = "application/json", + headers = listOf(), + responseFields = listOf(), + example = "{}" + ) + } + private fun getProductPayloadExample(): String { return "{\n" + " \"_id\": \"123\",\n" + @@ -473,6 +535,38 @@ class OpenApi20GeneratorTest { ) } + private fun headRequest(): RequestModel { + return RequestModel( + path = "/products", + method = HTTPMethod.HEAD, + securityRequirements = SecurityRequirements( + type = OAUTH2, + requiredScopes = listOf() + ), + headers = listOf(), + pathParameters = listOf( + ), + requestParameters = listOf(), + requestFields = listOf() + ) + } + + private fun optionsRequest(): RequestModel { + return RequestModel( + path = "/products", + method = HTTPMethod.OPTIONS, + securityRequirements = SecurityRequirements( + type = OAUTH2, + requiredScopes = listOf() + ), + headers = listOf(), + pathParameters = listOf( + ), + requestParameters = listOf(), + requestFields = listOf() + ) + } + private fun deleteProduct204Response(): ResponseModel { return ResponseModel( status = 204, From a4d6a617fe415ab8ae158bdd7c6718419c6190c8 Mon Sep 17 00:00:00 2001 From: Alex Sandau Date: Mon, 13 Aug 2018 14:49:33 +0200 Subject: [PATCH 2/2] Lint fix --- .../restdocs/openapi/gradle/OpenApi20GeneratorTest.kt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/restdocs-openapi-gradle-plugin/src/test/kotlin/com/epages/restdocs/openapi/gradle/OpenApi20GeneratorTest.kt b/restdocs-openapi-gradle-plugin/src/test/kotlin/com/epages/restdocs/openapi/gradle/OpenApi20GeneratorTest.kt index 7c664108..db8e89c4 100644 --- a/restdocs-openapi-gradle-plugin/src/test/kotlin/com/epages/restdocs/openapi/gradle/OpenApi20GeneratorTest.kt +++ b/restdocs-openapi-gradle-plugin/src/test/kotlin/com/epages/restdocs/openapi/gradle/OpenApi20GeneratorTest.kt @@ -544,8 +544,7 @@ class OpenApi20GeneratorTest { requiredScopes = listOf() ), headers = listOf(), - pathParameters = listOf( - ), + pathParameters = listOf(), requestParameters = listOf(), requestFields = listOf() ) @@ -560,8 +559,7 @@ class OpenApi20GeneratorTest { requiredScopes = listOf() ), headers = listOf(), - pathParameters = listOf( - ), + pathParameters = listOf(), requestParameters = listOf(), requestFields = listOf() )