Skip to content

Commit 728274f

Browse files
committed
feat: Preserve OpenAPI extensions on Spec
1 parent d1e6114 commit 728274f

3 files changed

Lines changed: 59 additions & 2 deletions

File tree

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,23 @@ public record Spec(
1919
Map<String, Parameter> componentParameters,
2020
String basePath,
2121
Map<String, Schema> schemaRefIndex,
22-
Map<String, Parameter> parameterRefIndex) {
22+
Map<String, Parameter> parameterRefIndex,
23+
Map<String, Object> extensions) {
2324

2425
private static final String SCHEMA_KEY = "schema";
2526
private static final String SCHEMA_REF_PREFIX = "#/components/schemas/";
2627
private static final String PARAMETER_REF_PREFIX = "#/components/parameters/";
2728

29+
static Map<String, Object> extractExtensions(Map<String, Object> raw) {
30+
Map<String, Object> out = new LinkedHashMap<>();
31+
for (var e : raw.entrySet()) {
32+
if (e.getKey().startsWith("x-")) {
33+
out.put(e.getKey(), e.getValue());
34+
}
35+
}
36+
return Map.copyOf(out);
37+
}
38+
2839
@SuppressWarnings("unchecked")
2940
public static Spec from(Map<String, Object> raw) {
3041
String openapi = (String) raw.get("openapi");
@@ -46,7 +57,8 @@ public static Spec from(Map<String, Object> raw) {
4657
componentParameters,
4758
computeBasePath(servers),
4859
indexByRef(componentSchemas, SCHEMA_REF_PREFIX),
49-
indexByRef(componentParameters, PARAMETER_REF_PREFIX));
60+
indexByRef(componentParameters, PARAMETER_REF_PREFIX),
61+
extractExtensions(raw));
5062
}
5163

5264
private static String computeBasePath(List<Server> servers) {

src/test/java/com/retailsvc/http/internal/RequestPreparationFilterTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ private Spec specWith(Operation... ops) {
5252
Map.of(),
5353
"",
5454
Map.of(),
55+
Map.of(),
5556
Map.of());
5657
}
5758

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.retailsvc.http.spec;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.List;
6+
import java.util.Map;
7+
import org.junit.jupiter.api.Test;
8+
9+
class ExtensionsTest {
10+
11+
@Test
12+
void specExtensionsExposeTopLevelXKeys() {
13+
Map<String, Object> raw =
14+
Map.of(
15+
"openapi",
16+
"3.1.0",
17+
"info",
18+
Map.of("title", "t", "version", "1"),
19+
"servers",
20+
List.of(Map.of("url", "https://example.com")),
21+
"paths",
22+
Map.of(),
23+
"x-vendor-build",
24+
"abc");
25+
Spec spec = Spec.from(raw);
26+
assertThat(spec.extensions()).containsEntry("x-vendor-build", "abc");
27+
}
28+
29+
@Test
30+
void specExtensionsEmptyWhenNoXKeys() {
31+
Map<String, Object> raw =
32+
Map.of(
33+
"openapi",
34+
"3.1.0",
35+
"info",
36+
Map.of("title", "t", "version", "1"),
37+
"servers",
38+
List.of(Map.of("url", "https://example.com")),
39+
"paths",
40+
Map.of());
41+
Spec spec = Spec.from(raw);
42+
assertThat(spec.extensions()).isEmpty();
43+
}
44+
}

0 commit comments

Comments
 (0)