This repository was archived by the owner on Mar 16, 2025. It is now read-only.
File tree 4 files changed +30
-14
lines changed
main/kotlin/io/openapiprocessor/core/parser
test/groovy/com/github/hauner/openapi/core/converter
4 files changed +30
-14
lines changed Original file line number Diff line number Diff line change @@ -18,11 +18,12 @@ package io.openapiprocessor.core.parser
18
18
19
19
/* *
20
20
* Resolves $ref objects from an OpenAPI.
21
- *
22
- * @author Martin Hauner
23
21
*/
24
22
interface RefResolver {
23
+ fun resolve (ref : Schema ): NamedSchema
24
+ }
25
25
26
- fun resolve (ref : Schema ): Schema
27
-
26
+ class NamedSchema (val name : String? , val schema : Schema ) {
27
+ val hasName = name != null
28
+ val hasNoName = name == null
28
29
}
Original file line number Diff line number Diff line change 16
16
17
17
package io.openapiprocessor.core.parser.openapi4j
18
18
19
+ import io.openapiprocessor.core.parser.NamedSchema
19
20
import io.openapiprocessor.core.parser.RefResolver as ParserRefResolver
20
21
import io.openapiprocessor.core.parser.Schema as ParserSchema
21
22
import org.openapi4j.parser.model.v3.OpenApi3 as O4jOpenApi
@@ -28,7 +29,7 @@ import org.openapi4j.parser.model.v3.Schema as O4jSchema
28
29
*/
29
30
class RefResolver (private val api : O4jOpenApi ): ParserRefResolver {
30
31
31
- override fun resolve (ref : ParserSchema ): ParserSchema {
32
+ override fun resolve (ref : ParserSchema ): NamedSchema {
32
33
val resolved: O4jSchema
33
34
34
35
val refName = getRefName(ref.getRef()!! )
@@ -40,11 +41,16 @@ class RefResolver(private val api: O4jOpenApi): ParserRefResolver {
40
41
o4jSchema.getReference(api.context).getMappedContent(O4jSchema ::class .java)
41
42
}
42
43
43
- return Schema ( resolved)
44
+ return NamedSchema (refName, Schema ( resolved) )
44
45
}
45
46
46
- private fun getRefName (ref : String ): String {
47
- return ref.substring(ref.lastIndexOf(' /' ) + 1 )
47
+ private fun getRefName (ref : String ): String? {
48
+ val split = ref.split(' #' )
49
+ if (split.size > 1 ) {
50
+ val hash = split[1 ]
51
+ return hash.substring(hash.lastIndexOf(' /' ) + 1 )
52
+ }
53
+ return null
48
54
}
49
55
50
56
}
Original file line number Diff line number Diff line change 16
16
17
17
package io.openapiprocessor.core.parser.swagger
18
18
19
+ import io.openapiprocessor.core.parser.NamedSchema as ParserNamedSchema
19
20
import io.openapiprocessor.core.parser.RefResolver as ParserRefResolver
20
21
import io.openapiprocessor.core.parser.Schema as ParserSchema
21
22
import io.swagger.v3.oas.models.media.Schema as SwaggerSchema
@@ -28,19 +29,24 @@ import io.swagger.v3.oas.models.OpenAPI
28
29
*/
29
30
class RefResolver (private val openapi : OpenAPI ): ParserRefResolver {
30
31
31
- override fun resolve (ref : ParserSchema ): ParserSchema {
32
+ override fun resolve (ref : ParserSchema ): ParserNamedSchema {
32
33
val refName = getRefName(ref.getRef()!! )
33
34
34
35
val schema: SwaggerSchema <* >? = openapi.components?.schemas?.get(refName)
35
36
if (schema == null ) {
36
37
throw Exception (" failed to resolve ${ref.getRef()} " )
37
38
}
38
39
39
- return Schema (schema)
40
+ return ParserNamedSchema (refName, Schema (schema) )
40
41
}
41
42
42
- private fun getRefName (ref : String ): String {
43
- return ref.substring(ref.lastIndexOf(' /' ) + 1 )
43
+ private fun getRefName (ref : String ): String? {
44
+ val split = ref.split(' #' )
45
+ if (split.size > 1 ) {
46
+ val hash = split[1 ]
47
+ return hash.substring(hash.lastIndexOf(' /' ) + 1 )
48
+ }
49
+ return null
44
50
}
45
51
46
52
}
Original file line number Diff line number Diff line change @@ -29,8 +29,10 @@ import io.openapiprocessor.core.model.HttpMethod
29
29
import io.openapiprocessor.core.model.datatypes.ObjectDataType
30
30
import com.github.hauner.openapi.core.test.TestSchema
31
31
import io.openapiprocessor.core.converter.mapping.UnknownDataTypeException
32
+ import io.openapiprocessor.core.parser.NamedSchema
32
33
import io.openapiprocessor.core.parser.RefResolver
33
34
import io.openapiprocessor.core.parser.Schema
35
+ import org.jetbrains.annotations.NotNull
34
36
import spock.lang.Specification
35
37
import spock.lang.Unroll
36
38
@@ -126,9 +128,10 @@ class DataTypeConverterSpec extends Specification {
126
128
])
127
129
128
130
def resolver = new RefResolver () {
131
+
129
132
@Override
130
- Schema resolve (Schema ref) {
131
- barSchema
133
+ NamedSchema resolve (@NotNull Schema ref) {
134
+ return new NamedSchema( " Bar " , barSchema)
132
135
}
133
136
}
134
137
You can’t perform that action at this time.
0 commit comments