Skip to content

Commit

Permalink
Merge pull request #678 from Azquelt/extensible-methods
Browse files Browse the repository at this point in the history
Add `Extensible.hasExtension` and `.getExtension`
  • Loading branch information
Azquelt authored Jan 23, 2025
2 parents 74ab942 + 1325b38 commit e80e76c
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,34 @@ default T extensions(Map<String, Object> extensions) {
*/
void setExtensions(Map<String, Object> extensions);

/**
* Checks whether an extension with the given name is present in this Extensible's map of extensions.
*
* @param name
* the key used to access the extension object. Always prefixed by "x-".
* @return {@code true} if an extension with the given name is present, otherwise {@code false}
*/
default boolean hasExtension(String name) {
Map<String, Object> map = getExtensions();
if (map == null) {
return false;
}
return map.containsKey(name);
}

/**
* Returns the extension object with the given name from this Extensible's map of extensions.
*
* @param name
* the key used to access the extension object. Always prefixed by "x-".
* @return the corresponding extension object, or {@code null} if no extension with the given name is present
*/
default Object getExtension(String name) {
Map<String, Object> map = getExtensions();
if (map == null) {
return null;
}
return map.get(name);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
* The behaviour of methods inherited from java.lang.Object are undefined by the MicroProfile OpenAPI specification.
*/

@org.osgi.annotation.versioning.Version("2.0")
@org.osgi.annotation.versioning.Version("2.1")
@org.osgi.annotation.versioning.ProviderType
package org.eclipse.microprofile.openapi.models.callbacks;
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
* </pre>
*/

@org.osgi.annotation.versioning.Version("1.0")
@org.osgi.annotation.versioning.Version("1.1")
@org.osgi.annotation.versioning.ProviderType
package org.eclipse.microprofile.openapi.models.examples;
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
* </pre>
*/

@org.osgi.annotation.versioning.Version("1.0")
@org.osgi.annotation.versioning.Version("1.1")
@org.osgi.annotation.versioning.ProviderType
package org.eclipse.microprofile.openapi.models.headers;
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@
* </pre>
*/

@org.osgi.annotation.versioning.Version("1.1")
@org.osgi.annotation.versioning.Version("1.2")
@org.osgi.annotation.versioning.ProviderType
package org.eclipse.microprofile.openapi.models.info;
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
* </pre>
*/

@org.osgi.annotation.versioning.Version("1.0")
@org.osgi.annotation.versioning.Version("1.1")
@org.osgi.annotation.versioning.ProviderType
package org.eclipse.microprofile.openapi.models.links;
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
* </pre>
*/

@org.osgi.annotation.versioning.Version("3.0")
@org.osgi.annotation.versioning.Version("3.1")
@org.osgi.annotation.versioning.ProviderType
package org.eclipse.microprofile.openapi.models.media;
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@
* </pre>
*/

@org.osgi.annotation.versioning.Version("2.1")
@org.osgi.annotation.versioning.Version("2.2")
@org.osgi.annotation.versioning.ProviderType
package org.eclipse.microprofile.openapi.models;
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
* </pre>
*/

@org.osgi.annotation.versioning.Version("1.0")
@org.osgi.annotation.versioning.Version("1.1")
@org.osgi.annotation.versioning.ProviderType
package org.eclipse.microprofile.openapi.models.parameters;
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@
* </pre>
*/

@org.osgi.annotation.versioning.Version("2.0")
@org.osgi.annotation.versioning.Version("2.1")
@org.osgi.annotation.versioning.ProviderType
package org.eclipse.microprofile.openapi.models.responses;
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
* </pre>
*/

@org.osgi.annotation.versioning.Version("2.1")
@org.osgi.annotation.versioning.Version("2.2")
@org.osgi.annotation.versioning.ProviderType
package org.eclipse.microprofile.openapi.models.security;
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
* </pre>
*/

@org.osgi.annotation.versioning.Version("2.0")
@org.osgi.annotation.versioning.Version("2.1")
@org.osgi.annotation.versioning.ProviderType
package org.eclipse.microprofile.openapi.models.servers;
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
* </pre>
*/

@org.osgi.annotation.versioning.Version("1.0")
@org.osgi.annotation.versioning.Version("1.1")
@org.osgi.annotation.versioning.ProviderType
package org.eclipse.microprofile.openapi.models.tags;
Original file line number Diff line number Diff line change
Expand Up @@ -1781,16 +1781,28 @@ private void processExtensible(Extensible<?> e) {
assertEquals(map.size(), 2, "The extensions map is expected to contain two entries.");
assertTrue(map.containsKey(extensionName1),
"The extensions map is expected to contain the key: " + extensionName1);
assertTrue(e.hasExtension(extensionName1),
"hasExtension is expected to return true for the key: " + extensionName1);
assertTrue(map.containsKey(extensionName2),
"The extensions map is expected to contain the key: " + extensionName2);
assertTrue(e.hasExtension(extensionName2),
"hasExtension is expected to return true for the key: " + extensionName2);
assertSame(map.get(extensionName1), obj1,
"The value associated with the key: " + extensionName1
+ " is expected to be the same one that was added.");
assertSame(e.getExtension(extensionName1), obj1,
"getExtension should return the same instance added for the key: " + extensionName1);
assertSame(map.get(extensionName2), obj2,
"The value associated with the key: " + extensionName2
+ " is expected to be the same one that was added.");
assertSame(e.getExtension(extensionName2), obj2,
"getExtension should return the same instance added for the key: " + extensionName2);
e.removeExtension(extensionName1);
assertEquals(e.getExtensions().size(), 1, "The extensions map is expected to contain one entry.");
assertFalse(e.hasExtension(extensionName1),
"hasExtension is expected to return false for removed key: " + extensionName1);
assertNull(e.getExtension(extensionName1),
"getExtension is expected to return null for removed key: " + extensionName1);
// Check that the extension map can be replaced with the setter and that it is returned by the getter.
final Map<String, Object> newMap = new HashMap<>();
e.setExtensions(newMap);
Expand Down

0 comments on commit e80e76c

Please sign in to comment.