Skip to content

Commit a78213a

Browse files
authored
Merge pull request #146 from mikrethor/fix/131-incorrect-mapping-head-trace-options
Fix 131 Incorrect Mapping for @HeadMapping/@TraceMapping/@OptionsMapping
2 parents 21f5587 + a4f594f commit a78213a

File tree

3 files changed

+95
-5
lines changed

3 files changed

+95
-5
lines changed

src/main/kotlin/io/openapiprocessor/spring/writer/java/MappingAnnotationWriter.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ package io.openapiprocessor.spring.writer.java
1818

1919
import io.openapiprocessor.core.model.Endpoint
2020
import io.openapiprocessor.core.model.EndpointResponse
21+
import io.openapiprocessor.core.model.HttpMethod
2122
import io.openapiprocessor.core.support.capitalizeFirstChar
22-
import io.openapiprocessor.core.writer.java.MappingAnnotationWriter as CoreMappingAnnotationWriter
2323
import java.io.Writer
24+
import io.openapiprocessor.core.writer.java.MappingAnnotationWriter as CoreMappingAnnotationWriter
2425

2526
/**
2627
* spring mapping annotation writer
@@ -60,12 +61,23 @@ class MappingAnnotationWriter: CoreMappingAnnotationWriter {
6061
mapping += "}"
6162
}
6263

64+
val method = endpoint.method.method
65+
if (method in arrayOf("head","trace","options")) {
66+
mapping += ", "
67+
mapping += "method = RequestMethod.${method.uppercase()}"
68+
}
69+
6370
mapping += ")"
6471
return mapping
6572
}
6673

6774
private fun getMappingAnnotation(endpoint: Endpoint): String {
68-
return "@${endpoint.method.method.capitalizeFirstChar ()}Mapping"
75+
return when(endpoint.method){
76+
HttpMethod.HEAD, HttpMethod.OPTIONS, HttpMethod.TRACE -> "@RequestMapping"
77+
else -> {
78+
"@${endpoint.method.method.capitalizeFirstChar ()}Mapping"
79+
}
80+
}
6981
}
7082

7183
private fun quote(content: String): String {

src/test/groovy/io/openapiprocessor/spring/writer/java/MappingAnnotationWriterSpec.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ class MappingAnnotationWriterSpec extends Specification {
4646
HttpMethod.PUT | "put-it" | """@PutMapping(path = "put-it")"""
4747
HttpMethod.POST | "post-it" | """@PostMapping(path = "post-it")"""
4848
HttpMethod.DELETE | "delete-it" | """@DeleteMapping(path = "delete-it")"""
49-
HttpMethod.OPTIONS | "options-it" | """@OptionsMapping(path = "options-it")"""
50-
HttpMethod.HEAD | "head-it" | """@HeadMapping(path = "head-it")"""
49+
HttpMethod.OPTIONS | "options-it" | """@RequestMapping(path = "options-it", method = RequestMethod.OPTIONS)"""
50+
HttpMethod.HEAD | "head-it" | """@RequestMapping(path = "head-it", method = RequestMethod.HEAD)"""
5151
HttpMethod.PATCH | "patch-it" | """@PatchMapping(path = "patch-it")"""
52-
HttpMethod.TRACE | "trace-it" | """@TraceMapping(path = "trace-it")"""
52+
HttpMethod.TRACE | "trace-it" | """@RequestMapping(path = "trace-it", method = RequestMethod.TRACE)"""
5353
}
5454

5555
void "writes 'consumes' parameter with body content type" () {

src/test/groovy/io/openapiprocessor/spring/writer/java/MethodWriterSpec.groovy

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,82 @@ class MethodWriterSpec extends Specification {
8282
"""
8383
}
8484

85+
void "head method" () {
86+
def dataTypeName = new DataTypeName('java.lang.String', 'java.lang.String')
87+
88+
def endpoint = createEndpoint (path: '/foo', method: HttpMethod.HEAD, responses: [
89+
'200': [new EmptyResponse()]
90+
], parameters: [
91+
new QueryParameter('foo', new MappedDataType (
92+
'String',
93+
'java.util',
94+
[],
95+
null,
96+
false,
97+
true
98+
), false, false, null)
99+
])
100+
101+
when:
102+
writer.write (target, endpoint, endpoint.endpointResponses.first ())
103+
104+
then:
105+
target.toString () == """\
106+
@RequestMapping(path = "${endpoint.path}", method = RequestMethod.HEAD)
107+
void headFoo(@RequestParam(name = "foo", required = false) String foo);
108+
"""
109+
}
110+
111+
void "trace method" () {
112+
def dataTypeName = new DataTypeName('java.lang.String', 'java.lang.String')
113+
114+
def endpoint = createEndpoint (path: '/foo', method: HttpMethod.TRACE, responses: [
115+
'200': [new EmptyResponse()]
116+
], parameters: [
117+
new QueryParameter('foo', new MappedDataType (
118+
'String',
119+
'java.util',
120+
[],
121+
null,
122+
false,
123+
true
124+
), false, false, null)
125+
])
126+
127+
when:
128+
writer.write (target, endpoint, endpoint.endpointResponses.first ())
129+
130+
then:
131+
target.toString () == """\
132+
@RequestMapping(path = "${endpoint.path}", method = RequestMethod.TRACE)
133+
void traceFoo(@RequestParam(name = "foo", required = false) String foo);
134+
"""
135+
}
136+
137+
void "option method" () {
138+
def dataTypeName = new DataTypeName('java.lang.String', 'java.lang.String')
139+
140+
def endpoint = createEndpoint (path: '/foo', method: HttpMethod.OPTIONS, responses: [
141+
'200': [new EmptyResponse()]
142+
], parameters: [
143+
new QueryParameter('foo', new MappedDataType (
144+
'String',
145+
'java.util',
146+
[],
147+
null,
148+
false,
149+
true
150+
), false, false, null)
151+
])
152+
153+
when:
154+
writer.write (target, endpoint, endpoint.endpointResponses.first ())
155+
156+
then:
157+
target.toString () == """\
158+
@RequestMapping(path = "${endpoint.path}", method = RequestMethod.OPTIONS)
159+
void optionsFoo(@RequestParam(name = "foo", required = false) String foo);
160+
"""
161+
}
162+
85163
}

0 commit comments

Comments
 (0)