@@ -8,10 +8,15 @@ package io.openapiparser
88import io.kotest.core.spec.style.StringSpec
99import io.kotest.matchers.shouldBe
1010import io.openapiparser.model.v31.OpenApi
11+ import io.openapiprocessor.jackson.JacksonJsonWriter
12+ import io.openapiprocessor.jackson.JacksonYamlWriter
1113import io.openapiprocessor.jsonschema.reader.UriReader
1214import io.openapiprocessor.jsonschema.schema.DocumentLoader
1315import io.openapiprocessor.jsonschema.schema.DocumentStore
1416import io.openapiprocessor.snakeyaml.SnakeYamlConverter
17+ import io.openapiprocessor.snakeyaml.SnakeYamlWriter
18+ import java.io.FileWriter
19+ import java.io.StringWriter
1520import java.net.URI
1621
1722class 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