OpenAPITools / OpenAPITools/openapi-generator

[BUG] Code generation is broken for `oneOf` types (Rust Server)

Open
#17,210 1 comment 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Rust Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I had some code that used to work on 5.4.0 and now it doesn't. I traced the issue down to some change in the implementation of the usage of oneOf, where previously swagger::OneOf<A,B> was generated, now a new model is generated, but that new model is empty.

https://gist.github.com/mirosval/433308deb22727e3b1102fb58f3a6652

openapi-generator version

Works on 5.4.0 and does not since 6.0.

OpenAPI declaration file content or url

I'll provide a full gist, but shortly these are the interesting bits:

openapi: 3.0.3

...

components:
  schemas:
    Test:
      type: object
      properties:
        something:
          oneOf:
            - type: string
            - type: integer

Before:

    pub something: Option<swagger::OneOf2<String,i32>>,

After:

    pub something: Option<models::TestSomething>,

...

#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, validator::Validate)]
#[cfg_attr(feature = "conversion", derive(frunk::LabelledGeneric))]
pub struct TestSomething {
}
Generation Details
generate-710:
	docker run --rm \
		-v $(PWD):/local openapitools/openapi-generator-cli:v7.1.0 generate \
		-i /local/test.yaml \
		--generator-name rust-server \
		--output /local/openapi \
		--global-property models,modelDocs=false,supportingFiles
	mv openapi/src/models.rs test-710.rs
	rm -rf openapi
Steps to reproduce

See the Gist and then make generate-540 generate-710 generate-latest

Related issues/PRs

This looks like it might be related:

https://github.com/OpenAPITools/openapi-generator/issues/13538

Suggest a fix

It seems that generating intermediate models for nested properties is a good idea, just that there should probably be some heuristic to detect when the nested type is a oneOf and use a different path.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Inspect the linked gist and test.yaml, then run the Makefile targets generate-540, generate-710, and generate-latest. Compare openapi/src/models.rs with the shown Rust outputs; done means the nested oneOf property is generated with a usable representation rather than an empty TestSomething model.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
api, backend, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.