From c305d1bd6092d85828fdb27a7814f40b4ef6fa46 Mon Sep 17 00:00:00 2001 From: Carsten Wickner Date: Wed, 10 Jan 2024 00:03:51 +0100 Subject: [PATCH] feat: Introduce Option for duplicate member attribute removal --- CHANGELOG.md | 7 +++- .../victools/jsonschema/generator/Option.java | 7 ++++ .../jsonschema/generator/OptionPreset.java | 9 +++-- .../jsonschema/generator/SchemaBuilder.java | 4 +- .../generator/SchemaGeneratorConfig.java | 10 +++++ .../generator/impl/SchemaCleanUpUtils.java | 16 +++++++- .../impl/SchemaGeneratorConfigImpl.java | 5 +++ .../jsonschema/generator/impl/Util.java | 18 +++++++++ .../SchemaGeneratorMemberCleanUpTest.java | 37 +++++++++++++++---- 9 files changed, 100 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d7fd1732..90376078 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,12 @@ 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` +#### Added +- new `Option.DUPLICATE_MEMBER_ATTRIBUTE_CLEANUP_AT_THE_END` discard duplicate elements from member sub-schemas + +#### Changed +- new `Option.DUPLICATE_MEMBER_ATTRIBUTE_CLEANUP_AT_THE_END` by default included in standard `OptionPreset`s ## [4.33.1] - 2023-12-19 ### `jsonschema-module-jackson` diff --git a/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/Option.java b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/Option.java index d154891e..6ccbf912 100644 --- a/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/Option.java +++ b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/Option.java @@ -322,6 +322,13 @@ public enum Option { * @since 4.6.0 */ ALLOF_CLEANUP_AT_THE_END(null, null), + /** + * Whether at the end of the schema generation, all member sub-schemas referencing a common definition should be checked for any duplicated + * attributes, which should be removed from the inline member sub-schemas in favor of the equivalent in the single common definition. + * + * @since 4.34.0 + */ + DUPLICATE_MEMBER_ATTRIBUTE_CLEANUP_AT_THE_END(null, null), /** * Whether at the end of the schema generation, all sub-schemas without an explicit "type" indication should be augmented by the implied "type" * based on the other tags in the respective schema. diff --git a/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/OptionPreset.java b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/OptionPreset.java index 7d8634ca..ad60275c 100644 --- a/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/OptionPreset.java +++ b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/OptionPreset.java @@ -46,7 +46,8 @@ public class OptionPreset { Option.DEFINITIONS_FOR_ALL_OBJECTS, Option.NULLABLE_FIELDS_BY_DEFAULT, Option.NULLABLE_METHOD_RETURN_VALUES_BY_DEFAULT, - Option.ALLOF_CLEANUP_AT_THE_END + Option.ALLOF_CLEANUP_AT_THE_END, + Option.DUPLICATE_MEMBER_ATTRIBUTE_CLEANUP_AT_THE_END ); /** @@ -63,7 +64,8 @@ public class OptionPreset { Option.PUBLIC_NONSTATIC_FIELDS, Option.NONPUBLIC_NONSTATIC_FIELDS_WITH_GETTERS, Option.NONPUBLIC_NONSTATIC_FIELDS_WITHOUT_GETTERS, - Option.ALLOF_CLEANUP_AT_THE_END + Option.ALLOF_CLEANUP_AT_THE_END, + Option.DUPLICATE_MEMBER_ATTRIBUTE_CLEANUP_AT_THE_END ); /** @@ -79,7 +81,8 @@ public class OptionPreset { Option.NONSTATIC_NONVOID_NONGETTER_METHODS, Option.SIMPLIFIED_ENUMS, Option.SIMPLIFIED_OPTIONALS, - Option.ALLOF_CLEANUP_AT_THE_END + Option.ALLOF_CLEANUP_AT_THE_END, + Option.DUPLICATE_MEMBER_ATTRIBUTE_CLEANUP_AT_THE_END ); private final Set