@@ -29,8 +29,51 @@ public Parse() {
2929 * If parsing fails.
3030 */
3131 public static OpenAPI fromString (String content ) throws IOException {
32+ return fromString (content , new File ("." ).toURI ().toString ());
33+ }
34+
35+ /**
36+ * Parse OpenAPI description from string with a base URI for resolving
37+ * references.
38+ *
39+ * @param content
40+ * The JSON/YAML string.
41+ * @param baseUri
42+ * The base URI.
43+ * @return OpenAPI object.
44+ * @throws IOException
45+ * If parsing fails.
46+ */
47+ public static OpenAPI fromString (String content , String baseUri ) throws IOException {
48+ try {
49+ String trimmed = content .trim ();
50+ JSONObject root ;
51+ if (trimmed .startsWith ("{" )) {
52+ root = new JSONObject (trimmed );
53+ } else {
54+ org .yaml .snakeyaml .Yaml yaml = new org .yaml .snakeyaml .Yaml ();
55+ java .util .Map <String , Object > map = yaml .load (content );
56+ root = new JSONObject (map );
57+ }
58+ RefResolver resolver = new RefResolver ();
59+ resolver .bundle (root , baseUri );
60+ return fromJson (root );
61+ } catch (Exception e ) {
62+ throw new IOException ("Failed to parse OpenAPI: " + e .getMessage (), e );
63+ }
64+ }
65+
66+ /**
67+ * Parse OpenAPI description from JSONObject.
68+ *
69+ * @param root
70+ * The root JSONObject.
71+ * @return OpenAPI object.
72+ * @throws IOException
73+ * If parsing fails.
74+ */
75+ public static OpenAPI fromJson (JSONObject root ) throws IOException {
3276 try {
33- JSONObject root = new JSONObject (content );
3477 OpenAPI api = new OpenAPI ();
3578 if (root .has ("openapi" ))
3679 api .openapi = root .getString ("openapi" );
@@ -273,7 +316,7 @@ public static OpenAPI fromFile(File file) throws IOException {
273316 try (FileInputStream fis = new FileInputStream (file )) {
274317 byte [] data = new byte [(int ) file .length ()];
275318 fis .read (data );
276- return fromString (new String (data , "UTF-8" ));
319+ return fromString (new String (data , "UTF-8" ), file . toURI (). toString () );
277320 }
278321 }
279322}
0 commit comments