From ac5ea4e82ba43d4b5313a8d4a84fe0cfd2b4408b Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Tue, 18 May 2021 13:27:03 +0200 Subject: [PATCH] Improve BytecodeRecorderImpl error message When getter type and setter type do not match, provide a better error message with as much information as possible. --- .../recording/BytecodeRecorderImpl.java | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/core/deployment/src/main/java/io/quarkus/deployment/recording/BytecodeRecorderImpl.java b/core/deployment/src/main/java/io/quarkus/deployment/recording/BytecodeRecorderImpl.java index 6d2107a8c2978..713313a520397 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/recording/BytecodeRecorderImpl.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/recording/BytecodeRecorderImpl.java @@ -1311,26 +1311,33 @@ public void prepare(MethodContext context) { continue; } Class propertyType = i.getPropertyType(); - if (ctorParamIndex == null - && i.getReadMethod().getReturnType() != i.getWriteMethod().getParameterTypes()[0]) { - if (relaxedValidation) { - //this is a weird situation where the reader and writer are different types - //we iterate and try and find a valid setter method for the type we have - //OpenAPI does some weird stuff like this - - for (Method m : param.getClass().getMethods()) { - if (m.getName().equals(i.getWriteMethod().getName())) { - if (m.getParameterTypes().length > 0 - && m.getParameterTypes()[0].isAssignableFrom(param.getClass())) { - propertyType = m.getParameterTypes()[0]; - break; + if (ctorParamIndex == null) { + Class getterReturnType = i.getReadMethod().getReturnType(); + Class setterParameterType = i.getWriteMethod().getParameterTypes()[0]; + if (getterReturnType != setterParameterType) { + if (relaxedValidation) { + //this is a weird situation where the reader and writer are different types + //we iterate and try and find a valid setter method for the type we have + //OpenAPI does some weird stuff like this + + for (Method m : param.getClass().getMethods()) { + if (m.getName().equals(i.getWriteMethod().getName())) { + if (m.getParameterTypes().length > 0 + && m.getParameterTypes()[0].isAssignableFrom(param.getClass())) { + propertyType = m.getParameterTypes()[0]; + break; + } } - } + } + } else { + throw new RuntimeException("Cannot serialise field '" + i.getName() + "' on object '" + param + + "' of type '" + param.getClass().getName() + + "' as getter and setter are of different types. Getter type is '" + + getterReturnType.getName() + "' while setter type is '" + + setterParameterType.getName() + + "'."); } - } else { - throw new RuntimeException("Cannot serialise field " + i.getName() + " on object " + param - + " as setter and getters were different types"); } } DeferredParameter val = loadObjectInstance(propertyValue, existing,