OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.57k stars 6.28k forks source link

[BUG] [JAVA] Is there currently support for oneOf/anyOf keywords in API spec? #18241

Open azadebrahimian opened 3 months ago

azadebrahimian commented 3 months ago

Bug Report Checklist

Description

I am trying to generate the necessary models for my API spec in springboot, however I am noticing issues with the generated files that utilize the oneOf keyword. I’d be more than happy to give more information on the specifics I am dealing with, but I’d first like to know if this is fully supported yet in my use case (java/springboot).

openapi-generator version

Most recent version

OpenAPI declaration file content or url
OneOfJobConfigs:
      title: OneOfJobConfigs
      oneOf:
        - $ref: '#/components/schemas/ObjectiveAJobConfig'
        - $ref: '#/components/schemas/ObjectiveBJobConfig'
        - $ref: '#/components/schemas/ObjectiveCJobConfig'
      discriminator:
        propertyName: job_config
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "job_config", visible = true)
@JsonSubTypes({
  @JsonSubTypes.Type(value = JobsConfigCreateInput.class, name = "JobsConfigCreateInput"),
  @JsonSubTypes.Type(value = ObjectiveAJobConfig.class, name = "ObjectiveAJobConfig"),
  @JsonSubTypes.Type(value = ObjectiveBJobConfig.class, name = "ObjectiveBJobConfig"),
  @JsonSubTypes.Type(value = ObjectiveCJobConfig.class, name = "ObjectiveCJobConfig")
})

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class OneOfJobConfigs {
  @JsonProperty("job_config")
  @Valid 
  private ObjectiveBJobConfigJobConfig jobConfig;

}

This is the generated model, but I need to be able to access all three Objective#JobConfigs from this.

Generation Details

n/a

Steps to reproduce

Use oneOf somewhere in your openAPI spec, and run the generate mvn command.

Related issues/PRs
Suggest a fix
sfumarolo commented 3 months ago

I'm having similar issues with an anyOf in the National Weather Service's public API.

    JsonLdContext:
      anyOf:
        -
          type: array
          items: {  }
        -
          type: object

The code generated can't handle when an Array is returned, even though the spec says it should be fine.

wing328 commented 3 months ago

please try java (jesery2) client generator with the latest master as we've merged a PR to support array in oneOf

you can find snapshot version in project's readme.

wing328 commented 2 months ago

just merged https://github.com/OpenAPITools/openapi-generator/pull/18324 to provide array type support in anyOf, oneOf,

please give it a try with the snapshot version (which will be published soon as part of the CI workflow)

snapshot version can be found in the project's readme.