Repository navigation
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator?
- 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 specifying a default value for a object type field, that is specified via allOf, the default value is not correctly composed to render a compilable value in Java.
E.g. this Spec:
openapi: 3.1.0
[...]
components:
schemas:
Dto:
type: object
properties:
test:
$ref: '#/components/schemas/Nested'
default:
one: "one"
two: "two"
Nested:
type: object
properties:
one:
type: string
two:
type: stringresults is a compile error, as the Dto class will have a property
private Nested test = {"one":"one","two":"two"};which is no valid Java.
In contrast this spec, which results in exactly the same semantics, but does not use a reference to an external type:
openapi: 3.1.0
[...]
components:
schemas:
Dto:
type: object
properties:
test:
type: object
properties:
one:
type: string
two:
type: string
default:
one: "one"
two: "two"results in the expected initializer for the Nested field:
private DtoTest test = new DtoTest().one("one).two("two");The issue is in the org.openapitools.codegen.languages.AbstractJavaCodeGen class as the toDefaultValue method does not handle the case fully. The first schema will try to create the default towards a Schema of type JsonSchema and detect it to be a CompositeSchema as the parser creates an allOf from the reference. This schema will not get further treatment and gets passed to the super implementation, which just invokes toString() on it, resulting in the value presented above. The same happens when directly using the allOf in the spec like so:
openapi: 3.1.0
[...]
components:
schemas:
Dto:
type: object
properties:
test:
allOf:
- type: object
properties:
one:
type: string
two:
type: string
default:
one: "one"
two: "two"openapi-generator version
I found this using the 7.22.0 version of the generator.
OpenAPI declaration file content or url
See above for the examples. Here is a full spec file including the parts omitted above:
openapi: 3.1.0
info:
title: test defaults
version: "1"
paths:
'/test':
get:
summary: test
responses:
"200":
description: bla
content:
application/json:
schema:
$ref: '#/components/schemas/Dto'
components:
schemas:
Dto:
type: object
properties:
test:
$ref: '#/components/schemas/Nested'
default:
one: "one"
two: "two"
Nested:
type: object
properties:
one:
type: string
two:
type: stringGeneration Details
I first discovered this with the Spring-Generator, but this is probably true for all Java generators, as the issue is in the AbstractJavaCodegen.
Steps to reproduce
Use the above spec e.g. with the maven plugin.
Related issues/PRs
There are a lot of Issues related to composite schemas that might be related, however I didn't find one that is seems to be directly related.
Activity
[Java] Render object default for composed ($ref + default) schemas (O…
Merge upstream/master-7.24.0