-
Notifications
You must be signed in to change notification settings - Fork 6k
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
[Java] Swagger oneOf type: Jackson trying to instantiate interface instead of implementation? #10011
Comments
I've run into a very similar issue, if i use anyOf or oneOf and generate a java client I run into issues where it has created an interface called AnyOfBody and then a basic implementation of it called Body. All of my generated classes are implementations of AnyOfBody but the api client methods are all expecting the input of type Body so all my request objects do not match. Because of this i am unable to execute any of the api calls that use oneOf or anyOf in the OAS. So far i haven't found a solution |
If I annotate the generated interface: public interface OneOfTestRequestCriteria {} with the following: @JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
@Type(value = CriteriaA.class, name = "CriteriaA")
})
public interface OneOfTestRequestCriteria {
} Then the request gets deserialized correctly into CriteriaA - am I missing something in my swagger.yaml that would result in this interface not getting annotated by the codegen tool? |
@rorytorneymf I have the same issue. Were you able to identify the issue? |
I have the same issue. |
Same problem here. I can make do with a custom deserializer but it would be nice if generation took care of it ^^ |
I had the same problem until I configured MixIn. I mapped the generated interface to implemented superclass with existing @JsonSubTypes and @JsonTypeInfo. It seems like |
Facing exact same issue |
+1 Same Issue |
any news? |
any update on this |
+1 Same Issue |
Hi, this issue hit our team too. |
There seem to be a bunch of issues with AnyOf / AllOf / OneOf - for example: swagger-api/swagger-codegen-generators#966 Is there any plan to address them / would you accept community submissions / bounties for addressing these? [@HugoMario ?] |
I've hit this as well. It's not usable. So I realize json that's not going to work, these generators need to do tricky jackson stuff. My workaround (if I stick with openapi at all) is to change oneOf to just a named references, and only use one of them. I think that would work well enough. In using openapi, coming from xmlschema, either I'm missing a lot since I'm new, or the tools are still pretty green in some areas. |
If anyone got the solution. Please publish it. |
I am also facing the same issue. What is the way to do through yml file. |
sorry for delay response, i'm going to implement the proposed solution. |
@HugoMario I've noticed there's a lot of issues in this space. It's especially tricky if you're trying to follow some 3rd party API that uses these heavily. If it would help I can provide some examples of real-world edge cases? |
@msmerc that would be helpful, sure. Please share the examples. |
hey guys, i added a PR to accomplish this issue Can you please check it out with this snapshot version of swagger cli and let me know if there is something else to add? |
@HugoMario I've put some comments on the MR. Unfortunately it wouldn't work for my use case since the component I'm talking to is very strict about the JSON it receives and will reject this because it has an unknown field. It actually makes it worse since currently I'm able to use jackson mixins to fix the broken inheritence. |
@HugoMario I've created a gist with the subset of an actual Bloomberg API I'm using. See: https://gist.github.com/msmerc/bb7d4b848585a26b7baa2bd5722d1fcc In particular it's worth noting they have a field that's called Slightly related to this issue; another thing they are keen on is this construct:
Here they are saying the type field is a |
@msmerc i'll file a new ticket to follow up what you're reporting. |
Hi @HugoMario, part of which release is this fix? |
Description
I'm using the
oneOf
feature to define several possible schemas that can go into a request body property of my service. In the generated Java client code, the Java implementations of these schemas implement an interface, but when I send a request through, Jackson is trying to create an instance of the interface, instead of the concrete class.Swagger-codegen version
Swagger declaration file content or url
Command line used for generation
Maven plugin specified above used for generation.
Steps to reproduce
The Java client code generated by swagger codegen looks like this:
Interface:
Concrete class:
I'm trying to use this generated client code to send a request:
Running the above client code results in this error when Jackson tries to deserialize it. It seems to be trying to construct an instance of the interface OneOfTestRequestCriteria, instead of the concrete implementation of the interface; CriteriaA:
Related issues/PRs
OpenAPITools/openapi-generator#4785
The text was updated successfully, but these errors were encountered: