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

Commit 8be1972

Browse files
authored
Merge pull request #23 from openapi-processor/ref-parameter
resolves #23
2 parents 44c1d76 + 635ee9d commit 8be1972

File tree

10 files changed

+101
-20
lines changed

10 files changed

+101
-20
lines changed

src/main/kotlin/io/openapiprocessor/core/parser/openapi4j/OpenApi.kt

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,22 @@ import org.openapi4j.parser.model.v3.Path as O4jPath
2828
*
2929
* @author Martin Hauner
3030
*/
31-
class OpenApi(private val api: O4jOpenApi, private val validations: ValidationResults): ParserOpenApi {
31+
class OpenApi(
32+
private val api: O4jOpenApi,
33+
private val validations: ValidationResults,
34+
): ParserOpenApi {
35+
private val refResolver: RefResolverNative = RefResolverNative(api)
3236

3337
override fun getPaths(): Map<String, ParserPath> {
3438
val paths = linkedMapOf<String, ParserPath>()
3539

3640
api.paths.forEach { (name: String, value: O4jPath) ->
3741
var path = value
38-
if (isRef(value)) {
39-
path = resolve (path)
42+
if (path.isRef) {
43+
path = refResolver.resolve(path)
4044
}
4145

42-
paths[name] = Path(name, path)
46+
paths[name] = Path(name, path, refResolver)
4347
}
4448

4549
return paths
@@ -50,12 +54,4 @@ class OpenApi(private val api: O4jOpenApi, private val validations: ValidationRe
5054
override fun printWarnings() {
5155
}
5256

53-
private fun resolve(path: O4jPath): O4jPath {
54-
return path.getReference (api.context).getMappedContent (O4jPath::class.java)
55-
}
56-
57-
private fun isRef(path: O4jPath): Boolean {
58-
return path.ref != null
59-
}
60-
6157
}

src/main/kotlin/io/openapiprocessor/core/parser/openapi4j/Operation.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ import org.openapi4j.parser.model.v3.Response as O4jResponse
3030
*
3131
* @author Martin Hauner
3232
*/
33-
class Operation(private val method: HttpMethod, private val operation: O4jOperation): ParserOperation {
33+
class Operation(
34+
private val method: HttpMethod,
35+
private val operation: O4jOperation,
36+
private val refResolver: RefResolverNative
37+
): ParserOperation {
3438

3539
override fun getMethod(): HttpMethod = method
3640

@@ -42,7 +46,12 @@ class Operation(private val method: HttpMethod, private val operation: O4jOperat
4246
val parameters = mutableListOf<ParserParameter>()
4347

4448
operation.parameters?.map { p: O4jParameter ->
45-
parameters.add(Parameter(p))
49+
var param = p
50+
if(p.isRef) {
51+
param = refResolver.resolve(p)
52+
}
53+
54+
parameters.add(Parameter(param))
4655
}
4756

4857
return parameters

src/main/kotlin/io/openapiprocessor/core/parser/openapi4j/Path.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ import org.openapi4j.parser.model.v3.Path as Oa4jPath
2626
*
2727
* @author Martin Hauner
2828
*/
29-
class Path(private val path: String, private val info: Oa4jPath): ParserPath {
29+
class Path(
30+
private val path: String,
31+
private val info: Oa4jPath,
32+
private val refResolver: RefResolverNative
33+
): ParserPath {
3034

3135
override fun getPath(): String = path
3236

@@ -36,7 +40,7 @@ class Path(private val path: String, private val info: Oa4jPath): ParserPath {
3640
HttpMethod.values().map {
3741
val op = info.getOperation(it.method)
3842
if (op != null) {
39-
ops.add (Operation(it, op))
43+
ops.add (Operation(it, op, refResolver))
4044
}
4145
}
4246

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright © 2020 https://github.com/openapi-processor/openapi-processor-core
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiprocessor.core.parser.openapi4j
7+
8+
import org.openapi4j.parser.model.v3.OpenApi3
9+
import org.openapi4j.parser.model.v3.Path as O4jPath
10+
import org.openapi4j.parser.model.v3.Parameter as O4jParameter
11+
12+
/**
13+
* openapi4j $ref resolver on o4j types. Resolves non-schema $ref's.
14+
*/
15+
class RefResolverNative(private val api: OpenApi3) {
16+
17+
fun resolve(path: O4jPath): O4jPath {
18+
return path.getReference(api.context).getMappedContent(O4jPath::class.java)
19+
}
20+
21+
fun resolve(param: O4jParameter): O4jParameter {
22+
return param.getReference(api.context).getMappedContent(O4jParameter::class.java)
23+
}
24+
25+
}

src/testInt/groovy/com/github/hauner/openapi/processor/core/ProcessorEndToEndTest.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class ProcessorEndToEndTest extends ProcessorTestBase {
3232

3333
static def testSets = TestSet.ALL + [
3434
'deprecated',
35-
'ref-into-another-file-path'
35+
'ref-into-another-file-path',
36+
'ref-parameter'
3637
]
3738

3839
@Parameterized.Parameters(name = "{0}")

src/testInt/groovy/com/github/hauner/openapi/processor/core/ProcessorPendingTest.groovy

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,17 @@ import com.github.hauner.openapi.test.TestSet
2323
import org.junit.Test
2424
import org.junit.runner.RunWith
2525
import org.junit.runners.Parameterized
26+
import spock.lang.Ignore
2627

27-
//@Ignore
28+
@Ignore
2829
@RunWith(Parameterized)
2930
class ProcessorPendingTest extends ProcessorTestBase {
3031

3132
@Parameterized.Parameters(name = "{0}")
3233
static Collection<TestSet> sources () {
3334
return [
34-
new TestSet(name: 'ref-array-items-nested', processor: new TestProcessor(), parser: ParserType.OPENAPI4J),
35-
new TestSet(name: 'ref-array-items-nested', processor: new TestProcessor(), parser: ParserType.SWAGGER)
35+
new TestSet(name: 'ref-parameter', processor: new TestProcessor(), parser: ParserType.OPENAPI4J),
36+
new TestSet(name: 'ref-parameter', processor: new TestProcessor(), parser: ParserType.SWAGGER)
3637
]
3738
}
3839

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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
11+
public interface Api {
12+
13+
@Mapping("/foo")
14+
String getFoo(@Parameter String bar);
15+
16+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
items:
2+
- inputs/openapi.yaml
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
openapi: 3.0.3
2+
info:
3+
title: ref parameters
4+
version: 1.0.0
5+
6+
paths:
7+
/foo:
8+
get:
9+
parameters:
10+
- $ref: "#/components/parameters/Bar"
11+
responses:
12+
'200':
13+
description: plain text response
14+
content:
15+
plain/text:
16+
schema:
17+
type: string
18+
19+
components:
20+
parameters:
21+
Bar:
22+
in: query
23+
name: bar
24+
schema:
25+
type: string

0 commit comments

Comments
 (0)