Skip to content

Commit 28f0e76

Browse files
committed
get all operations of a path item
1 parent 248dd5f commit 28f0e76

File tree

5 files changed

+94
-4
lines changed

5 files changed

+94
-4
lines changed

openapi-parser/src/main/java/io/openapiparser/Keywords.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package io.openapiparser;
77

88
import java.util.Collection;
9+
import java.util.Set;
910
import java.util.stream.Collectors;
1011

1112
public interface Keywords {
@@ -107,6 +108,8 @@ public interface Keywords {
107108
String WRITE_ONLY = "writeOnly";
108109
String XML = "xml";
109110

111+
Set<String> OPERATIONS = Set.of(DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT, TRACE);
112+
110113
static Collection<String> getProperties(Collection<Keyword> keywords) {
111114
return keywords
112115
.stream ()

openapi-parser/src/main/java/io/openapiparser/model/v30/PathItem.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
package io.openapiparser.model.v30;
77

88
import io.openapiparser.*;
9+
import io.openapiparser.Properties;
910
import io.openapiprocessor.jsonschema.schema.Bucket;
1011
import org.checkerframework.checker.nullness.qual.Nullable;
1112

12-
import java.util.Collection;
13-
import java.util.Map;
13+
import java.util.*;
1414

1515
import static io.openapiparser.Keywords.*;
1616

@@ -80,6 +80,16 @@ public PathItem getRefObject () {
8080
return getOperation (TRACE);
8181
}
8282

83+
public Map<String, Operation> getOperations () {
84+
Map<String, Operation> operations = new LinkedHashMap<>();
85+
bucket.forEach ((operation, value) -> {
86+
if (OPERATIONS.contains (operation)) {
87+
operations.put(operation, getOperation (operation));
88+
}
89+
});
90+
return Collections.unmodifiableMap (operations);
91+
}
92+
8393
public Collection<Server> getServers () {
8494
return getObjectsOrEmpty (SERVERS, Server.class);
8595
}

openapi-parser/src/main/java/io/openapiparser/model/v31/PathItem.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
package io.openapiparser.model.v31;
77

88
import io.openapiparser.*;
9+
import io.openapiparser.Properties;
910
import io.openapiprocessor.jsonschema.schema.Bucket;
1011
import org.checkerframework.checker.nullness.qual.Nullable;
1112

12-
import java.util.Collection;
13-
import java.util.Map;
13+
import java.util.*;
1414

1515
import static io.openapiparser.Keywords.*;
1616

@@ -82,6 +82,16 @@ public PathItem getRefObject () {
8282
return getOperation (TRACE);
8383
}
8484

85+
public Map<String, Operation> getOperations () {
86+
Map<String, Operation> operations = new LinkedHashMap<>();
87+
bucket.forEach ((operation, value) -> {
88+
if (OPERATIONS.contains (operation)) {
89+
operations.put(operation, getOperation (operation));
90+
}
91+
});
92+
return Collections.unmodifiableMap (operations);
93+
}
94+
8595
public Collection<Server> getServers () {
8696
return getObjectsOrEmpty (SERVERS, Server.class);
8797
}

openapi-parser/src/test/kotlin/io/openapiparser/model/v30/PathItemSpec.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package io.openapiparser.model.v30
77

88
import io.kotest.core.spec.style.StringSpec
99
import io.kotest.matchers.booleans.shouldBeFalse
10+
import io.kotest.matchers.collections.shouldContainExactly
11+
import io.kotest.matchers.maps.shouldHaveSize
1012
import io.kotest.matchers.nulls.shouldNotBeNull
1113
import io.kotest.matchers.shouldBe
1214
import io.openapiparser.support.ApiBuilder
@@ -88,4 +90,29 @@ class PathItemSpec : StringSpec({
8890
path?.description shouldBe "a description"
8991
}
9092

93+
"gets operations map in source order" {
94+
val api = ApiBuilder()
95+
.withApi("""
96+
paths:
97+
/foo:
98+
get: {}
99+
put: {}
100+
post: {}
101+
delete: {}
102+
options: {}
103+
head: {}
104+
patch: {}
105+
trace: {}
106+
""".trimIndent())
107+
.buildOpenApi30()
108+
109+
val path = api.paths.getPathItem("/foo")
110+
111+
val operations = path?.operations
112+
operations?.shouldHaveSize(8)
113+
114+
operations?.keys.shouldContainExactly(linkedSetOf(
115+
"get", "put", "post", "delete", "options", "head", "patch", "trace"
116+
))
117+
}
91118
})
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.openapiparser.model.v31
2+
3+
/*
4+
* Copyright 2021 https://github.com/openapi-processor/openapi-parser
5+
* PDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
import io.kotest.core.spec.style.StringSpec
9+
import io.kotest.matchers.collections.shouldContainExactly
10+
import io.kotest.matchers.maps.shouldHaveSize
11+
import io.openapiparser.support.ApiBuilder
12+
13+
class PathItemSpec : StringSpec({
14+
15+
"gets operations map in source order" {
16+
val api = ApiBuilder()
17+
.withApi("""
18+
paths:
19+
/foo:
20+
get: {}
21+
put: {}
22+
post: {}
23+
delete: {}
24+
options: {}
25+
head: {}
26+
patch: {}
27+
trace: {}
28+
""".trimIndent())
29+
.buildOpenApi30()
30+
31+
val path = api.paths.getPathItem("/foo")
32+
33+
val operations = path?.operations
34+
operations?.shouldHaveSize(8)
35+
36+
operations?.keys.shouldContainExactly(linkedSetOf(
37+
"get", "put", "post", "delete", "options", "head", "patch", "trace"
38+
))
39+
}
40+
})

0 commit comments

Comments
 (0)