openapi-generator
openapi-generator copied to clipboard
[BUG][JAVA] allOf with just one element generates incorrect validateJsonElement
Bug Report Checklist
- [x] Have you provided a full/minimal spec to reproduce the issue?
- [x] Have you validated the input using an OpenAPI validator (example)?
- [ ] Have you tested with the latest master to confirm the issue still exists?
- [x] Have you searched for related issues/PRs?
- [x] What's the actual output vs expected output?
- [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
When I provide only one element in allOf, then autogenerated java code doesn't compile, because it calls validateJsonElement on Object class.
openapi-generator version
7.9.0
OpenAPI declaration file content or url
Below yaml with only one element in allOf:
openapi: 3.1.0
info:
title: Product API
version: 1.0.0
paths:
/product:
get:
summary: Get a product
operationId: getProduct
responses:
'200':
description: Successful response
content:
application/json:
schema:
$ref: '#/components/schemas/Product'
components:
schemas:
Product:
type: object
title: Product
required:
- product
properties:
product:
$ref: '#/components/schemas/productDetails'
additionalProperties: false
productDetails:
type: object
allOf:
- required:
- productName
properties:
productName:
type: string
generates such code
// check to make sure all required properties/fields are present in the JSON string
for (String requiredField : Product.openapiRequiredFields) {
if (jsonElement.getAsJsonObject().get(requiredField) == null) {
throw new IllegalArgumentException(String.format("The required field `%s` is not found in the JSON string: %s", requiredField, jsonElement.toString()));
}
}
JsonObject jsonObj = jsonElement.getAsJsonObject();
// validate the required field `product`
Object.validateJsonElement(jsonObj.get("product"));
//^Cannot resolve method 'validateJsonElement' in 'Object' <-- error I see form above line in my IDE
}
but when I add second element to allOf, for example:
allOf:
- required:
- productName
- format: uri
it works fine and java code looks like this:
// check to make sure all required properties/fields are present in the JSON string
for (String requiredField : Product.openapiRequiredFields) {
if (jsonElement.getAsJsonObject().get(requiredField) == null) {
throw new IllegalArgumentException(String.format("The required field `%s` is not found in the JSON string: %s", requiredField, jsonElement.toString()));
}
}
JsonObject jsonObj = jsonElement.getAsJsonObject();
// validate the required field `product`
ProductDetails.validateJsonElement(jsonObj.get("product"));
Generation Details
I use java and gradle and this is my gradle task to generate docs:
tasks.named("openApiGenerate", org.openapitools.generator.gradle.plugin.tasks.GenerateTask::class) {
generatorName.set("java")
inputSpec.set("$rootDir/openapi/bundled.yaml")
outputDir.set("$buildDir/generated")
apiPackage.set("com.yourcompany.api")
modelPackage.set("com.yourcompany.model")
invokerPackage.set("com.yourcompany.invoker")
configOptions.set(mapOf(
"dateLibrary" to "java8",
"java8" to "true"
))
}
Steps to reproduce
just run openApiGenerate with provided openapi. Expected behaviour is that generated code compiles and work fine even if there is only one element in allOf.