openapi-generator
openapi-generator copied to clipboard
[BUG] Request body cannot be null
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
Support OAS 3 POST with no body for openapi: 3.* schemas rather than 2.*
- http://spec.openapis.org/oas/v3.1.0#fixed-fields-11
- https://swagger.io/specification/v2/#parameter-object
Given a post with no body
openapi: 3.0.1
#...
requestBody:
required: false
content:
application/json: {}
openapi-generator version
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>5.1.0</version>
<executions>
<execution>
<id>dlt-iam-client</id>
<phase>process-resources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<library>feign</library>
<inputSpec>${project.build.outputDirectory}/openapi/xxx.openapi.yml</inputSpec>
<modelPackage>au.com.xxx.client.models</modelPackage>
<apiPackage>au.com.xxxclient..rest</apiPackage>
<verbose>false</verbose>
<configHelp>false</configHelp>
<generatorName>java</generatorName>
<generateSupportingFiles>true</generateSupportingFiles>
<supportingFilesToGenerate>ApiClient.java,RFC3339DateFormat.java,HttpBasicAuth.java,ApiKeyAuth.java,HttpBearerAuth.java,EncodingUtils.java,StringUtil.java</supportingFilesToGenerate>
<generateApis>true</generateApis>
<generateModels>true</generateModels>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
<generateApiDocumentation>false</generateApiDocumentation>
<generateModelDocumentation>false</generateModelDocumentation>
<configOptions> <!-- https://github.com/OpenAPITools/openapi-generator/blob/v5.1.0/docs/generators/java.md -->
<performBeanValidation>true</performBeanValidation>
<useBeanValidation>true</useBeanValidation>
<java8>true</java8>
<interfaceOnly>true</interfaceOnly>
<dateLibrary>java8</dateLibrary>
<delegatePattern>false</delegatePattern>
<useTags>true</useTags>
</configOptions>
</configuration>
</execution>
</executions>
Steps to reproduce
attempt to generate classes for a POST operation with no body content
Caused by: java.lang.RuntimeException: Request body cannot be null. Possible cause: missing schema in body parameter (OAS v2): class RequestBody {
description: null
content: class Content {
{application/json=class MediaType {
schema: null
examples: null
example: null
encoding: null
}}
}
required: false
}
Suggest a fix
I can see here https://github.com/OpenAPITools/openapi-generator/blob/v5.1.0/docs/generators/java.md#parameter-feature
That the Body
param is aligned to OAS2 only.
I'm seeing the same issue on the CLI with an OpenAPI 3.0.3 spec EDIT: I think I confused this Issue with another similar one that said the Gradle Plugin was causing the issue but that the CLI worked
Getting the same error when using typescript-node
generator with Flink OpenAPI:
openapi-generator-cli generate -g typescript-node -i https://nightlies.apache.org/flink/flink-docs-release-1.15/generated/rest_v1_dispatcher.yml -o ./src
Any suggestion/workaround is appreciated.
Any update with this bug?
This is still an issue whatever the generator used
example :
$ openapi-generator-cli generate \
-i https://github.com/cloudflare/api-schemas/raw/main/openapi.json \
-g typescript-axios -o src/
output :
Exception: Request body cannot be null. Possible cause: missing schema in body parameter (OAS v2): class RequestBody {
description: null
content: class Content {
{application/json=class MediaType {
schema: null
examples: null
example: null
encoding: null
}}
}
required: true
}
at org.openapitools.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:1201)
at org.openapitools.codegen.DefaultGenerator.processPaths(DefaultGenerator.java:1094)
at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:586)
at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:929)
at org.openapitools.codegen.cmd.Generate.execute(Generate.java:487)
at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Caused by: java.lang.RuntimeException: Request body cannot be null. Possible cause: missing schema in body parameter (OAS v2): class RequestBody {
description: null
content: class Content {
{application/json=class MediaType {
schema: null
examples: null
example: null
encoding: null
}}
}
required: true
}
at org.openapitools.codegen.DefaultCodegen.fromRequestBody(DefaultCodegen.java:7357)
at org.openapitools.codegen.DefaultCodegen.fromOperation(DefaultCodegen.java:4508)
at org.openapitools.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:1169)
... 6 more
error Command failed with exit code 1.
$ openapi-generator-cli version
6.5.0
$ curl -sSL https://github.com/cloudflare/api-schemas/raw/main/openapi.json | jq '.openapi'
"3.0.3"
Any news? It is still an issue in latest plugin 6.6.0. Are a workaround available, because the code generation is not usable currently.
If anyone wants to contribute a fix or sponsor the fix, please reply to let us know. Thank you.
Because we needed to generate a quite large API, we investigated the error a bit.
To me it seems that the problem lies in the class org.openapitools.codegen.DefaultCodegen
(in module openapi-generator
).
Around line 7498 it states:
if (schema == null) {
throw new RuntimeException("Request body cannot be null. Possible cause: missing schema in body parameter (OAS v2): " + body);
}
This throws the exception, and there is no check whether it is OAS V3 or V2 (in V3, empty bodies are allowed). Also, line 7566 seems to handle the body somehow if not specified, but we never get there:
addJsonSchemaForBodyRequestInCaseItsNotPresent(codegenParameter, body);
So I suggest the following:
Replace the above if (schema == null)
check with the following:
if (schema == null) {
SemVer version = new SemVer(this.openAPI.getOpenapi());
if (version.atLeast("3.0")) {
LOGGER.debug("Request body is null - but in OAS v3 this is allowed");
}
else {
throw new RuntimeException("Request body cannot be null. Possible cause: missing schema in body parameter (OAS v2): " + body);
}
}
else {
// here: move all the code before the line addJsonSchemaForBodyRequestInCaseItsNotPresent(codegenParameter, body)
}
addJsonSchemaForBodyRequestInCaseItsNotPresent(codegenParameter, body);
I tested the changed code and calling the java code directly, I could successfully generate quite a large model that has empty body specifications.
Should I create a pull request with my changes?
Yes we welcome PR to fix this issue.
Thanks for the PR from @EdithBirrer1
For those who needs the fix, please pull the latest master or use the SNAPSHOT version to give it a try.