OpenAPITools / OpenAPITools/openapi-generator
[BUG] Description
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- 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
Spring Validation throws binding error for class with JsonNullable(from openAPI)
openapi-generator version
org.openapi.generator=5.3.0
OpenAPI declaration file content or url
paths:
patch:
summary: Update user properties
description: Allow to update user properties
operationId: updateUserInfo
tags:
- user info
requestBody:
content:
'application/json':
schema:
$ref: '#/components/schemas/UserDetails'
schemas:
UserDetails:
description: Details of the user
type: object
properties:
address:
nullable: true
allOf:
- $ref: '#/components/schemas/Address'
name:
type: string
description: name of the user
example: "John"
phone:
type: string
description: Phone number of the user
example: "8795678633"
nullable: true
Address:
type: object
required:
- country
properties:
country:
type: string
example: "France"
state:
type: string
example: "Lyon"
district:
type: string
example: 'LL'
Generation Details
There are two classes generated classes from openAPI,
UserDetails class holds,
@JsonProperty("address")
private JsonNullable<Address> address = JsonNullable.undefined();
@JsonProperty("name")
private String name;
@JsonProperty("phone")
private JsonNullable<String> phone = JsonNullable.undefined();
Address class holds
@JsonProperty("country")
private String country;
@JsonProperty("state")
private String state;
@JsonProperty("district")
private String district;
@ApiModelProperty(example = "France", required = true, value = "")
@NotNull
public String getCountry() {
return country;
}
In short, we have country as not null and address holding the country fields is wrapped by JsonNullable.
Steps to reproduce
We have a rest endpoint that takes in the UserDetails as the payload. When we send a payload without the country value in address, similar to the below
{
"name": "John",
"address": {
"state": "Lyon",
"district": "LL"
},
"phone" : "+44232309923"
}
We get a spring binding error,
JSR-303 validated property 'address.country' does not have a corresponding accessor for Spring data binding - check your DataBinder's configuration (bean property versus direct field access)
What happened behind the scenes?
- Spring identifies the required constraint violation for country.
- In order to bind the violation to the field variable, Spring searches for the wrapper class of country.
- Spring search happens in two steps
a. Fetches the bean wrapper class
b. Fetches the getter method in the wrapper class for the field violating the constraint - In an ideal scenario, it was expecting Address class which is the wrapper class in our case.
- Since Address is wrapped by JsonNullable, spring returns JsonNullable as the wrapper instead of Address.
- The getter for country is obviously not within the JsonNullable class, and so the error is thrown.
Is there a workaround for this? Or am I missing anything here?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the Spring binding failure with the provided OpenAPI schema, generated UserDetails and Address classes, and the JSON payload that omits address.country. Trace how JsonNullable wraps Address during validation; done means the required nested country violation is handled without the reported accessor error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi, spring
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100