-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
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
When using OAPI3 "discriminator" support, openapi-generator generates warnings when discriminator properties are specified with "$ref".
This is not ideal for large APIs that wish to use a shared enum that contains the discriminator mappings for many objects.
It is possible to silence the warning by re-defining the discriminator's type property in each polymorphic API object. In the example spec posted, this would look like:
BarBazType:
type: string
description: A discriminator reference.
enum:
- BAR
- BAZ
Baz:
required:
- type
properties:
type:
type: string
$ref: "#/components/schemas/BarBazType"
However, this produces an incorrect client, because the type property is generated as StrictStr with no enum constraints.
Actual output
... [main] WARN o.o.codegen.DefaultCodegen - 'Foo' defines discriminator 'type', but the referenced schema 'Bar' is incorrect. invalid type for type, set it to string [main] WARN o.o.codegen.DefaultCodegen - 'Foo' defines discriminator 'type', but the referenced schema 'Baz' is incorrect. invalid type for type, set it to string ...
Expected output
No warnings.
openapi-generator version
Bug detected on 7.2.0, confirmed against master (hash 6f01a7ad823).
OpenAPI declaration file content or url
Minimal spec to reproduce:
https://gist.github.com/Hnefi/86cb2d748748b468b449ed4984e71bef
Generation Details
Command-line parameters to reproduce:
openapi-generator generate -g python -i oapi3.yaml -o /tmp/python_client
Steps to reproduce
- Download the minimal API spec in the gist.
- Generate the client with the above command.
- Inspect warnings generated for the issue.
Related issues/PRs
The most relevant issue is:
Although the warning generated is the same, the root cause in my case seems to be the fact that the discriminator property in my minimal example uses "$ref".
Suggest a fix
When scanning the spec file to find the discriminator's property, inherit the type from child "$refs".