OpenAPITools / OpenAPITools/openapi-generator
[BUG][Spring] can't use `pattern` for the `nullable` property in request body
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)?
- What's the version of OpenAPI Generator used?
- Have you search for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Bounty to sponsor the fix (example)
Description
If the request body definition contains properties with both nullable and pattern, the generated Spring stub server will throw an exception when the request is received.
openapi-generator version
$ openapi-generator-cli version
4.0.3-SNAPSHOT
$ git log --oneline -n 1
51e7005373 [online] Remove GENERATOR_HOST defaults (#3289)
OpenAPI declaration file content or url
See: https://gist.github.com/autopp/ddb42bee720ba0147247da81b6647138
Note that the property foo in the request body for /hello is specified both nullable: true and pattern.
foo:
type: string
nullable: true
pattern: '[a-z]+'
Command line used for generation
$ java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate --skip-validate-spec -g spring -o /tmp/spring -i example.yml --additional-properties returnSuccessCode=true
Steps to reproduce
- Run the above command to generate Spring server.
- Build and run generated server.
- Request to
/hellowith payload$ curl -X POST localhost:8080/hello -H 'Content-Type: application/json' -d '{"foo":"abc"}'
The client should get the error response:
{"timestamp":"2019-07-08T00:52:28.547Z","status":500,"error":"Internal Server Error","message":"HV000030: No validator could be found for constraint 'javax.validation.constraints.Pattern' validating type 'org.openapitools.jackson.nullable.JsonNullable<java.lang.String>'. Check configuration for 'foo'","path":"/hello"}
And server output error log:
2019-07-08 09:52:22.719 ERROR 66621 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.Pattern' validating type 'org.openapitools.jackson.nullable.JsonNullable<java.lang.String>'. Check configuration for 'foo'] with root cause
javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.Pattern' validating type 'org.openapitools.jackson.nullable.JsonNullable<java.lang.String>'. Check configuration for 'foo'
...
Whole log: https://gist.github.com/autopp/c58955e0277b7dbb3c85bbffcfa89a6c
In my use case, I want to use both nullable and pattern simultaneously.
Related issues/PRs
N/A
Suggest a fix
Sorry, I have no idea.
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 generated Spring server with the command in modules/openapi-generator-cli and the nullable string property using both pattern and nullable. Trace the Spring generator output and validation handling for that property. Done means a valid request such as {"foo":"abc"} no longer raises the Pattern validator exception while the pattern constraint remains effective.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100