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

[BUG] encodedName of a root coproduct type is applied to sub-types #3424

Closed
jumale opened this issue Dec 22, 2023 · 0 comments · Fixed by #3430
Closed

[BUG] encodedName of a root coproduct type is applied to sub-types #3424

jumale opened this issue Dec 22, 2023 · 0 comments · Fixed by #3430
Assignees
Labels
bug Something isn't working

Comments

@jumale
Copy link
Contributor

jumale commented Dec 22, 2023

Tapir version: 1.9.5

Scala version: 2.13

Note: I'm not completely sure if in general this is a valid use-case for encodedName, because in Tapir-documentation it's only mentioned as an annotation for class-fields, but it does not say anything about classes themselves.

Describe the bug

When annotating a sealed trait with encodedName - the encoded name is applied to both the parent trait and all it's sub-types, unless they have their own encodedName.
As you can see in the example below - the trait has 3 sub-types Foo/Bar/Baz.
The root type and one of sub-types are annotated with custom encoded name.
The resulting derived schema has correct encoded names for MyType and Foo, however Bar and Baz are both named as their parent "CustomMyType", which probably also resulted in replacing Bar with Baz in the list of subtypes.

How to reproduce?

import sttp.tapir.Schema
import sttp.tapir.Schema.annotations.encodedName

// given
@encodedName("CustomMyType")
sealed trait MyType

object MyType {

  @encodedName("CustomFoo")
  final case class Foo(a: String)  extends MyType
  final case class Bar(b: Int)     extends MyType
  final case class Baz(c: Boolean) extends MyType
}

// when
val schema = Schema.derived[MyType]

// then schema results in
Schema(
  name = Some(SName("CustomMyType")),
  discriminator = None,
  schemaType = SCoproduct(
    subtypes = List(
      Schema(
        schemaType = SProduct(List(SProductField(FieldName(a,a),Schema(SString(), ...)))),
        name = Some(SName("CustomFoo")),
        ...
      ),
      Schema(
        schemaType = SProduct(List(SProductField(FieldName(c,c),Schema(SBoolean(), ...)))),
        name = Some(SName("CustomMyType")),
        ...
      )
    )
  ),
  ...
)
@kciesielski kciesielski self-assigned this Dec 27, 2023
@kciesielski kciesielski added the bug Something isn't working label Dec 28, 2023
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
Development

Successfully merging a pull request may close this issue.

2 participants