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

Handle naming conflicts from internally defined subclass #116

Open
mdechdee opened this issue Nov 6, 2023 · 3 comments
Open

Handle naming conflicts from internally defined subclass #116

mdechdee opened this issue Nov 6, 2023 · 3 comments

Comments

@mdechdee
Copy link

mdechdee commented Nov 6, 2023

I have a use case here where kotlin have internal subclass

@Serializable
data class A(val c: C){
    enum class C{
        one
    }
}

@Serializable
data class B(val c: C){
    enum class C {
        two
    }
}

@Serializable
data class Test(
    val a: A,
    val b: B
)

the generated ts definition of class Test is

export interface Test {
  a: A;
  b: B;
}

export interface A {
  c: C;
}

export interface B {
  c: C;
}

export enum C {
  one = "one",
}

export enum C {
  two = "two",
}

which has some naming conflicts on enum C.
Do we have a way to handle this? I see some unimplemented namespace, could it be used here?

@aSemy
Copy link
Contributor

aSemy commented Nov 6, 2023

Thanks for the report! It's an interesting case, I'll have a think about it.

The generator could wrap each nested C class in a separate TS namespace, but that could make other generated code more verbose.

You could specify a distinct @SerialName for each C class, but that's manual and repetitive.

I've tried to make the generator as open and flexible as possible, so please try experimenting with overriding the default generator, and if you find something that works then please share it here.

@aSemy
Copy link
Contributor

aSemy commented Jan 7, 2024

Hey @mdechdee, I've had a play around and I think I've finally got the namespace grouping working. I had implemented the skeleton for it, but never finished it. I was just playing around and I realised that it can be used to solve your problem.

It's been a while since I've used TypeScript though - can you verify that this output would be suitable for you?

export interface Test {
  a: A;
  b: B;
}

export interface A {
  content: Content;
}

export interface B {
  content: Content;
}

export namespace A {
  export interface Content {
  }
}

export namespace B {
  export interface Content {
  }
}

I've added an option so the namespaces can be controlled dynamically, so this could be fine tuned if desired.

@mdechdee
Copy link
Author

mdechdee commented Jan 11, 2024

@aSemy sorry for late response. I think the interface and namespace should not have naming conflict.
Should be something like this

export interface Test {
    a: A;
    b: B;
}

export interface A {
    content: ANamespace.Content;
}

export interface B {
    content: BNamespace.Content;
}

export namespace ANamespace {
    export interface Content {};
}

export namespace BNamespace {
    export interface Content {};
}

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

2 participants