Skip to content

Commit 5895014

Browse files
committed
new helper to reduce duplication
1 parent 5edec36 commit 5895014

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

  • json-schema-validator/src
    • main/java/io/openapiprocessor/jsonschema/support
    • test/kotlin/io/openapiprocessor/jsonschema/support

json-schema-validator/src/main/java/io/openapiprocessor/jsonschema/support/Types.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ public static Collection<Object> convertCollection (String path, @Nullable Objec
6565
return (Map<String, @Nullable Object>) o;
6666
}
6767

68+
/**
69+
* Returns {@code o} as {@code @Nullable Map<String, @Nullable Object>} or null if it not a map.
70+
* @param o the potential map
71+
* @return the map or null if {@code o} is not a map
72+
*/
73+
public static @Nullable Map<String, @Nullable Object> asObjectOrNull(@PolyNull Object o) {
74+
if (!isObject(o))
75+
return null;
76+
77+
return asObject(o);
78+
}
79+
6880
@SuppressWarnings ("unchecked")
6981
public static <T> @PolyNull Collection<@Nullable T> asCol (@PolyNull Object o) {
7082
return (Collection<@Nullable T>) o;

json-schema-validator/src/test/kotlin/io/openapiprocessor/jsonschema/support/TypesSpec.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,10 @@ class TypesSpec: StringSpec({
3131
"casts object to object" {
3232
asObject(emptyMap<String, String>() as Any).shouldNotBeNull()
3333
}
34+
35+
"check and cast to object" {
36+
asObjectOrNull(null).shouldBeNull()
37+
asObjectOrNull(emptyList<Any>()).shouldBeNull()
38+
asObjectOrNull(emptyMap<String, Any>()).shouldNotBeNull()
39+
}
3440
})

0 commit comments

Comments
 (0)