New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Invalid request body reference passes validation, but throws NPE in codegen #1112
Comments
I'm having the same problem, but my schema objects aren't missing. Any advice? I've tried 3.3.1 and the 3.4.0-SNAPSHOT openapi: "3.0.2"
info:
description: test
version: "v1"
title: "Moov API"
contact:
email: security@moov.io
url: "https://groups.google.com/forum/#!forum/moov-users"
license:
name: "Apache 2.0"
url: "http://www.apache.org/licenses/LICENSE-2.0.html"
servers:
- url: https://api.moov.io
description: Production server
tags:
- name: User
description: User represents an entity that can create api auth tokens used to make requests.
paths:
/v1/users/{user_id}:
patch:
tags:
- User
summary: Update a User's profile information
operationId: updateUserProfile
security:
- cookieAuth: []
parameters:
- $ref: '#/components/parameters/cookie'
- name: user_id
in: path
description: Moov API User ID
required: true
schema:
type: string
example: 3f2d23ee214
requestBody:
description: "test" # TODO(adam)
content:
application/json:
schema:
$ref: '#/components/requestBodies/UserProfile'
responses:
'200':
description: User profile updated
content:
application/json:
schema:
$ref: '#/components/schemas/User'
'400':
description: Invalid request body, check error(s).
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
components:
schemas:
Error:
required:
- error
properties:
error:
type: string
description: An error message describing the problem intended for humans.
example: Validation error(s) present.
User:
properties:
id:
description: Moov API user ID
type: string
example: c05ad98a
firstName:
type: string
example: Taylor
requestBodies:
UserProfile:
description: 'User profile information'
# required: true
content:
application/json:
schema:
properties:
firstName:
type: string
description: Legal first name
example: Jane
parameters:
cookie:
name: cookie
in: cookie
schema:
type: string
description: moov_auth Cookie
securitySchemes:
cookieAuth:
type: apiKey
in: header
name: Cookie
description: moov_auth Cookie header
|
It looks like there's a problem if you https://github.com/moov-io/api/pull/24/commits/5486832ce4fa8eed614b0fa8beb76b6b0db4e99d |
I figured out my problem. It was missing https://github.com/moov-io/api/pull/43 was the fix for my project |
I have a similar problem, but no NPE. Instead:
I moved a |
I had the same problem and after moving them into See: https://github.com/moov-io/api/pull/43/files#diff-fe030a7c1568b3decf599edf399be7f4 |
so the conclusion is like openapi-generator doesnt deal with requestBodies well? |
Same problem with requestBodies |
I also ran into this issue - the validation step passes, as does my usual validation tooling (spectral) - but the generated code has the |
… generating golang client Issue described is described here: OpenAPITools/openapi-generator#1112
… generating golang client Issue described is described here: OpenAPITools/openapi-generator#1112
This bug is really annoying I got completely broken Javascript client build because of that. I also do In generated API class: import ApiClient from "../ApiClient";
import ModelApplication from '../model/ModelApplication';
import UNKNOWN_BASE_TYPE from '../model/UNKNOWN_BASE_TYPE'; Then compile error: ERROR in ./out/api-client/src/api/MessagesApi.js 20:0-59
Module not found: Error: Can't resolve '../model/UNKNOWN_BASE_TYPE' in '/Users/ybelenko/Sites/front_react/out/api-client/src/api' |
First of all, you have to override the here is mine : @Override
public void processOpenAPI(OpenAPI openAPI) {
//Include Request bodies into models
if(openAPI.getComponents().getRequestBodies()!=null)
openAPI.getComponents().getRequestBodies().forEach((requestName, requestBody) -> {
if(openAPI.getComponents().getSchemas()!=null)
openAPI.getComponents().getSchemas().put(requestName.contains("Request")?requestName:requestName+"Request",requestBody.getContent().get("application/json").getSchema());
});
//Include Responses into models
if(openAPI.getComponents().getResponses()!=null)
openAPI.getComponents().getResponses().forEach((responseName, apiResponse) -> {
openAPI.getComponents().getSchemas().put(responseName.contains("Response")?responseName:responseName+"Response",apiResponse.getContent().get("application/json").getSchema());
});
super.processOpenAPI(openAPI);
} this code will add all requestBodies and responseBodies to Models with a suffix Of Request and Response respectively. then you need to add this |
I had same problem. And I fixed (a bit troublesome though...)
|
Description
A spec with an invalid request body reference will pass validation, but get NullPointerException during codegen.
The NPE happens in DefaultCodegen.fromRequestBody:
schema is not found, and ModelUtils.getSchemaFromRequestBody returns null.
openapi-generator version
3.3.0-SNAPSHOT
OpenAPI declaration file content or url
Config file:
Command line used for generation
java -jar OPENAPIVERSION.jar generate -v -o genjava -g java -i invalid_request_body_ref.json -c rpdmcpp.config
Steps to reproduce
Run the generate command above
Related issues/PRs
None I could find.
Suggest a fix/enhancement
Returning null from not-found references seems like a bad idea. Is this ever valid? If not, throw an exception:
Overall, the "return null if not found" convention seems to be poorly documented at best, and probably just wrong, given how easy it is to cause an NPE.
The text was updated successfully, but these errors were encountered: