Skip to content

Commit a972601

Browse files
committed
docs: Hoist Spec out of try-with-resources in README examples
Surrounding text builds the server from spec, so it must be reachable after the try block. Use an uninitialized declaration rather than null-init — javac's definite-assignment analysis covers the only non-throwing path.
1 parent 083abf9 commit a972601

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,19 @@ public class YourServerLauncher {
128128
To load a spec from the classpath (including from inside a JAR) use the `InputStream` overloads:
129129

130130
``` java
131+
Spec spec;
131132
try (InputStream in = YourServerLauncher.class.getResourceAsStream("/openapi.json")) {
132-
Spec spec = Spec.fromJson(in); // Gson on the classpath
133+
spec = Spec.fromJson(in); // Gson on the classpath
133134
}
134135
```
135136

136137
The matching `Spec.fromYaml(InputStream)` uses SnakeYAML. Both close the stream before returning. If you can't (or don't want to) depend on Gson/SnakeYAML, supply your own parser:
137138

138139
``` java
139140
ObjectMapper jackson = new ObjectMapper();
141+
Spec spec;
140142
try (InputStream in = YourServerLauncher.class.getResourceAsStream("/openapi.json")) {
141-
Spec spec = Spec.fromJson(in, bytes -> jackson.readValue(bytes, Map.class));
143+
spec = Spec.fromJson(in, bytes -> jackson.readValue(bytes, Map.class));
142144
}
143145
```
144146

0 commit comments

Comments
 (0)