Skip to content

[Bug] Column include/exclude drops or keeps the wrong columns for field names like xRealIp #1129

Description

@CodeMan-cmd

Search before asking

  • I searched in the issues and found nothing similar.

Fesod version

2.1.0-incubating (main branch, commit 4e3f3aa)

JDK version

Temurin 1.8.0_472

Operating system

Windows 10 (amd64)

Steps To Reproduce

Field names that start with a lower case letter followed by an upper case letter, for example xRealIp or pName, are common in generated DTOs (a X_REAL_IP column converted to camel case).

Data class:

public class CamelCaseFieldData {
    @ExcelProperty("name")
    private String name;
    @ExcelProperty("xRealIp")
    private String xRealIp;
    @ExcelProperty("pName")
    private String pName;
}

Test code:

File file = new File("camel.xlsx");
FesodSheet.write(file, CamelCaseFieldData.class)
        .includeColumnFieldNames(Arrays.asList("name", "xRealIp", "pName"))
        .sheet()
        .doWrite(data());
List<Map<Integer, String>> dataMap = FesodSheet.read(file).sheet().doReadSync();
// only one column is written, the others are silently dropped

A test method with this content is added by the PR that fixes it: it fails on the current main branch and passes with the fix.

Current Behavior

includeColumnFieldNames(Arrays.asList("name", "xRealIp", "pName")) writes 1 column instead of 3, the xRealIp and pName columns are dropped from the file. excludeColumnFieldNames(Arrays.asList("xRealIp")) keeps the xRealIp column.

Cause: ClassUtils.doDeclaredFields filters with writeHolder.ignore(field.getFieldName(), ...), and FieldWrapper getFieldName is the name kept by the cglib bean map of the field (see FieldUtils resolveCglibFieldName), which is XRealIp for the field xRealIp. AbstractWriteHolder ignore compares that name with the field names configured by the user, so the comparison only works when the cglib name equals the Java field name, which is the case for names like column1 or name, but not for names like xRealIp or pName.

ClassUtils.resortField has the same problem: it looks the order of a column up by field.getFieldName() and assigns a position to every configured name, so an unknown name leaves a hole and shifts the following columns when orderByIncludeColumn(true) is used.

Expected Behavior

includeColumnFieldNames(Arrays.asList("name", "xRealIp", "pName")) writes all three columns, excludeColumnFieldNames(Arrays.asList("xRealIp")) writes name and pName only.

Filtering by field name must use the Java field name, which is the name the user configures and the name used when reading the file back, and an unknown name in the include list must not hold a column position.

Anything else?

Related EasyExcel issue, the same code exists there: alibaba/easyexcel#4110

I have a fix ready in a fork, it also adds unit tests: https://github.com/CodeMan-cmd/fesod/tree/fix-include-exclude-camel-case-field-name (PR follows).

Are you willing to submit a PR?

  • I'm willing to submit a PR!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions