Skip to content

Commit d46418b

Browse files
committed
overlay: info/action objects (#120)
1 parent fea669c commit d46418b

File tree

9 files changed

+277
-7
lines changed

9 files changed

+277
-7
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ public interface Keywords {
112112

113113
// overlay
114114
String OVERLAY = "overlay";
115+
String EXTENDS = "extends";
116+
String TARGET = "target";
117+
String UPDATE = "update";
118+
String REMOVE = "remove";
119+
String ACTIONS = "actions";
115120

116121
static Collection<String> getProperties(Collection<Keyword> keywords) {
117122
return keywords
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2024 https://github.com/openapi-processor/openapi-parser
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiparser.model.ov10;
7+
8+
import io.openapiparser.Context;
9+
import io.openapiparser.Properties;
10+
import io.openapiparser.support.Required;
11+
import io.openapiprocessor.jsonschema.schema.Bucket;
12+
import org.checkerframework.checker.nullness.qual.Nullable;
13+
14+
import java.util.Map;
15+
16+
import static io.openapiparser.Keywords.*;
17+
18+
/**
19+
* the <em>Action</em> object.
20+
*
21+
* <p>See specification:
22+
* <a href="https://spec.openapis.org/overlay/v1.0.0.html#action-object">4.4.3 Action Object</a>
23+
*/
24+
public class Action extends Properties implements Extensions {
25+
26+
public Action(Context context, Bucket bucket) {
27+
super(context, bucket);
28+
}
29+
30+
@Required
31+
public String getTarget() {
32+
return getStringOrThrow (TARGET);
33+
}
34+
35+
public @Nullable String getDescription () {
36+
return getStringOrNull (DESCRIPTION);
37+
}
38+
39+
public @Nullable Object getUpdate () {
40+
return getRawValue (UPDATE);
41+
}
42+
43+
public @Nullable <T> T getUpdate (Class<T> api) {
44+
return getObjectOrThrow(UPDATE, api);
45+
}
46+
47+
public @Nullable Boolean getRemove () {
48+
return getBooleanOrNull (REMOVE);
49+
}
50+
51+
@Override
52+
public Map<String, Object> getExtensions() {
53+
return super.getExtensions ();
54+
}
55+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2024 https://github.com/openapi-processor/openapi-parser
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiparser.model.ov10;
7+
8+
import io.openapiparser.Context;
9+
import io.openapiparser.Properties;
10+
import io.openapiparser.support.Required;
11+
import io.openapiprocessor.jsonschema.schema.Bucket;
12+
13+
import java.util.Map;
14+
15+
import static io.openapiparser.Keywords.TITLE;
16+
import static io.openapiparser.Keywords.VERSION;
17+
18+
/**
19+
* the <em>Info</em> object.
20+
*
21+
* <p>See specification:
22+
* <a href="https://spec.openapis.org/overlay/v1.0.0.html#info-object">4.4.2 Info Object</a>
23+
*/
24+
public class Info extends Properties implements Extensions {
25+
26+
public Info(Context context, Bucket bucket) {
27+
super (context, bucket);
28+
}
29+
30+
@Required
31+
public String getTitle () {
32+
return getStringOrThrow (TITLE);
33+
}
34+
35+
@Required
36+
public String getVersion () {
37+
return getStringOrThrow (VERSION);
38+
}
39+
40+
@Override
41+
public Map<String, Object> getExtensions () {
42+
return super.getExtensions ();
43+
}
44+
}

openapi-parser/src/main/java/io/openapiparser/model/ov10/Overlay.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
import io.openapiparser.Context;
99
import io.openapiparser.Properties;
1010
import io.openapiparser.support.Required;
11+
import io.openapiprocessor.jsonschema.converter.NoValueException;
1112
import io.openapiprocessor.jsonschema.schema.Bucket;
1213

14+
import java.util.Collection;
1315
import java.util.Map;
1416

15-
import static io.openapiparser.Keywords.OVERLAY;
17+
import static io.openapiparser.Keywords.*;
1618

1719
/**
1820
* the <em>Overlay</em> object.
@@ -31,6 +33,24 @@ public String getOverlay () {
3133
return getStringOrThrow (OVERLAY);
3234
}
3335

36+
public String getExtends () {
37+
return getStringOrNull (EXTENDS);
38+
}
39+
40+
@Required
41+
public Info getInfo () {
42+
return getObjectOrThrow (INFO, Info.class);
43+
}
44+
45+
@Required
46+
public Collection<Action> getActions () {
47+
Collection<Action> actions = getObjectsOrEmpty(ACTIONS, Action.class);
48+
if (actions == null || actions.isEmpty()) {
49+
throw new NoValueException(bucket.getLocation().append(ACTIONS));
50+
}
51+
return actions;
52+
}
53+
3454
@Override
3555
public Map<String, Object> getExtensions() {
3656
return super.getExtensions ();
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2024 https://github.com/openapi-processor/openapi-parser
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiparser.model.ov10
7+
8+
import io.kotest.assertions.throwables.shouldThrow
9+
import io.kotest.core.spec.style.StringSpec
10+
import io.kotest.matchers.booleans.shouldBeTrue
11+
import io.kotest.matchers.nulls.shouldBeNull
12+
import io.kotest.matchers.nulls.shouldNotBeNull
13+
import io.kotest.matchers.shouldBe
14+
import io.openapiparser.model.v3x.testExtensions
15+
import io.openapiprocessor.jsonschema.converter.NoValueException
16+
import io.openapiprocessor.jsonschema.converter.TypeMismatchException
17+
import io.openapiparser.model.v31.Info as Info31
18+
19+
class ActionSpec: StringSpec({
20+
21+
"get target" {
22+
action("target: $.info").target shouldBe "$.info"
23+
}
24+
25+
"get target throws if it is missing" {
26+
shouldThrow<NoValueException> { action().target }
27+
}
28+
29+
"get action description" {
30+
action("description: description").description shouldBe "description"
31+
}
32+
33+
"gets action description is null if it is missing" {
34+
action().description.shouldBeNull()
35+
}
36+
37+
"get update" {
38+
action("update: {}").update.shouldNotBeNull()
39+
}
40+
41+
"get update is null if it is missing" {
42+
action().update.shouldBeNull()
43+
}
44+
45+
"get update as type" {
46+
action("update: {title: the title}").getUpdate(Info31::class.java).shouldNotBeNull()
47+
}
48+
49+
"get update as type throws if conversion fails" {
50+
shouldThrow<TypeMismatchException> {
51+
action("update: false").getUpdate(Info31::class.java)
52+
}
53+
}
54+
55+
"get remove" {
56+
action("remove: true").remove!!.shouldBeTrue()
57+
}
58+
59+
"get remove is null if it is missing" {
60+
action().remove.shouldBeNull()
61+
}
62+
63+
include(testExtensions("Action", ::action) { it.extensions })
64+
})
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2024 https://github.com/openapi-processor/openapi-parser
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiparser.model.ov10
7+
8+
import io.openapiparser.support.buildObject
9+
10+
fun overlay(content: String = "{}"): Overlay {
11+
return buildObject(Overlay::class.java, content)
12+
}
13+
14+
fun info(content: String = "{}"): Info {
15+
return buildObject(Info::class.java, content)
16+
}
17+
18+
fun action(content: String = "{}"): Action {
19+
return buildObject(Action::class.java, content)
20+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2024 https://github.com/openapi-processor/openapi-parser
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiparser.model.ov10
7+
8+
import io.kotest.assertions.throwables.shouldThrow
9+
import io.kotest.core.spec.style.StringSpec
10+
import io.kotest.matchers.shouldBe
11+
import io.openapiparser.model.v3x.testExtensions
12+
import io.openapiprocessor.jsonschema.converter.NoValueException
13+
14+
class InfoSpec: StringSpec({
15+
16+
"gets info title" {
17+
info("title: the title").title shouldBe "the title"
18+
}
19+
20+
"gets info title throws if it is missing" {
21+
shouldThrow<NoValueException> { info().title }
22+
}
23+
24+
"gets info version" {
25+
info("""version: "1"""").version shouldBe "1"
26+
}
27+
28+
"gets info version throws if it is missing" {
29+
shouldThrow<NoValueException> { info().version }
30+
}
31+
32+
include(testExtensions("Info", ::info) { it.extensions })
33+
})

openapi-parser/src/test/kotlin/io/openapiparser/model/ov10/OverlaySpec.kt

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ package io.openapiparser.model.ov10
77

88
import io.kotest.assertions.throwables.shouldThrow
99
import io.kotest.core.spec.style.StringSpec
10+
import io.kotest.matchers.nulls.shouldBeNull
11+
import io.kotest.matchers.nulls.shouldNotBeNull
1012
import io.kotest.matchers.shouldBe
11-
import io.openapiparser.model.v30.overlay
13+
import io.openapiparser.model.v3x.testExtensions
1214
import io.openapiprocessor.jsonschema.converter.NoValueException
1315

1416
class OverlaySpec: StringSpec({
@@ -20,4 +22,36 @@ class OverlaySpec: StringSpec({
2022
"get overlay version throws if it is missing" {
2123
shouldThrow<NoValueException> { overlay().overlay }
2224
}
25+
26+
"gets info object" {
27+
overlay("info: {}").info.shouldNotBeNull()
28+
}
29+
30+
"gets info object throws if it missing" {
31+
shouldThrow<NoValueException> { overlay().info }
32+
}
33+
34+
"get extends url" {
35+
overlay("extends: https://a.url").extends shouldBe "https://a.url"
36+
}
37+
38+
"get extends url is null if it is missing" {
39+
overlay().extends.shouldBeNull()
40+
}
41+
42+
"get action objects" {
43+
val actions = overlay("actions: [{}, {}]").actions
44+
actions.shouldNotBeNull()
45+
actions.size shouldBe 2
46+
}
47+
48+
"get action object throws if it is missing" {
49+
shouldThrow<NoValueException> { overlay().actions }
50+
}
51+
52+
"get action object throws if it is empty" {
53+
shouldThrow<NoValueException> { overlay("actions: []").actions }
54+
}
55+
56+
include(testExtensions("Overlay", ::overlay) { it.extensions })
2357
})

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
package io.openapiparser.model.v30
77

8-
import io.openapiparser.model.ov10.Overlay
98
import io.openapiparser.support.buildObject
109

1110
fun callback(content: String = "{}"): Callback {
@@ -99,7 +98,3 @@ fun serverVariable(content: String = "{}"): ServerVariable {
9998
fun xml(content: String = "{}"): Xml {
10099
return buildObject(Xml::class.java, content)
101100
}
102-
103-
fun overlay(content: String = "{}"): Overlay {
104-
return buildObject(Overlay::class.java, content)
105-
}

0 commit comments

Comments
 (0)