@@ -7,37 +7,29 @@ package io.openapiparser.support
7
7
8
8
import io.openapiparser.*
9
9
import io.openapiprocessor.jsonschema.support.Types
10
- import io.openapiprocessor.jsonschema.reader.StringReader
11
- import io.openapiprocessor.jsonschema.reader.UriReader
12
10
import io.openapiprocessor.jsonschema.schema.Bucket
13
11
import io.openapiprocessor.jsonschema.schema.DocumentLoader
14
12
import io.openapiprocessor.jsonschema.schema.DocumentStore
15
13
import io.openapiprocessor.jsonschema.schema.Resolver
16
14
import io.openapiprocessor.jsonschema.schema.SchemaVersion
17
15
import io.openapiprocessor.snakeyaml.SnakeYamlConverter
18
16
import io.openapiprocessor.interfaces.Converter
19
- import io.openapiprocessor.interfaces.Reader
20
17
import java.net.URI
21
18
import io.openapiparser.model.v30.OpenApi as OpenApi30
22
19
import io.openapiparser.model.v31.OpenApi as OpenApi31
23
20
24
21
class ApiBuilder {
25
- private var api: String? = null
26
- private lateinit var apiUri: URI
22
+ private val reader = TestReader ()
27
23
private var converter: Converter = SnakeYamlConverter ()
28
24
private var documents: DocumentStore = DocumentStore ()
29
25
30
- fun withApi (api : String ): ApiBuilder {
31
- return withYaml(" file:///any" , api.trimIndent())
32
- }
33
-
34
- fun withApi (api : URI ): ApiBuilder {
35
- apiUri = api
26
+ fun withApi (apiYaml : String ): ApiBuilder {
27
+ reader.addApi(URI (" file:///openapi.yaml" ), apiYaml.trimIndent())
36
28
return this
37
29
}
38
30
39
- fun withResource ( api : String ): ApiBuilder {
40
- apiUri = this :: class .java.getResource(api) !! .toURI( )
31
+ fun withApi ( uri : String , apiYaml : String ): ApiBuilder {
32
+ reader.addApi( URI (uri), apiYaml.trimIndent() )
41
33
return this
42
34
}
43
35
@@ -46,59 +38,69 @@ class ApiBuilder {
46
38
return this
47
39
}
48
40
41
+ fun withDocument (uri : URI , document : Any ): ApiBuilder {
42
+ documents.addId(uri, document)
43
+ return this
44
+ }
45
+
49
46
fun buildParser (): OpenApiParser {
50
- return OpenApiParser (documents, DocumentLoader (getReader() , converter))
47
+ return OpenApiParser (documents, DocumentLoader (reader , converter))
51
48
}
52
49
53
50
fun buildOpenApi30 (): OpenApi30 {
51
+ return buildOpenApi30(" file:///openapi.yaml" )
52
+ }
53
+
54
+ fun buildOpenApi30 (uri : String ): OpenApi30 {
55
+ var source = URI (uri)
56
+ if (! source.isAbsolute) {
57
+ source = this ::class .java.getResource(uri)!! .toURI()
58
+ }
59
+
54
60
val resolver = createResolver()
55
- val result = resolver.resolve(apiUri , Resolver .Settings (SchemaVersion .Draft4 ))
61
+ val result = resolver.resolve(source , Resolver .Settings (SchemaVersion .Draft4 ))
56
62
57
63
return OpenApi30 (
58
64
Context (result.scope, result.registry),
59
65
Bucket (result.scope, Types .asMap(result.document)!! ))
60
66
}
61
67
62
68
fun buildOpenApi31 (): OpenApi31 {
69
+ return buildOpenApi31(" file:///openapi.yaml" )
70
+ }
71
+
72
+ fun buildOpenApi31 (uri : String ): OpenApi31 {
73
+ var source = URI (uri)
74
+ if (! source.isAbsolute) {
75
+ source = this ::class .java.getResource(uri)!! .toURI()
76
+ }
77
+
63
78
val resolver = createResolver()
64
- val result = resolver.resolve(apiUri , Resolver .Settings (SchemaVersion .Draft201909 ))
79
+ val result = resolver.resolve(source , Resolver .Settings (SchemaVersion .Draft201909 ))
65
80
66
81
return OpenApi31 (
67
82
Context (result.scope, result.registry),
68
83
Bucket (result.scope, Types .asMap(result.document)!! ))
69
84
}
70
85
71
86
fun <T > build (clazz : Class <T >): T {
72
- return build { c, n -> clazz
87
+ return build30 { c, n -> clazz
73
88
.getDeclaredConstructor(Context ::class .java, Bucket ::class .java)
74
89
.newInstance(c, n)
75
90
}
76
91
}
77
92
78
- private fun <T > build (factory : (context: Context , bucket: Bucket ) -> T ): T {
93
+ private fun <T > build30 (factory : (context: Context , bucket: Bucket ) -> T ): T {
79
94
val resolver = createResolver()
80
- val result = resolver.resolve(apiUri , Resolver .Settings (SchemaVersion .Draft4 ))
95
+ val result = resolver.resolve(URI ( " file:///openapi.yaml " ) , Resolver .Settings (SchemaVersion .Draft4 ))
81
96
82
97
return factory(
83
98
Context (result.scope, result.registry),
84
99
Bucket (result.scope, Types .asMap(result.document)!! ))
85
100
}
86
101
87
102
private fun createResolver (): Resolver {
88
- return Resolver (documents, DocumentLoader (getReader(), converter))
89
- }
90
-
91
- private fun withYaml (baseUri : String , api : String ): ApiBuilder {
92
- this .apiUri = URI (baseUri)
93
- this .api = api
94
- return this
95
- }
96
-
97
- private fun getReader (): Reader {
98
- if (api == null ) {
99
- return UriReader ()
100
- }
101
- return StringReader (api!! )
103
+ return Resolver (documents, DocumentLoader (reader, converter))
102
104
}
103
105
}
104
106
0 commit comments