swagger-api / swagger-api/swagger-parser
Null Pointer Exceptions when using anyOf/oneOf/allOf
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 867
- Forks
- 560
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 7
Description
When using anyOf, oneOf, or allOf in v2.0.9, I get a NullPointerException when trying to read the specification.
File I've been using to reproduce:
{
"openapi": "3.0.0",
"info": {
"description": "Some API",
"version": "0.0.1",
"title": "Some Title",
"contact": {
"name": "me",
"url": "https://suhdude.com",
"email": "nowhere@nowhere.com"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
}
},
"tags": [
{
"name": "Something",
"description": "API that does something"
}
],
"paths": {
"/some-path": {
"post": {
"summary": "Do something",
"description": "Does something",
"operationId": "someOperation",
"tags": [
"something"
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/SomeRequest"
}
}
}
},
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/SomeResponse"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"SomeResponse": {
"type": "object",
"description": "Some response",
"properties": {
"message": {
"type": "string",
"description": "Some message"
}
}
},
"SomeRequest": {
"type": "object",
"description": "Some request",
"properties": {
"regularType": {
"type": "string",
"description": "Simple type works",
"default": "some message"
},
"someOfType": {
"oneOf": [
{
"type": "string"
},
{
"type": "integer"
}
],
"description": "this breaks using oneOf/anyOf/etc.",
"default": 10
}
}
}
}
}
}
Just parsing/debugging with this:
package com.somewhere;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.OpenAPIV3Parser;
public class Main {
public static void main(String[] args) {
OpenAPI openAPI = new OpenAPIV3Parser().read(Main.class.getClassLoader().getResource("some-spec.json").getFile());
}
}
This same code parses the swagger file just fine on v2.0.8.
It seems to throw the error at some point in the getSchema method of the OpenAPIDeserializer.java file when attempting to parse the components. I noticed after a bit of debugging that after several calls to the getSchema method, the node it attempts to get a schema for {"oneOf":[{"type":"string"},{"type":"integer"}],"description":"this breaks using oneOf/anyOf/etc.","default":10} and it correctly identifies the oneOfArray as [{"type":"string"},{"type":"integer"}], but when it attempts to call getType() on the schema (ComposedSchema) at line 2260 it returns null and causes the NPE.
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
Reproduce the failure with the supplied OpenAPI JSON and Java parsing snippet, then inspect getSchema in OpenAPIDeserializer.java around line 2260. Trace how the oneOf/anyOf/allOf schema is represented and identify why getType() leads to the NullPointerException. Done means the supplied specification parses without an exception, as it did with v2.0.8.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- api, backend-api-design
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100