Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Creator Generation Error #261

Merged
merged 3 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -222,7 +221,6 @@ public void cascadeTypes(Set<String> types) {
@Override
public void writeFields(Append writer) {
writer.append(" // naming convention %s", namingConvention).eol();

for (final FieldReader allField : allFields) {
allField.writeDebug(writer);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<MethodReader.MethodParam> 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<MethodReader.MethodParam> 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);
Expand Down Expand Up @@ -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
Expand All @@ -613,8 +620,7 @@ private void writeFromJsonSwitch(Append writer, boolean defaultConstructor, Stri
private void writeSubTypeCase(String name, Append writer, List<FieldReader> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ final class FieldReader {
void readParam(VariableElement element) {
this.deserialize = true;
this.isCreatorParam = true;
property.setConstructorParam();
initAliases(element);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public final class PropertyCreator {

@Json.Creator
public PropertyCreator(
String identifier,
String uid,
Instant startup,
Properties configuration,
Set<PropertyCreator> instances) {
this.arguments = null;
this.hooks = null;

this.identifier = identifier;
this.identifier = uid;
this.startup = startup;
this.configuration = configuration;
this.instances = null;
Expand Down
Loading