Skip to content

Commit eeda20b

Browse files
committed
accept OpenAPI parameter names (openapi-processor/openapi-processor-spring#365)
1 parent 39abef0 commit eeda20b

File tree

7 files changed

+67
-21
lines changed

7 files changed

+67
-21
lines changed

openapi-processor-core/src/main/antlr/Mapping.g4

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,36 @@ type
1414
: anyType
1515
;
1616

17+
// basic type mapping
1718
map
1819
: sourceType Arrow anyType
1920
;
2021

22+
// content type mapping
2123
content
2224
: contentType Arrow anyType
2325
;
2426

27+
// annotation mapping
2528
annotate
2629
: sourceType Annotate annotationType
2730
;
2831

32+
// add, drop parameter
2933
name
30-
: sourceIdentifier
34+
: sourceType
3135
;
3236

3337
anyType
34-
: plainType | primitiveType | targetType
38+
: plainType | primitiveType | targetType | FormatType | Primitive
3539
;
3640

3741
plainType
3842
: Plain
3943
;
4044

4145
primitiveType
42-
: primitiveValue | primitiveValue OpenArray CloseArray
46+
: Primitive | Primitive OpenArray CloseArray
4347
;
4448

4549
sourceType
@@ -91,15 +95,11 @@ genericParameterAny
9195
;
9296

9397
sourceIdentifier
94-
: Identifier | String
98+
: Identifier | ApiIdentifier | String | FormatType | Primitive
9599
;
96100

97101
formatIdentifier
98-
: Identifier | Format | String | primitiveValue
99-
;
100-
101-
primitiveValue
102-
: 'byte' | 'short' | 'int' | 'long' | 'float' | 'double' | 'boolean' | 'char'
102+
: Identifier | ApiIdentifier | String | FormatType | Primitive
103103
;
104104

105105

@@ -123,8 +123,12 @@ Whitespace
123123
: [ \t] -> skip
124124
;
125125

126+
Primitive
127+
: 'byte' | 'short' | 'int' | 'long' | 'float' | 'double' | 'boolean' | 'char'
128+
;
129+
126130
Identifier
127-
: JavaLetter JavaLetterOrDigit*
131+
: JavaLetter JavaLetterOrDigit* | FormatType | Primitive
128132
;
129133

130134
QualifiedTypeClass
@@ -135,10 +139,15 @@ QualifiedType
135139
: (Identifier | Package) ('.' Identifier)*
136140
;
137141

138-
Format
139-
: FormatLetter FormatLetterOrDigit*
142+
// Api types with optional format
143+
FormatType
144+
: 'string' | 'integer' | 'number'
140145
;
141146

147+
ApiIdentifier
148+
: ApiLetter (ApiLetterOrDigit | [ \t])+ ApiLetterOrDigit+ | FormatType | Primitive
149+
;
150+
142151
String
143152
: DoubleQuote ( ~["\\] | '\\' [\t\\"] )* DoubleQuote
144153
;
@@ -168,10 +177,10 @@ fragment JavaLetterOrDigit
168177
: [a-zA-Z0-9$_]
169178
;
170179

171-
fragment FormatLetter
180+
fragment ApiLetter
172181
: [a-zA-Z_-]
173182
;
174183

175-
fragment FormatLetterOrDigit
184+
fragment ApiLetterOrDigit
176185
: [a-zA-Z0-9_-]
177186
;

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/processor/mapping/v2/parser/antlr/MappingExtractor.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,8 @@ class MappingExtractor: MappingBaseListener(), Mapping {
150150
}
151151
}
152152
}
153+
154+
override fun enterName(ctx: MappingParser.NameContext) {
155+
sourceType = ctx.text
156+
}
153157
}

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/processor/mapping/v2/parser/antlr/Parser.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fun parseMapping(mapping: String): Mapping {
1919
val lexer = MappingLexer(CharStreams.fromString(mapping))
2020
val tokens = CommonTokenStream(lexer)
2121
val parser = MappingParser(tokens)
22-
parser.removeErrorListener(ConsoleErrorListener.INSTANCE);
22+
parser.removeErrorListener(ConsoleErrorListener.INSTANCE)
2323
parser.addErrorListener(MappingErrorListener())
2424
val ctx = parser.mapping()
2525
val extractor = MappingExtractor()

openapi-processor-core/src/test/kotlin/io/openapiprocessor/core/converter/mapping/MappingFinderEndpointMethodSpec.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,15 @@ class MappingFinderEndpointMethodSpec: StringSpec({
113113
| /foo:
114114
| get:
115115
| parameters:
116-
| - add: fooParam => io.openapiprocessor.Foo
116+
| - add: foo-Param => io.openapiprocessor.Foo
117117
| - add: barParam => io.openapiprocessor.Foo
118118
""")
119119

120120
val finder = mappingFinder(options)
121121
val result = finder.findAddParameterTypeMappings(query(path = "/foo", method = GET))
122122

123123
result.shouldNotBeEmpty()
124-
result[0].parameterName.shouldBe("fooParam")
124+
result[0].parameterName.shouldBe("foo-Param")
125125
result[1].parameterName.shouldBe("barParam")
126126
}
127127

@@ -133,15 +133,15 @@ class MappingFinderEndpointMethodSpec: StringSpec({
133133
| /foo:
134134
| get:
135135
| parameters:
136-
| - drop: fooParam
136+
| - drop: "foo-Param"
137137
| - drop: barParam
138138
""")
139139

140140
val finder = mappingFinder(options)
141141
val result = finder.findDropParameterTypeMappings(query(path = "/foo", method = GET))
142142

143143
result.shouldNotBeEmpty()
144-
result[0].parameterName.shouldBe("fooParam")
144+
result[0].parameterName.shouldBe("foo-Param")
145145
result[1].parameterName.shouldBe("barParam")
146146
}
147147

openapi-processor-core/src/test/kotlin/io/openapiprocessor/core/processor/mapping/v2/AntlrParserSpec.kt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ class AntlrParserSpec: StringSpec({
4343
mapping.targetGenericTypes.shouldBeEmpty()
4444
}
4545

46+
// only for string, number, integer
4647
"map source type with format to fully qualified java target type" {
47-
val source = "SourceType:format => io.oap.TargetType"
48+
val source = "string:format => io.oap.TargetType"
4849

4950
val mapping = parseMapping(source)
5051
mapping.kind shouldBe Mapping.Kind.MAP
51-
mapping.sourceType shouldBe "SourceType"
52+
mapping.sourceType shouldBe "string"
5253
mapping.sourceFormat shouldBe "format"
5354
mapping.targetType shouldBe "io.oap.TargetType"
5455
mapping.targetGenericTypes.shouldBeEmpty()
@@ -361,4 +362,22 @@ class AntlrParserSpec: StringSpec({
361362
mapping.sourceType shouldBe source
362363
mapping.targetType shouldBe null
363364
}
365+
366+
"map simple string with dash" {
367+
val source = "name-with-dash"
368+
369+
val mapping = parseMapping(source)
370+
mapping.kind shouldBe Mapping.Kind.TYPE
371+
mapping.sourceType shouldBe source
372+
mapping.targetType shouldBe null
373+
}
374+
375+
"map simple string with space" {
376+
val source = "name with space"
377+
378+
val mapping = parseMapping(source)
379+
mapping.kind shouldBe Mapping.Kind.TYPE
380+
mapping.sourceType shouldBe source
381+
mapping.targetType shouldBe null
382+
}
364383
})

openapi-processor-core/src/testInt/resources/tests/params-unnecessary/inputs/foo.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ get:
66
required: true
77
schema:
88
type: string
9+
- name: foo-x
10+
description: query, required
11+
in: query
12+
required: true
13+
schema:
14+
type: string
15+
- name: foo x
16+
description: query, required
17+
in: query
18+
required: true
19+
schema:
20+
type: string
921
responses:
1022
'204':
1123
description: empty

openapi-processor-core/src/testInt/resources/tests/params-unnecessary/inputs/mapping.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ map:
1010
/foo:
1111
parameters:
1212
- drop: foo
13+
- drop: foo-x
14+
- drop: foo x

0 commit comments

Comments
 (0)