Skip to content

Commit dcb1385

Browse files
committed
refactor: Drop fromYaml(InputStream, Function) BYO-parser overload
SnakeYAML is the only YAML parser in common Java use, so the parser- injecting variant carries API surface for no real callers. Users who need a different YAML library can decode the stream themselves and call Spec.from(Map).
1 parent a972601 commit dcb1385

3 files changed

Lines changed: 3 additions & 40 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ try (InputStream in = YourServerLauncher.class.getResourceAsStream("/openapi.jso
134134
}
135135
```
136136

137-
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:
137+
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, supply your own JSON parser:
138138

139139
``` java
140140
ObjectMapper jackson = new ObjectMapper();
@@ -144,7 +144,7 @@ try (InputStream in = YourServerLauncher.class.getResourceAsStream("/openapi.jso
144144
}
145145
```
146146

147-
`Spec.fromYaml(InputStream, Function<byte[], Map<String, Object>>)` is the equivalent escape hatch for YAML.
147+
YAML always parses through SnakeYAML — there's no parser-injecting overload. If you want a different YAML library, decode the stream yourself and call `Spec.from(Map<String, Object>)`.
148148

149149
### JSON mapping
150150

src/main/java/com/retailsvc/http/spec/Spec.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,26 +142,12 @@ public static Spec fromJson(InputStream in, Function<byte[], Map<String, Object>
142142
* classpath; otherwise throws {@link IllegalStateException}. The stream is fully consumed and
143143
* closed before this method returns.
144144
*
145-
* <p>To avoid the SnakeYAML dependency, use {@link #fromYaml(InputStream, Function)} instead.
146-
*
147145
* @throws NullPointerException if {@code in} is {@code null}
148146
* @throws UncheckedIOException if the stream cannot be read
149147
* @throws IllegalStateException if SnakeYAML is not on the classpath
150148
*/
151149
public static Spec fromYaml(InputStream in) {
152-
return fromYaml(in, Spec::parseYamlWithSnakeYaml);
153-
}
154-
155-
/**
156-
* Reads a YAML OpenAPI specification from {@code in} using the supplied {@code parser}. The
157-
* stream is fully consumed and closed before this method returns.
158-
*
159-
* @throws NullPointerException if {@code in} or {@code parser} is {@code null}
160-
* @throws UncheckedIOException if the stream cannot be read
161-
*/
162-
public static Spec fromYaml(InputStream in, Function<byte[], Map<String, Object>> parser) {
163-
Objects.requireNonNull(parser, "parser");
164-
return from(parser.apply(readAll(in)));
150+
return from(parseYamlWithSnakeYaml(readAll(in)));
165151
}
166152

167153
private static byte[] readAll(InputStream in) {

src/test/java/com/retailsvc/http/spec/SpecFromInputStreamTest.java

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.util.concurrent.atomic.AtomicBoolean;
1414
import java.util.function.Function;
1515
import org.junit.jupiter.api.Test;
16-
import org.yaml.snakeyaml.Yaml;
1716

1817
class SpecFromInputStreamTest {
1918

@@ -51,19 +50,6 @@ void fromJsonWithCustomParserDoesNotRequireGson() throws Exception {
5150
}
5251
}
5352

54-
@Test
55-
void fromYamlWithCustomParserDoesNotRequireSnakeYaml() throws Exception {
56-
Yaml yaml = new Yaml();
57-
Function<byte[], Map<String, Object>> parser =
58-
bytes -> yaml.load(new String(bytes, StandardCharsets.UTF_8));
59-
60-
try (InputStream in = getClass().getResourceAsStream("/openapi.yaml")) {
61-
Spec spec = Spec.fromYaml(in, parser);
62-
63-
assertThat(spec.openapi()).startsWith("3.1");
64-
}
65-
}
66-
6753
@Test
6854
void fromJsonClosesStream() throws Exception {
6955
AtomicBoolean closed = new AtomicBoolean(false);
@@ -126,15 +112,6 @@ void fromJsonWithParserRejectsNullArgs() {
126112
assertThatThrownBy(() -> Spec.fromJson(in, null)).isInstanceOf(NullPointerException.class);
127113
}
128114

129-
@Test
130-
void fromYamlWithParserRejectsNullArgs() {
131-
InputStream in = new ByteArrayInputStream(new byte[0]);
132-
Function<byte[], Map<String, Object>> parser = bytes -> Map.of();
133-
134-
assertThatThrownBy(() -> Spec.fromYaml(null, parser)).isInstanceOf(NullPointerException.class);
135-
assertThatThrownBy(() -> Spec.fromYaml(in, null)).isInstanceOf(NullPointerException.class);
136-
}
137-
138115
@Test
139116
void fromJsonPropagatesIoFailure() {
140117
InputStream broken = brokenStream();

0 commit comments

Comments
 (0)