Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.

Commit 59694fa

Browse files
committed
openapi-processor/openapi-processor-spring#140, finish global additional parameters
1 parent b1004f9 commit 59694fa

File tree

11 files changed

+97
-5
lines changed

11 files changed

+97
-5
lines changed

src/main/kotlin/io/openapiprocessor/core/converter/ApiConverter.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,26 @@ class ApiConverter(
114114
ep.parameters.add (createParameter (ep, parameter, dataTypes, resolver))
115115
}
116116

117-
val addMappings = mappingFinder.findEndpointAddParameterTypeMappings (ep.path, ep.method)
117+
val addMappings = getAdditionalParameter (ep)
118118
addMappings.forEach {
119119
ep.parameters.add (createAdditionalParameter (it, dataTypes, resolver))
120120
}
121121
}
122122

123+
private fun getAdditionalParameter(ep: Endpoint): List<AddParameterTypeMapping> {
124+
// check endpoint parameter mappings
125+
val epMatch = mappingFinder.findEndpointAddParameterTypeMappings (ep.path, ep.method)
126+
if (epMatch.isNotEmpty())
127+
return epMatch
128+
129+
// check global parameter mappings
130+
val paramMatch = mappingFinder.findAddParameterTypeMappings()
131+
if (paramMatch.isNotEmpty())
132+
return paramMatch
133+
134+
return emptyList()
135+
}
136+
123137
private fun collectRequestBody(requestBody: RequestBody?, ep: Endpoint, dataTypes: DataTypes, resolver: RefResolver) {
124138
if (requestBody == null) {
125139
return

src/main/kotlin/io/openapiprocessor/core/converter/mapping/MappingFinder.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,27 @@ class MappingFinder(private val typeMappings: List<Mapping> = emptyList()) {
7676
return getTypeMapping(filterMappings(TypeMatcher(info), typeMappings))
7777
}
7878

79+
/**
80+
* find a matching (global) add parameter type mapping.
81+
*
82+
* @return the matching mappings or an empty list.
83+
* @throws AmbiguousTypeMappingException if there is more than one match.
84+
*/
85+
fun findAddParameterTypeMappings(): List<AddParameterTypeMapping> {
86+
val matches = typeMappings
87+
.filterIsInstance(AddParameterTypeMapping::class.java)
88+
89+
if (matches.isNotEmpty())
90+
return matches
91+
92+
return emptyList()
93+
}
94+
7995
/**
8096
* find all (endpoint) add parameter type mappings for the given schema info.
8197
*
8298
* @param path the endpoint path
83-
* @return the matching mapping or null if there is no match.
99+
* @return the matching mappings or an empty list.
84100
* @throws AmbiguousTypeMappingException if there is more than one match.
85101
*/
86102
fun findEndpointAddParameterTypeMappings(path: String, method: HttpMethod): List<AddParameterTypeMapping> {

src/testInt/kotlin/io/openapiprocessor/core/ProcessorPendingSpec.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import io.openapiprocessor.test.TestSetRunner
1919
class ProcessorPendingSpec: StringSpec({
2020

2121
for (testSet in sources()) {
22-
"native - $testSet".config(enabled = true) {
22+
"native - $testSet".config(enabled = false) {
2323
val folder = tempdir()
2424

2525
val support = FileSupport(
@@ -37,8 +37,8 @@ private fun sources(): Collection<TestSet> {
3737
return listOf(
3838
// testSet("params-additional-new", ParserType.SWAGGER, API_30),
3939
// testSet("params-additional-new", ParserType.OPENAPI4J, API_30),
40-
testSet("params-additional", INTERNAL, API_30),
41-
testSet("params-additional", INTERNAL, API_31)
40+
// testSet("params-additional-global", INTERNAL, API_30),
41+
// testSet("params-additional-global", INTERNAL, API_31)
4242
)
4343
}
4444

src/testInt/kotlin/io/openapiprocessor/core/ProcessorTestSets.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ val ALL_30: List<TestSet> = listOf(
2525
TestSet("object-read-write-properties", API_30),
2626
TestSet("object-without-properties", API_30),
2727
TestSet("params-additional", API_30),
28+
TestSet("params-additional-global", API_30),
2829
TestSet("params-complex-data-types", API_30), // framework specific
2930
TestSet("params-endpoint", API_30),
3031
TestSet("params-enum", API_30),
@@ -80,6 +81,7 @@ val ALL_31: List<TestSet> = listOf(
8081
TestSet("object-read-write-properties", API_31),
8182
TestSet("object-without-properties", API_31),
8283
TestSet("params-additional", API_31),
84+
TestSet("params-additional-global", API_31),
8385
TestSet("params-complex-data-types", API_31), // framework specific
8486
TestSet("params-endpoint", API_31),
8587
TestSet("params-enum", API_31),
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
items:
2+
- generated/api/Api.java
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* This class is auto generated by https://github.com/hauner/openapi-processor-core.
3+
* TEST ONLY.
4+
*/
5+
6+
package generated.api;
7+
8+
import annotation.Mapping;
9+
import annotation.Parameter;
10+
import javax.servlet.http.HttpServletRequest;
11+
12+
public interface Api {
13+
14+
@Mapping("/foo")
15+
void getFoo(@Parameter String foo, @Parameter HttpServletRequest request);
16+
17+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
items:
2+
- inputs/openapi30.yaml
3+
- inputs/openapi31.yaml
4+
- inputs/foo.yaml
5+
- inputs/mapping.yaml
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
get:
2+
parameters:
3+
- name: foo
4+
description: query, required
5+
in: query
6+
required: true
7+
schema:
8+
type: string
9+
responses:
10+
'204':
11+
description: empty
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
openapi-processor-mapping: v2
2+
3+
options:
4+
package-name: generated
5+
6+
map:
7+
8+
parameters:
9+
- add: request => javax.servlet.http.HttpServletRequest
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
openapi: 3.0.2
2+
info:
3+
title: test additional global parameters
4+
version: 1.0.0
5+
6+
paths:
7+
/foo:
8+
$ref: 'foo.yaml'
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
openapi: 3.1.0
2+
info:
3+
title: test additional global parameters
4+
version: 1.0.0
5+
6+
paths:
7+
/foo:
8+
$ref: 'foo.yaml'

0 commit comments

Comments
 (0)