Skip to content

Namespace for sealed class generated twice #110

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

Closed
kunyavskiy opened this issue Sep 15, 2023 · 1 comment · Fixed by #111
Closed

Namespace for sealed class generated twice #110

kunyavskiy opened this issue Sep 15, 2023 · 1 comment · Fixed by #111
Labels
bug Something isn't working

Comments

@kunyavskiy
Copy link

kunyavskiy commented Sep 15, 2023

Let's look on code

@Serializable
sealed class S {
    @Serializable
    class A : S()
    @Serializable
    class B: S()
}

@Serializable
class C(
    val s: S,
    val snull: S?
)

fun main() {
    val tsGenerator = KxsTsGenerator()
    println(tsGenerator.generate(serializer<C>()))
}

It generates the following type of script file, which is not valid.

export interface C {
  s: S;
  snull: S | null;
}

export type S =
  | S.A
  | S.B;

export namespace S {
  export enum Type {
    A = "S.A",
    B = "S.B",
  }
  
  export interface A {
    type: S.Type.A;
  }
  
  export interface B {
    type: S.Type.B;
  }
}

export namespace S {
  export enum Type {
    A = "S.A",
    B = "S.B",
  }
  
  export interface A {
    type: S.Type.A;
  }
  
  export interface B {
    type: S.Type.B;
  }
}

As I understood from debugger, there is some problems with handling nullable types, as there is something called S.A and something called S?.A, and deduplication doesn't work because of that.

@aSemy
Copy link
Contributor

aSemy commented Sep 16, 2023

Definitely a bug! Thanks for finding and reporting it.

I think your analysis is right - kxs-ts-gen isn't de-duplicating a nullable serializer.

I expect it won't be too difficult to fix, but I haven't touched this project in a while so I'll need to do some housekeeping first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants