openapi-generator: [JAVA] Untyped request bodies not handled
Description
Generating code (both client and server) for a model where a resource has a body parameter with either no type or of type object generates a warning and code which does not compile.
Warning this generates is similar to:
[main] WARN o.o.codegen.DefaultCodegen - Unknown type found in the schema: object
[main] WARN o.o.codegen.DefaultCodegen - The following schema has undefined (null) baseType. It could be due to form parameter defined in OpenAPI v2 spec with incorrect consumes. A correct 'consumes' for form parameters should be 'application/x-www-form-urlencoded' or 'multipart/form-data'
[main] WARN o.o.codegen.DefaultCodegen - schema: class ObjectSchema {
class Schema {
title: null
multipleOf: null
maximum: null
exclusiveMaximum: null
minimum: null
exclusiveMinimum: null
maxLength: null
minLength: null
pattern: null
maxItems: null
minItems: null
uniqueItems: null
maxProperties: null
minProperties: null
required: null
type: null
not: null
properties: null
additionalProperties: null
description: null
format: null
$ref: null
nullable: null
readOnly: null
writeOnly: null
example: null
externalDocs: null
deprecated: null
discriminator: null
xml: null
}
type: object
defaultObject: null
}
[main] WARN o.o.codegen.DefaultCodegen - codegenModel is null. Default to UNKNOWN_BASE_TYPE
Generated client code in DefaultApi:
import org.openapitools.client.model.UNKNOWN_BASE_TYPE;
...
public void thePost(UNKNOWN_BASE_TYPE UNKNOWN_BASE_TYPE) throws ApiException {
thePostWithHttpInfo(UNKNOWN_BASE_TYPE);
}
No such model is generated though even if it was, it wouldn’t be terribly useful. The server generated code has a very similar problem.
I have tried this with both type: object and no type specified.
openapi-generator version
I’ve tried with 3.3.2 and master.
OpenAPI declaration file content or url
{
"swagger": "2.0",
"info": {
"version": "0.0.1",
"title": "Test"
},
"basePath": "/",
"paths": {
"/": {
"post": {
"operationId": "thePost",
"parameters": [
{
"in": "body",
"name": "body",
"required": "false",
"schema": {
"type": "object"
}
}
],
"responses": {
"200": {
"description": "response"
}
}
}
}
}
}
Command line used for generation
Client generation:
java -jar ~/.m2/repository/org/openapitools/openapi-generator-cli/3.3.2/openapi-generator-cli-3.3.2.jar generate -c config.json -i api.json -g java -o output
The contents of config.json are:
{
"library": "jersey2"
}
Having said that, I have tried with other libraries and the same problem exists.
Server generation:
java -jar ~/.m2/repository/org/openapitools/openapi-generator-cli/3.3.2/openapi-generator-cli-3.3.2.jar generate -i api.json -g jaxrs-jersey -o output
Steps to reproduce
Generate code with the model above and compile.
Related issues/PRs
This looks similar to https://github.com/OpenAPITools/openapi-generator/pull/1078 but the proposed fix in that PR doesn’t resolve this particular problem.
Suggest a fix/enhancement
I have been able to fix this by changing https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java#L4556 to set codegenModelName to typeMapping.get("Object") instead of the hardcoded UNKNOWN_BASE_TYPE. I’m not totally convinced that’s the right fix though but if it is I’m happy to open a PR with that.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 4
- Comments: 23 (13 by maintainers)
Fixed in v6.0.0. Please give it another try.
With org.openapitools:openapi-generator-maven-plugin:5.2.1 (latest at time of this comment) the problem occurs when the spec json/yaml’s endpoint is lacking the “type” attribute even though the validator passes just fine. I had to manually add in “type: object” and all is well I get one of those InlineObject## classes to use with the generated endpoint.
Fails:
Works:
To reproduce simply try generating a client from this spec: https://docs.corepro.io/reference#programquestionslist
OperationIDs that are affected:
@wing328 any help pretty please? Still can’t figure out how to do it without workarounds on 5.0.1 version
Yes, please.
(I agree the generator should better handle references to
content)Same problem if define two endpoints with formData property (same name). Example with relevant code: