-
Notifications
You must be signed in to change notification settings - Fork 427
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
Rework Pickler for coproducts and enums #3222
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9a8f84f
Rework handling for enumeration
kciesielski 0e8fad8
Remove EnumValueDiscriminator
kciesielski b76356e
Test signatures
kciesielski 4ca7130
Split tests into smaller suites
kciesielski 718232a
Extend documentation about enumerations
kciesielski c0d32a2
Restore section name
kciesielski 5222bf3
Rework coproducts and configuration
kciesielski f7be39b
Add missing file
kciesielski f3792c3
Adjust documentation
kciesielski 23384c9
Organize imports
kciesielski 6368534
Restore original file state
kciesielski 7088450
Refactor handling default discriminator in writers
kciesielski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package sttp.tapir.json.pickler | ||
|
||
import _root_.upickle.{default => udefault} | ||
import magnolia1.SealedTrait | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import sttp.tapir.DecodeResult.Value | ||
|
@@ -360,28 +361,6 @@ class PicklerTest extends AnyFlatSpec with Matchers { | |
decoded shouldBe Value(inputObject) | ||
} | ||
|
||
it should "work2" in { | ||
sealed trait Entity { | ||
def kind: String | ||
} | ||
case class Person(firstName: String, lastName: String) extends Entity { | ||
def kind: String = "person" | ||
} | ||
case class Organization(name: String) extends Entity { | ||
def kind: String = "org" | ||
} | ||
|
||
import sttp.tapir.* | ||
import sttp.tapir.json.* | ||
|
||
val pPerson = Pickler.derived[Person] | ||
val pOrganization = Pickler.derived[Organization] | ||
given pEntity: Pickler[Entity] = | ||
Pickler.oneOfUsingField[Entity, String](_.kind, _.toString)("person" -> pPerson, "org" -> pOrganization) | ||
|
||
// { "$type": "person", "firstName": "Jessica", "lastName": "West" } | ||
pEntity.toCodec.encode(Person("Jessica", "West")) | ||
} | ||
it should "Set discriminator value using oneOfUsingField" in { | ||
// given | ||
val picklerOk = Pickler.derived[StatusOk] | ||
|
@@ -523,6 +502,21 @@ class PicklerTest extends AnyFlatSpec with Matchers { | |
codec.decode(encoded) shouldBe Value(inputObj) | ||
} | ||
|
||
it should "handle sealed hierarchies consisting of objects only" in { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we should somehow separate tests per-feature, here: for enumerations (direct, wrapped, customised) |
||
// given | ||
import generic.auto.* // for Pickler auto-derivation | ||
val inputObj = SealedVariantContainer(VariantA) | ||
|
||
// when | ||
val pickler = Pickler.derived[SealedVariantContainer] | ||
val codec = pickler.toCodec | ||
val encoded = codec.encode(inputObj) | ||
|
||
// then | ||
encoded shouldBe """{"v":"VariantA"}""" | ||
|
||
} | ||
|
||
it should "handle value classes" in { | ||
// when | ||
val pickler = Pickler.derived[ClassWithValues] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe also introduce a test case with
enum
s (parameterless and not)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(unless we already don't have one :) )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have such tests
tapir/json/pickler/src/test/scala/sttp/tapir/json/pickler/PicklerTest.scala
Line 448 in 3d981a7
tapir/json/pickler/src/test/scala/sttp/tapir/json/pickler/PicklerTest.scala
Line 478 in 3d981a7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok, sorry, I didn't unwrap enough context :) But maybe if they will be in separate files it will be easier to see what's already there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(btw. open api docs tests are grouped in a similar way - each test has its own test classes in the companion object etc.)