-
-
Notifications
You must be signed in to change notification settings - Fork 17
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
OneOf for sealed classes imported from another module #235
Comments
It's a little bit tricky, because annotation processing is supposed to work only for current sources - other modules behave quite the same as external dependencies, these sources were already processed and compiled before, so there's no point to repeat that process over and over again. I don't quite remember the limitations of AP, but assuming we can get an annotation from an external class, maybe we could just extend @OneOf(
discriminator = Discriminator(
property = DiscriminatorProperty(
name = "type",
type = String::class,
injectInMappings = true
)
)
)
sealed interface Union
@DiscriminatorMappingName("class-a")
data class A(val a: Int) : Union
@DiscriminatorMappingName("class-b")
@OpenApiName("B")
data class C(val b: String) : Union we could allow defining it like that: @OneOf(
discriminator = Discriminator(
property = DiscriminatorProperty(
name = "type",
type = String::class,
mappings = [
Mapping("class-a", A::class),
Mapping("class-b", B::class),
]
)
)
)
sealed interface Union
data class A(val a: Int) : Union
@OpenApiName("B")
data class C(val b: String) : Union I think that then we could be able to get it from |
Thanks for a quick reply @dzikoysk Duplicating all classes with their dependencies becomes too massive, so for now I ended up with a code pretty close to your second example with one difference: the A and B classes are also required to be added to This is not enough though, because if I do so, injectMapping is not working (debug revealed that it happens because subclasses are first added to references from I like that the approach you are suggesting allows usuto take the list of classes for oneOf values directly from mappings, it also should resolve the issue with injecting discrimator property. The only thing that worries me is that this approach assumes that
Such a number of options to do the same looks a bit complicated especially when they are allowed all at the same time and there is no clear priority of one over another. There are several things I would like to discuss: where to have
|
I agree, let's keep it as simple as possible. I didn't pay that much attention into details, I'm completely fine with having
I'd personally throw an exception for now - it seems to be simpler. In case someone will face an issue with that, we can always go back to this discussion and implement that having a real world use-case. So summing up - we'll have pretty much something like that: @OneOf(
discriminator = Discriminator(
property = DiscriminatorProperty(
name = "type",
type = String::class,
)
),
mappings = [
Mapping("class-a", A::class),
Mapping("class-b", B::class),
]
)
sealed interface Union
If it looks correct to you, feel free to open a PR 🙏 |
Hi @dzikoysk thank you for the great support of
@OneOf
for sealed classes. Unfortunately, recently I faced a new issue connected to it.Previously I had only one submodule requiring OpenAPI spec but recently I got another one. And this second one uses some API classes from the first one. And there I discovered that automatic filling and mapping of
@OneOf
is not working for sealed classes imported from another module.After looking into the annotation processor code I realized that it happens because the processor searches for candidate classes for
oneOf
list – (subtypes of the given sealed class) among the list of classes annotated with@DiscrimnatorMappingName
which doesn't include any classes from an external module.I couldn't find any quick solution to this problem and have to duplicate such classes in every module that uses them.
Do you have any idea of how to make
@OneOf
work properly (the same way as it works in its own module)? I'm ready to try to implement it if you have one.The text was updated successfully, but these errors were encountered: