From ffee01fafe347d5ca5e7efb5cbcaaf002580b300 Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Thu, 3 Jul 2025 15:39:40 +0200 Subject: [PATCH] fix: support non-conforming getter names with the JacksonModule --- .../jsonschema/module/jackson/JacksonModule.java | 13 ++++++++++++- .../module/jackson/IgnorePropertyTest.java | 14 +++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JacksonModule.java b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JacksonModule.java index 2a8a2c95..7db77492 100644 --- a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JacksonModule.java +++ b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JacksonModule.java @@ -303,7 +303,18 @@ protected boolean shouldIgnoreField(FieldScope field) { // other kinds of field ignorals are handled implicitly, i.e. are only available by way of being absent return beanDescription.findProperties().stream() .noneMatch(propertyDefinition -> declaredName.equals(propertyDefinition.getInternalName()) - || fieldName.equals(propertyDefinition.getInternalName())); + || fieldName.equals(propertyDefinition.getInternalName()) + || fieldName.equals(toNonJavaBeanConformingName(propertyDefinition.getInternalName()))); + } + + private String toNonJavaBeanConformingName(String fieldName) { + // a field name like "xIndex" will have a propertyDefinition name "xindex", but should not be ignored. + if(fieldName == null || fieldName.length() < 2) { + return fieldName; + } + return fieldName.charAt(0) + + fieldName.substring(1, 2).toUpperCase() + + fieldName.substring(2); } /** diff --git a/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/IgnorePropertyTest.java b/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/IgnorePropertyTest.java index c41c99b1..c75dbeeb 100644 --- a/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/IgnorePropertyTest.java +++ b/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/IgnorePropertyTest.java @@ -46,7 +46,8 @@ static Stream parametersForTestJsonIgnoreProperties() { return Stream.of( Arguments.of(SubType.class, "[includedChildField, includedParentField1, includedParentField2]"), Arguments.of(SuperType.class, "[ignoredParentField2, includedParentField1]"), - Arguments.of(TypeWithUnderscoreField.class, "[import]") + Arguments.of(TypeWithUnderscoreField.class, "[import]"), + Arguments.of(TypeWithNonCompatibleGetter.class, "[aIncludedField2, included1]") ); } @@ -99,4 +100,15 @@ public String getIgnoredValue() { return "method being ignored, because there is no matching field"; } } + + private static class TypeWithNonCompatibleGetter { + public String included1; + +// @JsonProperty("aIncludedField2") // This will fix the test + private int aIncludedField2; + + public int getAIncludedField2() { + return this.aIncludedField2; + } + } }