|
2 | 2 |
|
3 | 3 | import com.retailsvc.http.spec.schema.Schema; |
4 | 4 | import com.retailsvc.http.spec.schema.SchemaParser; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.UncheckedIOException; |
| 7 | +import java.lang.reflect.Method; |
5 | 8 | import java.net.URI; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
6 | 11 | import java.util.ArrayList; |
7 | 12 | import java.util.LinkedHashMap; |
8 | 13 | import java.util.List; |
@@ -36,6 +41,91 @@ static Map<String, Object> extractExtensions(Map<String, Object> raw) { |
36 | 41 | return Map.copyOf(out); |
37 | 42 | } |
38 | 43 |
|
| 44 | + private static final String GSON_CLASS = "com.google.gson.Gson"; |
| 45 | + private static final String SNAKEYAML_CLASS = "org.yaml.snakeyaml.Yaml"; |
| 46 | + |
| 47 | + /** |
| 48 | + * Reads an OpenAPI specification from {@code path}. Picks the parser by file extension: |
| 49 | + * |
| 50 | + * <ul> |
| 51 | + * <li>{@code .json} → Gson must be on the classpath. |
| 52 | + * <li>{@code .yaml} or {@code .yml} → SnakeYAML must be on the classpath. |
| 53 | + * </ul> |
| 54 | + * |
| 55 | + * <p>Both Gson and SnakeYAML are optional dependencies of this library. If the parser for the |
| 56 | + * file's extension is not present, throws {@link IllegalStateException} — register your own |
| 57 | + * parser and call {@link #from(Map)} instead. |
| 58 | + * |
| 59 | + * @throws UncheckedIOException if the file cannot be read |
| 60 | + * @throws IllegalStateException if the required parser is not on the classpath, or if the file |
| 61 | + * has an unrecognised extension |
| 62 | + */ |
| 63 | + public static Spec fromPath(Path path) { |
| 64 | + String text; |
| 65 | + try { |
| 66 | + text = Files.readString(path); |
| 67 | + } catch (IOException e) { |
| 68 | + throw new UncheckedIOException("Failed to read OpenAPI spec from " + path, e); |
| 69 | + } |
| 70 | + String name = path.getFileName().toString().toLowerCase(Locale.ROOT); |
| 71 | + Map<String, Object> raw; |
| 72 | + if (name.endsWith(".json")) { |
| 73 | + raw = parseJson(text); |
| 74 | + } else if (name.endsWith(".yaml") || name.endsWith(".yml")) { |
| 75 | + raw = parseYaml(text); |
| 76 | + } else { |
| 77 | + throw new IllegalStateException( |
| 78 | + "Unrecognised OpenAPI spec extension for " |
| 79 | + + path |
| 80 | + + " — expected .json, .yaml, or .yml. Parse the file yourself and call" |
| 81 | + + " Spec.from(Map<String, Object>) instead."); |
| 82 | + } |
| 83 | + return from(raw); |
| 84 | + } |
| 85 | + |
| 86 | + private static Map<String, Object> parseJson(String text) { |
| 87 | + Class<?> gsonClass = loadOptional(GSON_CLASS, "JSON", "Gson"); |
| 88 | + try { |
| 89 | + Object gson = gsonClass.getDeclaredConstructor().newInstance(); |
| 90 | + Method fromJson = gsonClass.getMethod("fromJson", String.class, Class.class); |
| 91 | + @SuppressWarnings("unchecked") |
| 92 | + Map<String, Object> raw = (Map<String, Object>) fromJson.invoke(gson, text, Map.class); |
| 93 | + return raw; |
| 94 | + } catch (ReflectiveOperationException e) { |
| 95 | + throw new IllegalStateException("Failed to parse OpenAPI spec via Gson", e); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private static Map<String, Object> parseYaml(String text) { |
| 100 | + Class<?> yamlClass = loadOptional(SNAKEYAML_CLASS, "YAML", "SnakeYAML"); |
| 101 | + try { |
| 102 | + Object yaml = yamlClass.getDeclaredConstructor().newInstance(); |
| 103 | + Method load = yamlClass.getMethod("load", String.class); |
| 104 | + @SuppressWarnings("unchecked") |
| 105 | + Map<String, Object> raw = (Map<String, Object>) load.invoke(yaml, text); |
| 106 | + return raw; |
| 107 | + } catch (ReflectiveOperationException e) { |
| 108 | + throw new IllegalStateException("Failed to parse OpenAPI spec via SnakeYAML", e); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private static Class<?> loadOptional(String className, String format, String libName) { |
| 113 | + try { |
| 114 | + return Class.forName(className, false, Spec.class.getClassLoader()); |
| 115 | + } catch (ClassNotFoundException e) { |
| 116 | + throw new IllegalStateException( |
| 117 | + "Spec.fromPath requires " |
| 118 | + + libName |
| 119 | + + " on the classpath for " |
| 120 | + + format |
| 121 | + + " specs. Add a " |
| 122 | + + libName |
| 123 | + + " dependency, or parse the file yourself and call" |
| 124 | + + " Spec.from(Map<String, Object>) instead.", |
| 125 | + e); |
| 126 | + } |
| 127 | + } |
| 128 | + |
39 | 129 | @SuppressWarnings("unchecked") |
40 | 130 | public static Spec from(Map<String, Object> raw) { |
41 | 131 | String openapi = (String) raw.get("openapi"); |
|
0 commit comments