From 6c07ac801d0a9dab9f66e3f741cdd50b50e3e5ea Mon Sep 17 00:00:00 2001 From: Carsten Wickner <11309681+CarstenWickner@users.noreply.github.com> Date: Wed, 10 Jan 2024 06:57:07 +0100 Subject: [PATCH] feat: Support removal of duplicate attributes from member schemas (#428) * chore: Replace deprecated method usage * 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 | 27 +++++-- .../generator/SchemaGeneratorConfig.java | 10 +++ .../generator/impl/SchemaCleanUpUtils.java | 81 +++++++++++++++++++ .../impl/SchemaGeneratorConfigImpl.java | 5 ++ .../jsonschema/generator/impl/Util.java | 18 +++++ .../generator/AbstractTypeAwareTest.java | 2 +- .../SchemaGeneratorMemberCleanUpTest.java | 73 +++++++++++++++++ .../impl/AttributeCollectorTest.java | 4 +- .../SchemaDefinitionNamingStrategyTest.java | 3 +- .../module/jackson/AbstractTypeAwareTest.java | 2 +- .../CustomEnumDefinitionProviderTest.java | 3 +- .../jsonschema/module/jackson/TestType.java | 4 +- .../module/jakarta/validation/TestType.java | 4 +- .../module/javax/validation/TestType.java | 4 +- .../jsonschema/module/swagger15/TestType.java | 4 +- 18 files changed, 246 insertions(+), 21 deletions(-) create mode 100644 jsonschema-generator/src/test/java/com/github/victools/jsonschema/generator/SchemaGeneratorMemberCleanUpTest.java 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