Repository: victools/jsonschema-generator (java)

Issue:
bug in SchemaCleanUpUtils::returnOverlapOfStringsOrStringArrays
In version 4.31.0 there's [this call](https://github.com/victools/jsonschema-generator/blob/f93681b98b8029410133fc521842cfe54860955f/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/impl/SchemaCleanUpUtils.java#L389).
This requires `encounteredValues` to be mutable, but it's not due to [the use of `Collections.singletonList`](https://github.com/victools/jsonschema-generator/blob/f93681b98b8029410133fc521842cfe54860955f/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/impl/SchemaCleanUpUtils.java#L412).

As a result we see this exception:

```
java.lang.UnsupportedOperationException: null
    at java.util.Collections$1.remove(Collections.java:4787) ~[?:?]
    at java.util.AbstractCollection.retainAll(AbstractCollection.java:404) ~[?:?]
    at com.github.victools.jsonschema.generator.impl.SchemaCleanUpUtils.returnOverlapOfStringsOrStringArrays(SchemaCleanUpUtils.java:389) ~[jsonschema-generator-4.31.0.jar:?]
```

Pull request:
fix: error during allOf clean-up for invalid schema
This fixes #348.

However, that doesn't resolve the fact that when you reach the affected point in the code, your generated schema may contain an error due to unresolvable type definitions (e.g., at the same time `type: object` and `type: string`).

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3994bec..23f8c0a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
 ## [Unreleased]
+### `jsonschema-generator`
+#### Fixed
+- avoid error being thrown in `allOf` clean-up for invalid payload
 
 ## [4.31.0] - 2023-04-22
 ### `jsonschema-generator`
diff --git a/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/impl/SchemaCleanUpUtils.java b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/impl/SchemaCleanUpUtils.java
index a31c131..d6d450c 100644
--- a/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/impl/SchemaCleanUpUtils.java
+++ b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/impl/SchemaCleanUpUtils.java
@@ -386,8 +386,13 @@ public class SchemaCleanUpUtils {
                 // invalid node; abort merge
                 return null;
             }
-            encounteredValues.retainAll(nextValues);
-            if (encounteredValues.isEmpty()) {
+            if (encounteredValues.size() > 1) {
+                encounteredValues.retainAll(nextValues);
+                if (encounteredValues.isEmpty()) {
+                    // invalid merge result: no valid value remained; abort merge
+                    return null;
+                }
+            } else if (!nextValues.contains(encounteredValues.get(0))) {
                 // invalid merge result: no valid value remained; abort merge
                 return null;
             }


Test patch:
diff --git a/jsonschema-generator/src/test/java/com/github/victools/jsonschema/generator/SchemaGeneratorAllOfCleanUpTest.java b/jsonschema-generator/src/test/java/com/github/victools/jsonschema/generator/SchemaGeneratorAllOfCleanUpTest.java
index 96e11da..1cb3d4a 100644
--- a/jsonschema-generator/src/test/java/com/github/victools/jsonschema/generator/SchemaGeneratorAllOfCleanUpTest.java
+++ b/jsonschema-generator/src/test/java/com/github/victools/jsonschema/generator/SchemaGeneratorAllOfCleanUpTest.java
@@ -52,8 +52,23 @@ public class SchemaGeneratorAllOfCleanUpTest {
                                 + "\"allOf\": [{ \"title\":\"same in all three\" }, { \"title\":\"same in all three\" }] }",
                         "{ \"type\": \"object\", \"title\":\"same in all three\" }"),
                     Arguments.of(schemaVersion,
-                        "{ \"type\": \"object\",\"allOf\": [{ \"title\":\"from allOf[0]\" }, { \"description\":\"from allOf[1]\" }] }",
-                        "{ \"type\": \"object\", \"title\":\"from allOf[0]\", \"description\":\"from allOf[1]\" }")
+                        "{ \"type\": \"object\", \"allOf\": [{ \"title\":\"from allOf[0]\" }, { \"description\":\"from allOf[1]\" }] }",
+                        "{ \"type\": \"object\", \"title\":\"from allOf[0]\", \"description\":\"from allOf[1]\" }"),
+                    Arguments.of(schemaVersion,
+                        "{ \"type\": \"object\", \"allOf\": [{ \"type\": [\"object\",\"null\"] }] }",
+                        "{ \"type\": \"object\" }"),
+                    Arguments.of(schemaVersion,
+                        "{ \"type\": [\"object\",\"null\"], \"allOf\": [{ \"type\": \"object\" }] }",
+                        "{ \"type\": \"object\" }"),
+                    Arguments.of(schemaVersion,
+                        "{ \"type\": \"object\", \"allOf\": [{ \"type\": \"null\" }] }",
+                        "{ \"type\": \"object\", \"allOf\": [{ \"type\": \"null\" }] }"),
+                    Arguments.of(schemaVersion,
+                        "{ \"type\": \"object\", \"allOf\": [{ \"type\": [\"string\",\"null\"] }] }",
+                        "{ \"type\": \"object\", \"allOf\": [{ \"type\": [\"string\",\"null\"] }] }"),
+                    Arguments.of(schemaVersion,
+                        "{ \"type\": [\"object\",\"null\"], \"allOf\": [{ \"type\": \"string\" }] }",
+                        "{ \"type\": [\"object\",\"null\"], \"allOf\": [{ \"type\": \"string\" }] }")
                 ))
                 .collect(Collectors.toList());