Skip to content

Extra underscores when XSD elements use upper underscore #667

Description

@wardev

With 1.97 I have a <xsd:element name="COMMENT" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/> that gets turned into repeated string c_o_m_m_e_n_t, but I would expect repeated string comment.

Looks like the current code assumes element names are in camel case. Here is a patch to use getCaseFormatName(...) to figure out which case convention is used. I changed \w+ to \w* so that names like abc3 are treated as camel case.

diff --git a/schema2proto-lib/src/main/java/no/entur/schema2proto/generateproto/ProtoSerializer.java b/schema2proto-lib/src/main/java/no/entur/schema2proto/generateproto/ProtoSerializer.java
index 2b5cc93..1272253 100644
--- a/schema2proto-lib/src/main/java/no/entur/schema2proto/generateproto/ProtoSerializer.java
+++ b/schema2proto-lib/src/main/java/no/entur/schema2proto/generateproto/ProtoSerializer.java
@@ -565,13 +565,13 @@ public class ProtoSerializer {
                        }
                } else {
                        if (Character.isLowerCase(s.charAt(0))) {
-                               if (s.matches("([a-z]+[A-Z0-9]+\\w+)+")) {
+                               if (s.matches("([a-z]+[A-Z0-9]+\\w*)+")) {
                                        return CaseFormat.LOWER_CAMEL;
                                } else if (s.matches("[a-z]+")) {
                                        return CaseFormat.LOWER_UNDERSCORE;
                                }
                        } else {
-                               if (s.matches("([A-Z]+[a-z0-9]+\\w+)+")) {
+                               if (s.matches("([A-Z]+[a-z0-9]+\\w*)+")) {
                                        return CaseFormat.UPPER_CAMEL;
                                } else if (s.matches("[A-Z]+")) {
                                        return CaseFormat.UPPER_UNDERSCORE;
@@ -1158,7 +1158,7 @@ public class ProtoSerializer {
 
                        String strippedFieldName = StringUtils.removeEnd(StringUtils.removeStart(fieldName, UNDERSCORE), UNDERSCORE);
 
-                       String newFieldName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, strippedFieldName);
+                       String newFieldName = getCaseFormatName(strippedFieldName).to(CaseFormat.LOWER_UNDERSCORE, strippedFieldName);
 
                        // Remove all dashes
                        newFieldName = StringUtils.remove(newFieldName, DASH);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions