From f05ae83249c37d6b41166bfd0e07509154ab3692 Mon Sep 17 00:00:00 2001 From: Martin Hauner Date: Mon, 18 Nov 2019 23:18:45 +0100 Subject: [PATCH] handle header parameter --- .../spring/converter/ApiConverter.groovy | 3 + .../model/parameters/HeaderParameter.groovy | 72 +++++++++++++++++++ .../ApiConverterParameterSpec.groovy | 40 +++++++++++ .../spring/writer/MethodWriterSpec.groovy | 35 +++++++++ 4 files changed, 150 insertions(+) create mode 100644 src/main/groovy/com/github/hauner/openapi/spring/model/parameters/HeaderParameter.groovy diff --git a/src/main/groovy/com/github/hauner/openapi/spring/converter/ApiConverter.groovy b/src/main/groovy/com/github/hauner/openapi/spring/converter/ApiConverter.groovy index 1d1cace8..098d03c1 100644 --- a/src/main/groovy/com/github/hauner/openapi/spring/converter/ApiConverter.groovy +++ b/src/main/groovy/com/github/hauner/openapi/spring/converter/ApiConverter.groovy @@ -18,6 +18,7 @@ package com.github.hauner.openapi.spring.converter import com.github.hauner.openapi.spring.model.Api import com.github.hauner.openapi.spring.model.Endpoint +import com.github.hauner.openapi.spring.model.parameters.HeaderParameter import com.github.hauner.openapi.spring.model.parameters.Parameter as ModelParameter import com.github.hauner.openapi.spring.model.parameters.PathParameter import com.github.hauner.openapi.spring.model.parameters.QueryParameter @@ -124,6 +125,8 @@ class ApiConverter { return new QueryParameter (name: parameter.name, required: parameter.required, dataType: dataType) case 'path': return new PathParameter (name: parameter.name, required: parameter.required, dataType: dataType) + case 'header': + return new HeaderParameter (name: parameter.name, required: parameter.required, dataType: dataType) default: // todo throw null diff --git a/src/main/groovy/com/github/hauner/openapi/spring/model/parameters/HeaderParameter.groovy b/src/main/groovy/com/github/hauner/openapi/spring/model/parameters/HeaderParameter.groovy new file mode 100644 index 00000000..293b6a6f --- /dev/null +++ b/src/main/groovy/com/github/hauner/openapi/spring/model/parameters/HeaderParameter.groovy @@ -0,0 +1,72 @@ +/* + * Copyright 2019 the original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.hauner.openapi.spring.model.parameters + +import com.github.hauner.openapi.spring.model.datatypes.DataType + +/** + * OpenAPI header parameter. + * + * @author Martin Hauner + */ +class HeaderParameter implements Parameter { + String name + boolean required + DataType dataType + + String getAnnotationName () { + "RequestHeader" + } + + String getAnnotationWithPackage () { + "org.springframework.web.bind.annotation.${annotationName}" + } + + String getAnnotation () { + "@${annotationName}" + } + + Set getDataTypeImports () { + dataType.imports + } + + /** + * Is the parameter required? + * + * @return true if the parameter is required, otherwise false. + */ + boolean isRequired () { + required + } + + /** + * Create annotation? + */ + boolean withAnnotation () { + true + } + + /** + * Create annotation with parameters? + * + * @return true if the annotation should have parameters, false otherwise + */ + boolean withParameters () { + true + } + +} diff --git a/src/test/groovy/com/github/hauner/openapi/spring/converter/ApiConverterParameterSpec.groovy b/src/test/groovy/com/github/hauner/openapi/spring/converter/ApiConverterParameterSpec.groovy index ad3e457c..a702e5d5 100644 --- a/src/test/groovy/com/github/hauner/openapi/spring/converter/ApiConverterParameterSpec.groovy +++ b/src/test/groovy/com/github/hauner/openapi/spring/converter/ApiConverterParameterSpec.groovy @@ -102,4 +102,44 @@ paths: param.annotationWithPackage == 'org.springframework.web.bind.annotation.PathVariable' } + void "converts simple header parameter"() { + def openApi = parse ( +"""\ +openapi: 3.0.2 +info: + title: test simple header parameter + version: 1.0.0 + +paths: + /endpoint: + + get: + tags: + - endpoint + parameters: + - name: x-foo + description: header, required, string + in: header + required: true + schema: + type: string + responses: + '204': + description: empty +""") + + when: + def api = new ApiConverter ().convert (openApi) + + then: + def itf = api.interfaces.first () + def ep = itf.endpoints.first () + def param = ep.parameters.first () + param.name == 'x-foo' + param.required + param.dataType.name == 'String' + param.annotation == '@RequestHeader' + param.annotationWithPackage == 'org.springframework.web.bind.annotation.RequestHeader' + } + } diff --git a/src/test/groovy/com/github/hauner/openapi/spring/writer/MethodWriterSpec.groovy b/src/test/groovy/com/github/hauner/openapi/spring/writer/MethodWriterSpec.groovy index f3ad56a3..086d958b 100644 --- a/src/test/groovy/com/github/hauner/openapi/spring/writer/MethodWriterSpec.groovy +++ b/src/test/groovy/com/github/hauner/openapi/spring/writer/MethodWriterSpec.groovy @@ -32,6 +32,7 @@ import com.github.hauner.openapi.spring.model.datatypes.NoneDataType import com.github.hauner.openapi.spring.model.datatypes.ObjectDataType import com.github.hauner.openapi.spring.model.datatypes.SetDataType import com.github.hauner.openapi.spring.model.datatypes.StringDataType +import com.github.hauner.openapi.spring.model.parameters.HeaderParameter import com.github.hauner.openapi.spring.model.parameters.QueryParameter import spock.lang.Specification import spock.lang.Unroll @@ -179,6 +180,40 @@ class MethodWriterSpec extends Specification { """ } + void "writes simple (required) header parameter" () { + def endpoint = new Endpoint (path: '/foo', method: HttpMethod.GET, responses: [ + new Response (contentType: 'application/json', responseType: new NoneDataType()) + ], parameters: [ + new HeaderParameter(name: 'x-foo', required: true, dataType: new StringDataType()) + ]) + + when: + writer.write (target, endpoint) + + then: + target.toString () == """\ + @GetMapping(path = "${endpoint.path}") + ResponseEntity getFoo(@RequestHeader(name = "x-foo") String xFoo); +""" + } + + void "writes simple (optional) header parameter" () { + def endpoint = new Endpoint (path: '/foo', method: HttpMethod.GET, responses: [ + new Response (contentType: 'application/json', responseType: new NoneDataType()) + ], parameters: [ + new HeaderParameter(name: 'x-foo', required: false, dataType: new StringDataType()) + ]) + + when: + writer.write (target, endpoint) + + then: + target.toString () == """\ + @GetMapping(path = "${endpoint.path}") + ResponseEntity getFoo(@RequestHeader(name = "x-foo", required = false) String xFoo); +""" + } + void "writes object query parameter without @RequestParam annotation" () { def endpoint = new Endpoint (path: '/foo', method: HttpMethod.GET, responses: [ new Response (contentType: 'application/json', responseType: new NoneDataType())