Skip to content

Commit 1f55142

Browse files
committed
bundling result is always an object
1 parent 0afdff5 commit 1f55142

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

openapi-parser/src/main/java/io/openapiparser/OpenApiBundler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public OpenApiBundler (Context context, DocumentStore documents, Bucket root) {
4242
this.rootDocumentUri = root.getScope ().getDocumentUri ();
4343
}
4444

45-
public Object bundle () {
45+
public Map<String, Object> bundle () {
4646
URI documentUri = root.getScope ().getDocumentUri ();
4747
Object document = documents.get (documentUri);
4848

openapi-parser/src/main/java/io/openapiparser/OpenApiResult.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import java.io.IOException;
1313
import java.util.Collection;
14+
import java.util.Map;
1415

1516
/**
1617
* OpenAPI parser result.
@@ -41,11 +42,11 @@ enum Version { V30, V31 }
4142
<T> T getModel (Class<T> api);
4243

4344
/**
44-
* bundle the document. The bundled document has to be parsed again to navigate the OpenAPI model.
45+
* Experimental. Bundle the document. The bundled document has to be parsed to navigate its OpenAPI model.
4546
*
46-
* @return the bundled document tree.
47+
* @return a raw bundled copy of the OpenAPI document.
4748
*/
48-
Object bundle();
49+
Map<String, Object> bundle();
4950

5051
/**
5152
* Experimental. Write the document. This will produce useful results only if the document is a single file

openapi-parser/src/main/java/io/openapiparser/OpenApiResult30.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.Collection;
1818
import java.util.Collections;
1919
import java.util.List;
20+
import java.util.Map;
2021
import java.util.stream.Collectors;
2122

2223
import static io.openapiparser.OpenApiSchemas.OPENAPI_SCHEMA_30;
@@ -52,7 +53,7 @@ public <T> T getModel (Class<T> api) {
5253
}
5354

5455
@Override
55-
public Object bundle () {
56+
public Map<String, Object> bundle () {
5657
return new OpenApiBundler (context, documents, root).bundle ();
5758
}
5859

openapi-parser/src/main/java/io/openapiparser/OpenApiResult31.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Collection;
2222
import java.util.Collections;
2323
import java.util.List;
24+
import java.util.Map;
2425
import java.util.stream.Collectors;
2526

2627
import static io.openapiparser.OpenApiSchemas.OPENAPI_SCHEMA_31;
@@ -58,7 +59,7 @@ public <T> T getModel (Class<T> api) {
5859
}
5960

6061
@Override
61-
public Object bundle () {
62+
public Map<String, Object> bundle () {
6263
return new OpenApiBundler (context, documents, root).bundle ();
6364
}
6465

openapi-parser/src/test/kotlin/io/openapiparser/BundleSpec.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ package io.openapiparser
88
import io.kotest.core.spec.style.StringSpec
99
import io.kotest.matchers.shouldBe
1010
import io.openapiparser.model.v31.OpenApi
11+
import io.openapiprocessor.jackson.JacksonJsonWriter
12+
import io.openapiprocessor.jackson.JacksonYamlWriter
1113
import io.openapiprocessor.jsonschema.reader.UriReader
1214
import io.openapiprocessor.jsonschema.schema.DocumentLoader
1315
import io.openapiprocessor.jsonschema.schema.DocumentStore
1416
import io.openapiprocessor.snakeyaml.SnakeYamlConverter
17+
import io.openapiprocessor.snakeyaml.SnakeYamlWriter
18+
import java.io.FileWriter
19+
import java.io.StringWriter
1520
import java.net.URI
1621

1722
class BundleSpec : StringSpec({
@@ -36,4 +41,42 @@ class BundleSpec : StringSpec({
3641
val api = bundled.getModel(OpenApi::class.java)
3742
api.components!!.schemas.size shouldBe 2
3843
}
44+
45+
"bundle & save example" {
46+
// val loader = DocumentLoader(UriReader(), SnakeYamlConverter())
47+
48+
// parse a multi file OpeAPI document (from resources)
49+
val originalDocs = DocumentStore()
50+
val originalParser = OpenApiParser(originalDocs, loader)
51+
val originalResult = originalParser.parse("/bundle-ref-nested/openapi31.yaml")
52+
53+
// bundle the OpenAPI, i.e. move $ref content to "components.schemas" and friends if the
54+
// ref points to a different file. The original document is not touched.
55+
val bundled = originalResult.bundle()
56+
57+
// parse the bundled document (this creates a reference map for ref lookups) to navigate
58+
// its OpenAPI model.
59+
val bundledDocs = DocumentStore()
60+
val bundledParser = OpenApiParser(bundledDocs, loader)
61+
val bundledResult = bundledParser.parse(URI.create("/bundle-ref-nested/openapi31.yaml"), bundled)
62+
63+
// We could use the same document store, but it would override the original (root) document
64+
// if we use the same id (in this case file name). We can use a different id to avoid that.
65+
//val bundledParser = OpenApiParser(bundledDocs, loader)
66+
//val bundledResult = bundledParser.parse(URI.create("/bundled/openapi.yaml"), bundled)
67+
68+
val api = bundledResult.getModel(OpenApi::class.java)
69+
val summary = api.info.summary
70+
71+
// write the bundled OpenAPI document. Use the alternative constructor to configure the
72+
// jackson, snake yaml specific formatting.
73+
val out = StringWriter()
74+
// val out = FileWriter("bundled.yaml")
75+
val writer = JacksonYamlWriter(out)
76+
//val writer = SnakeYamlWriter(out)
77+
//val writer = JacksonJsonWriter(out)
78+
bundledResult.write(writer)
79+
80+
println(out.toString())
81+
}
3982
})

0 commit comments

Comments
 (0)