Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<void> 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<void> 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())
Expand Down