Hi, what's the status of `withExplicitSealedTypes(...
# hoplite
s
Hi, what's the status of
withExplicitSealedTypes()
in Hoplite 3? Its docs say it was supposed to become the default in v3, but apparently it didn't. Which actually is good for me, as I still need to deserialize sealed interfaces without a type descriptor. However, doing that now seem to be broken, as the returned type always is one fixed sealed interface member, and not the one with matching properties. So, to start with, is deserializing without a type descriptor supposed to work in v3 at all?
The underlying problem seems to be that
DataClassDecoder().decode()
regards data as valid even if the key names do not match the property names...
I plan to prepare a PR that takes the number of matches into account when determining candidates.
But first some cleanups: github.com/sksamuel/hoplite/pull/597
s
merged
s
Awesome, thanks. Then here comes the real thing: github.com/sksamuel/hoplite/pull/598
what's the status of
withExplicitSealedTypes()
in Hoplite 3?
Any comment here? If it's not intended to become the default anymore, I could make another PR to fixup the docs in that regard.
s
We could change the docs yes.
s
s
Merged
πŸ™πŸ» 1
s
Thanks! There seem to be 3 more rather low-hanging-fruit PRs open from your side. Can you find the time to update and then merge these, and then getting out another 3.x release soon?
Gentle ping @sam πŸ™‚
s
1 merged 2 to go. Release coming today
πŸš€ 1
r
Not using
withExplicitSealedTypes
in 3.0 still prints a deprecation warning though -- is not using
withExplicitSealedTypes
actually still deprecated?
s
Well, the deprecation warning was there also pre-3.0 IIRC, so it would be a bit awkward to not make it deprecated behavior now in 3.0, or?
r
Why? Making it non-deprecated now just reflects a change in direction, contemplated but not carried out. The real question is: are we actually changing directions, or just delaying the change?
There is another problem with
withExplicitSealedTypes
as it is currently implemented: the default type discriminator is
_type
which works very poorly with the new idiomatic env var style rolled out in 3.x -- in fact, in the testing I just did, I can see it does not work at all. Updating the env var parsing logic to handle a double-underscore naively may cause issues with users upgrading from 2.x, as their existing env var variables of the form
FOO__BAR
will silently get transformed to
foo._bar
instead of
foo.bar
, which is a good reason to not futz with the current parsing logic. Given an input type:
Copy code
data class SomeConfig(
  val sealedValue: SomeSealedClass
)

// only one implementation shown
data class Foo(val x: String) : SomeSealedClass
A user might assume this should work:
Copy code
SOMECONFIG_SEALEDVALUE__TYPE=Foo
SOMECONFIG_SEALEDVALUE_X=abc
but that is both ugly, and does not work anyway, because the current code splits on
_
. The natural and idiomatic way to express that in environment variables (or really any source with flat keys) would be:
Copy code
SOMECONFIG_SEALEDVALUE=Foo
SOMECONFIG_SEALEDVALUE_X=abc
But formats like HOCON can't express this -- they can't have a key that is both a value and a map. So those would still require the discriminator. I haven't investigated what the code for this would look like, but in principle what do you guys think about that approach? It makes flat sources parse a bit differently than structured ones, but I think in a good way. Related: I think I would also want subtype lookup to honor
withResolveTypesCaseInsensitive()
(the kdoc already mentions "type/name" resolution, so I think that is consistent with the original intent).
s
are we actually changing directions, or just delaying the change?
I was asking the same to @sam but got no clear answer πŸ˜… Just to clarify: I'm not against un-deprecating (de-)serialization without type descriptors. Actually, we currently depend on not having type descriptors. But I felt that doing the fixups that I did was the smallest investment to make things consistent again, until we have clarity on the above question.
πŸ‘ 1
To me, being able to deserialize sealed classes without a type descriptor actually is a major selling point for Hoplite. Maybe, the motivation for moving to type descriptors came from not having a heuristic like github.com/sksamuel/hoplite/pull/598 yet, but now that we have it, maybe we don't need to rely on explicit type descriptors anymore?
πŸ‘ 1
r
I don't completely disagree, but any heuristic is inevitably wrong in some cases -- and I dislike magic. So personally I like and prefer
withExplicitSealedTypes
, That being said, I wouldn't deprecate the implicit approach unless it creates an unreasonable maintenance burden to keep it.
Over the next few days I'll submit a PR for the change I proposed above, unless there are objections here.
πŸ‘πŸ» 1
s
the default type discriminator is
_type
which works very poorly with the new idiomatic env var style
Ok, but why would you ever want to set such kind of metadata values via environment variables? As a user, I wouldn't even necessarily expect it to work...
r
Why would it be unusual? Say I have a sealed class that represents an http client connection config: one is non-ssl, and one that has tls keys. It would be normal to specify the TLS-enabled sealed type via env vars for the production application (12-factor app), which means the env has to override the sealed type and set its properties.
s
I just feel that such _meta_data should not be tampered with / be allowed to set manually, via environment variables or other means. A type descriptor is a hint encoded as metadata by the serializer for the deserializer, as I see it. But I won't object, I just find it strange.
r
I mean, that's effectively what we're doing here too right? We're just explicitly telling the config system -- the deserializer in this case -- our desired target type.
s
Yeah, I guess you could see it that way πŸ˜ƒ
r
My example above also shows why the heuristic is not sufficient. Say you define the TLS settings by default (in a config file shipped with the app), but you want to override to the non-TLS endpoint at deployment time (say in a development environment for testing). The heuristic approach would not allow it -- because all the defined TLS settings pulled from the default config would force the TLS sealed class to be chosen over the plain one.
Here is the PR to solve the problem with
withExplicitSealedTypes
and env vars not being able to specify the discriminator value
_type
, with the nice benefit that it allows env vars and other flat sources to specify sealed class subtypes cleanly and elegantly: https://github.com/sksamuel/hoplite/pull/604 Example:
Copy code
# specifies the type
DATABASE=Elasticsearch
DATABASE_PORT=9200