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

I have bug fixes for two bugs - Are pull requests still being considered? #48

Open
MarkCranness opened this issue Mar 6, 2016 · 0 comments

Comments

@MarkCranness
Copy link

This code throws exceptions when it should not:

public void Test() {
    var csvContext = new LINQtoCSV.CsvContext();
    string testInput =
        "NotNullColumn1,NotNullColumn2" + Environment.NewLine +
        "Line1NotNull1,Line1NotNull2"; // Note: All not-null columns have values
    csvContext.Read<Data>(this.StreamReaderFromString(testInput));
    testInput =
        "NotNullColumn" + Environment.NewLine +
        "Line1NotNull"; // Note: All not-null columns have values
    csvContext.Read<Data2>(this.StreamReaderFromString(testInput));
}
class Data {
    [LINQtoCSV.CsvColumn(CanBeNull = false)]
    public string NotNullColumn1 { get; set; }
    public string ExtraColumn { get; set; }
    [LINQtoCSV.CsvColumn(CanBeNull = false)]
    public string NotNullColumn2 { get; set; }
}
class Data2 {
    [LINQtoCSV.CsvColumn(FieldIndex = 1)] // Force Array.Sort() ordering to expose bug
    public string ExtraColumn { get; set; }
    [LINQtoCSV.CsvColumn(CanBeNull = false)]
    public string NotNullColumn { get; set; }
}

The first read (of class Data) throws: 'In line 2, no value provided for required field or property "Column1" in type "UserQuery+Data"'
(The exact column name it complains about depends on exactly how Array.Sort() sorts in line 224 of file FieldMapper.cs, and for some sorting may not even give an error.)

The second read (of class Data2) throws: 'In line 2, no value provided for required field or property "NotNullColumn" in type "UserQuery+Data2".'

The problem is in FieldMapper.ReadNames() where it destructively copies TypeFieldInfos into m_IndexToInfo array, without taking care to preserve the tail of that array.

Before ReadNames, m_IndexToInfo may look like (in the Data2 example):

ExtraColumn,
NotNullColumn

After ReadNames() m_IndexToInfo is:

NotNullColumn,
NotNullColumn

NotNullColumn is duplicated and ExtraColumn has been deleted.
The duplicate NotNullColumn triggers MissingRequiredFieldException at the very very end of FieldMapper.Reading().

Bug 2:

public void Test() {
    var csvContext = new LINQtoCSV.CsvContext();
    testInput =
        "ExtraColumn,Column" + Environment.NewLine +
        "Extra,Value";
    var fileDescription = new LINQtoCSV.CsvFileDescription { EnforceCsvColumnAttribute = true, IgnoreUnknownColumns = true
    }; 
    csvContext.Read<Bug2>(this.StreamReaderFromString(testInput), fileDescription).Dump();
}
class Bug2 {
    [LINQtoCSV.CsvColumn()]
    public string Column { get; set; }
}

This throws: 'Index was outside the bounds of the array.' (and NOT wrapped in AggregatedException).

The problem is line 438 of FieldMapper.ReadNames() where the field being checked is using the wrong index (it should be m_IndexToInfo[_mappingIndexes[i]].hasColumnAttribute).

The fixed lines are mostly:

protected List<TypeFieldInfo> m_IndexToInfo = null;
...
m_IndexToInfo = new List<TypeFieldInfo>(nbrTypeFields);
...
m_IndexToInfo.Sort();
...
// Re-order m_IndexToInfo to match the field names
for (int i = 0; i < row.Count; i++) {
    if (!_mappingIndexes.ContainsKey(i)) {
        continue;
    }

    TypeFieldInfo tfi = m_NameToInfo[row[i].Value];
    m_IndexToInfo.Remove(tfi);
    m_IndexToInfo.Insert(_mappingIndexes[i], tfi);

    if (m_fileDescription.EnforceCsvColumnAttribute && !tfi.hasColumnAttribute) {
        // enforcing column attr, but this field/prop has no column attr.
        throw new MissingCsvColumnAttributeException(typeof (T).ToString(), row[i].Value, m_fileName);
    }
}

I can create unit tests for these and create a new pull request, are pull requests still being considered?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant