typescript-generator icon indicating copy to clipboard operation
typescript-generator copied to clipboard

Duplicated fields when java name is uppercase

Open baffelli opened this issue 1 year ago • 1 comments

I observed that if I have a Java field starting with an uppercase letter (I know it is against Java conventions, but it is legal syntax nonetheless), the generated Typescript code contains duplicate fields; e.g. the following Java code:

class MyClass{
  string A
}

will give the following interface

interface MyClass{
  A: string
  a: string
}

You can test this with the following:



import ch.systemsx.cisd.base.annotation.JsonObject;
import cz.habarta.typescript.generator.*;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.util.List;

public class MethodExtensionTest {

    interface GenericTestClass<A> {
        A get();
    }


    class TestClass {
        public boolean A;
        public String[] B;
        public boolean getA() {return false;}
        public String[] getB() {return null;}
    }

    class NestedTestClass {
        public TestClass A;
        public boolean getA() {return false;}
    }


    final Settings settings = new Settings();
    @BeforeTest
    public void setSettings(){
        settings.outputKind = (TypeScriptOutputKind.module);
        settings.jsonLibrary = JsonLibrary.jackson2;

    }



    @Test
    public void testGenericClass() {
        final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(GenericTestClass.class));
        System.out.println(output);

    }

    @Test
    public void testNormalClass(){
        final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(TestClass.class));
        System.out.println(output);
    }

    @Test
    public void testNestedClass(){
        final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(NestedTestClass.class));
        System.out.println(output);
    }
}

The output of testNormalClass will contain both uppercase and lowercase fields "A", "a" with the same type.

baffelli avatar Aug 24 '23 11:08 baffelli

Not a bug. All the public members are included into JSON serialisation. If you don't want some of them - they should be annotated with @JsonIgnore or similar annotation.

panchenko avatar Feb 16 '24 11:02 panchenko