diff --git a/CHANGELOG.md b/CHANGELOG.md index 3994bec7..23f8c0a3 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 a31c1314..d6d450c4 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 @@ private Supplier returnOverlapOfStringsOrStringArrays(List n // 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; } 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 96e11daf..1cb3d4a0 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 @@ static Stream parametersForTestAllOfCleanUp() { + "\"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());