Skip to content

Commit 5121ae7

Browse files
committed
Add Schema extension tests
Signed-off-by: Michael Edgar <michael@xlate.io>
1 parent 8d9fe1c commit 5121ae7

1 file changed

Lines changed: 104 additions & 0 deletions

File tree

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* Copyright (c) 2026 Contributors to the Eclipse Foundation
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.eclipse.microprofile.openapi.tck;
18+
19+
import static org.hamcrest.MatcherAssert.assertThat;
20+
import static org.hamcrest.Matchers.is;
21+
import static org.hamcrest.Matchers.sameInstance;
22+
23+
import java.util.Collections;
24+
import java.util.List;
25+
import java.util.Map;
26+
27+
import org.eclipse.microprofile.openapi.OASFactory;
28+
import org.eclipse.microprofile.openapi.models.media.Schema;
29+
import org.eclipse.microprofile.openapi.models.media.Schema.SchemaType;
30+
import org.testng.annotations.Test;
31+
32+
/**
33+
* This test covers the Extensible aspect of the Schema model, checking that properties are recognized correctly as
34+
* extensions by an implementation and handled according to the Schema documentation.
35+
*/
36+
public class SchemaExtensionPropertyTest {
37+
38+
private static final String EXTNAME = "my-extension";
39+
40+
@Test
41+
public void testExtensionSetForUnknownProperty() {
42+
Schema schema = OASFactory.createSchema();
43+
Object theExtension = new Object();
44+
schema.set(EXTNAME, theExtension);
45+
46+
assertThat(schema.get(EXTNAME), is(sameInstance(theExtension)));
47+
assertThat(schema.getExtension(EXTNAME), is(sameInstance(theExtension)));
48+
}
49+
50+
@Test
51+
public void testExtensionSetAllForUnknownProperty() {
52+
Schema schema = OASFactory.createSchema();
53+
Object theExtension = new Object();
54+
schema.setAll(Map.of(
55+
"type", List.of(SchemaType.STRING),
56+
EXTNAME, theExtension));
57+
58+
assertThat(schema.getType(), is(List.of(SchemaType.STRING)));
59+
assertThat(schema.get(EXTNAME), is(sameInstance(theExtension)));
60+
assertThat(schema.getExtension(EXTNAME), is(sameInstance(theExtension)));
61+
}
62+
63+
@Test
64+
public void testExtensionAvailableFromGet() {
65+
Schema schema = OASFactory.createSchema();
66+
Object theExtension = new Object();
67+
schema.addExtension(EXTNAME, theExtension);
68+
69+
assertThat(schema.get(EXTNAME), is(sameInstance(theExtension)));
70+
assertThat(schema.getExtension(EXTNAME), is(sameInstance(theExtension)));
71+
}
72+
73+
@Test
74+
public void testExtensionSetWithNonnullDialect() {
75+
Schema schema = OASFactory.createSchema();
76+
schema.setSchemaDialect("https://spec.openapis.org/oas/3.1/dialect/base");
77+
Object theExtension = new Object();
78+
schema.set(EXTNAME, theExtension);
79+
assertThat(schema.getExtension(EXTNAME), is(sameInstance(theExtension)));
80+
}
81+
82+
@Test
83+
public void testSetAllClearsExtensions() {
84+
Schema schema = OASFactory.createSchema();
85+
Object theExtension = new Object();
86+
schema.set(EXTNAME, theExtension);
87+
assertThat(schema.getExtension(EXTNAME), is(sameInstance(theExtension)));
88+
schema.setAll(Collections.emptyMap());
89+
assertThat(schema.hasExtension(EXTNAME), is(false));
90+
}
91+
92+
@Test
93+
public void testNullExtensionNotAdded() {
94+
Schema schema = OASFactory.createSchema();
95+
96+
try {
97+
schema.addExtension(EXTNAME, null);
98+
} catch (Exception e) {
99+
// May or may not be thrown
100+
}
101+
102+
assertThat(schema.hasExtension(EXTNAME), is(false));
103+
}
104+
}

0 commit comments

Comments
 (0)