Skip to content

Fix issue in ComplexTypeWriter #1042

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

Merged
merged 2 commits into from
Jun 20, 2017
Merged
Changes from 1 commit
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
23 changes: 13 additions & 10 deletions core/src/main/java/cucumber/runtime/xstream/ComplexTypeWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import cucumber.runtime.table.CamelCaseStringConverter;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static java.util.Arrays.asList;

public class ComplexTypeWriter extends CellWriter {
private final List<String> columnNames;
private final List<String> fieldNames = new ArrayList<String>();
private final List<String> fieldValues = new ArrayList<String>();
private Map<String, String> fields = new LinkedHashMap<String, String>();
private String currentKey;

private int nodeDepth = 0;

Expand All @@ -20,7 +22,7 @@ public ComplexTypeWriter(List<String> columnNames) {

@Override
public List<String> getHeader() {
return columnNames.isEmpty() ? fieldNames : columnNames;
return columnNames.isEmpty() ? new ArrayList<String>(fields.keySet()) : columnNames;
}

@Override
Expand All @@ -30,24 +32,24 @@ public List<String> getValues() {
String[] explicitFieldValues = new String[columnNames.size()];
int n = 0;
for (String columnName : columnNames) {
int index = fieldNames.indexOf(converter.map(columnName));
if (index == -1) {
explicitFieldValues[n] = "";
final String convertedColumnName = converter.map(columnName);
if (fields.containsKey(convertedColumnName)) {
explicitFieldValues[n] = fields.get(convertedColumnName);
} else {
explicitFieldValues[n] = fieldValues.get(index);
explicitFieldValues[n] = "";
}
n++;
}
return asList(explicitFieldValues);
} else {
return fieldValues;
return new ArrayList<String>(fields.values());
}
}

@Override
public void startNode(String name) {
if (nodeDepth == 1) {
this.fieldNames.add(name);
currentKey = name;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not going to work with nested nodes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically when the nodes add values after the nested element. E.g.: [value, [value, value], value] because the current key will be null.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I can take a look at fixing this! Since the current tests all pass I think some more are needed. Would it be possible for you to add one or more failing tests that show the behaviour? I can try adding them myself but I don't have enough knowledge of all the various use cases to support.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've send a PR on your PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, I hope to take a look when I'm back at work on Monday.

}
nodeDepth++;
}
Expand All @@ -58,12 +60,13 @@ public void addAttribute(String name, String value) {

@Override
public void setValue(String value) {
fieldValues.add(value == null ? "" : value);
fields.put(currentKey, value == null ? "" : value);
}

@Override
public void endNode() {
nodeDepth--;
currentKey = null;
}

@Override
Expand Down