diff --git a/jsonb-generator/src/main/java/io/avaje/jsonb/generator/ClassReader.java b/jsonb-generator/src/main/java/io/avaje/jsonb/generator/ClassReader.java index 9d279f42..d3e6e278 100644 --- a/jsonb-generator/src/main/java/io/avaje/jsonb/generator/ClassReader.java +++ b/jsonb-generator/src/main/java/io/avaje/jsonb/generator/ClassReader.java @@ -70,7 +70,6 @@ final class ClassReader implements BeanReader { subTypes.stream().map(TypeSubTypeMeta::type).forEach(importTypes::add); final var userTypeField = allFields.stream().filter(f -> f.propertyName().equals(typePropertyKey())).findAny(); - this.usesTypeProperty = userTypeField.isPresent(); this.useEnum = userTypeField @@ -222,7 +221,6 @@ public void cascadeTypes(Set types) { @Override public void writeFields(Append writer) { writer.append(" // naming convention %s", namingConvention).eol(); - for (final FieldReader allField : allFields) { allField.writeDebug(writer); } @@ -264,9 +262,9 @@ public void writeConstructor(Append writer) { if (allField.include() && !allField.isRaw() && uniqueTypes.add(allField.adapterShortType())) { if (hasSubTypes) { final var isCommonDiffType = - allFields.stream() - .filter(s -> s.fieldName().equals(allField.fieldName())) - .anyMatch(f -> !allField.adapterShortType().equals(f.adapterShortType())); + allFields.stream() + .filter(s -> s.fieldName().equals(allField.fieldName())) + .anyMatch(f -> !allField.adapterShortType().equals(f.adapterShortType())); isCommonFieldMap.put(allField.fieldName(), isCommonDiffType); } allField.writeConstructor(writer); @@ -472,23 +470,32 @@ private void writeFromJsonImplementation(Append writer, String varName) { private void writeJsonBuildResult(Append writer, String varName) { writer.append(" // build and return %s", shortName).eol(); - if (constructor == null) { - writer.append(" %s _$%s = new %s(", shortName, varName, shortName); - } else { - writer.append(" %s _$%s = " + constructor.creationString(), shortName, varName); - final List params = constructor.getParams(); - for (int i = 0, size = params.size(); i < size; i++) { - if (i > 0) { - writer.append(", "); - } - final var name = params.get(i).name(); - // append increasing numbers to constructor params sharing names with other subtypes - final var frequency = frequencyMap.compute(name, (k, v) -> v == null ? 0 : v + 1); - // assuming name matches field here? - writer.append(constructorParamName(name + (frequency == 0 ? "" : frequency.toString()))); + if (constructor == null) { + writer.append(" %s _$%s = new %s(", shortName, varName, shortName); + } else { + writer.append(" %s _$%s = " + constructor.creationString(), shortName, varName); + final List params = constructor.getParams(); + for (int i = 0, size = params.size(); i < size; i++) { + if (i > 0) { + writer.append(", "); } + + final var paramName = params.get(i).name(); + var name = + allFields.stream() + .filter(FieldReader::isConstructorParam) + .filter(f -> f.propertyName().equals(paramName) || f.fieldName().equals(paramName)) + .map(FieldReader::fieldName) + .findFirst() + .orElse(paramName); + + // append increasing numbers to constructor params sharing names with other subtypes + final var frequency = frequencyMap.compute(name, (k, v) -> v == null ? 0 : v + 1); + // assuming name matches field here? + writer.append(constructorParamName(name + (frequency == 0 ? "" : frequency.toString()))); } - writer.append(");").eol(); + } + writer.append(");").eol(); for (final FieldReader allField : allFields) { if (allField.includeFromJson()) { frequencyMap.compute(allField.fieldName(), (k, v) -> v == null ? 0 : v + 1); @@ -574,23 +581,23 @@ private void writeFromJsonSwitch(Append writer, boolean defaultConstructor, Stri final var isCommonFieldDiffType = isCommonFieldMap.get(name); if (isCommonFieldDiffType == null || !isCommonFieldDiffType) { allField.writeFromJsonSwitch( - writer, - defaultConstructor, - varName, - caseInsensitiveKeys, - allFields.stream() - .filter(x -> x.fieldName().equals(name)) - .flatMap(f -> f.aliases().stream()) - .collect(toList())); + writer, + defaultConstructor, + varName, + caseInsensitiveKeys, + allFields.stream() + .filter(x -> x.fieldName().equals(name)) + .flatMap(f -> f.aliases().stream()) + .collect(toList())); } else { // if subclass shares a field name with another subclass // write a special case statement writeSubTypeCase( - name, - writer, - allFields.stream().filter(x -> x.fieldName().equals(name)).collect(toList()), - defaultConstructor, - varName); + name, + writer, + allFields.stream().filter(x -> x.fieldName().equals(name)).collect(toList()), + defaultConstructor, + varName); } } else @@ -613,8 +620,7 @@ private void writeFromJsonSwitch(Append writer, boolean defaultConstructor, Stri private void writeSubTypeCase(String name, Append writer, List commonFields, boolean defaultConstructor, String varName) { writer.append(" case \"%s\":", name).eol(); // get all possible aliases of this field from the subtypes - for (final String alias : - commonFields.stream().map(FieldReader::aliases).findFirst().orElseGet(List::of)) { + for (final String alias : commonFields.stream().map(FieldReader::aliases).findFirst().orElseGet(List::of)) { final String propertyKey = caseInsensitiveKeys ? alias.toLowerCase() : alias; writer.append(" case \"%s\":", propertyKey).eol(); } diff --git a/jsonb-generator/src/main/java/io/avaje/jsonb/generator/FieldReader.java b/jsonb-generator/src/main/java/io/avaje/jsonb/generator/FieldReader.java index 356e061b..4661687f 100644 --- a/jsonb-generator/src/main/java/io/avaje/jsonb/generator/FieldReader.java +++ b/jsonb-generator/src/main/java/io/avaje/jsonb/generator/FieldReader.java @@ -68,6 +68,7 @@ final class FieldReader { void readParam(VariableElement element) { this.deserialize = true; this.isCreatorParam = true; + property.setConstructorParam(); initAliases(element); } diff --git a/jsonb-generator/src/main/java/io/avaje/jsonb/generator/TypeReader.java b/jsonb-generator/src/main/java/io/avaje/jsonb/generator/TypeReader.java index 33d45818..fbe58960 100644 --- a/jsonb-generator/src/main/java/io/avaje/jsonb/generator/TypeReader.java +++ b/jsonb-generator/src/main/java/io/avaje/jsonb/generator/TypeReader.java @@ -293,7 +293,7 @@ private boolean checkMethod2(ExecutableElement methodElement) { private void matchFieldsToSetterOrConstructor() { for (FieldReader field : allFields) { - if (field.includeFromJson()) { + if (field.includeFromJson() && !field.isConstructorParam()) { if (constructorParamMap.get(field.fieldName()) != null) { field.setConstructorParam(); } else { diff --git a/jsonb-generator/src/test/java/io/avaje/jsonb/generator/models/valid/PropertyCreator.java b/jsonb-generator/src/test/java/io/avaje/jsonb/generator/models/valid/PropertyCreator.java index faca4109..8aaabb50 100644 --- a/jsonb-generator/src/test/java/io/avaje/jsonb/generator/models/valid/PropertyCreator.java +++ b/jsonb-generator/src/test/java/io/avaje/jsonb/generator/models/valid/PropertyCreator.java @@ -36,14 +36,14 @@ public final class PropertyCreator { @Json.Creator public PropertyCreator( - String identifier, + String uid, Instant startup, Properties configuration, Set instances) { this.arguments = null; this.hooks = null; - this.identifier = identifier; + this.identifier = uid; this.startup = startup; this.configuration = configuration; this.instances = null;