networknt / networknt/openapi-parser

NullPointerException when trying to parse OAS3 from a String

Open
#45 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
13
Forks
9
PR merge metrics
No merged PRs in 30d

Description

The KaiZen-OpenApi-Parser documentation sounds as if the "resolutionBase" can be left empty if the OAS3 is fully self-contained and no relative references are used:

https://github.com/RepreZen/KaiZen-OpenApi-Parser/blob/master/API-Overview.md
OpenApi3 parse(String model, URL resolutionBase) - parse a JSON or YAML string, with the given URL used for resolving any relative references encountered in the model. If resolutionBase is null, relative references will all fail resolution.

But calling the parse method without a resolutionBase leads to a NullPointerException:

java.lang.NullPointerException: Cannot invoke "java.net.URL.toString()" because "this.docUrl" is null
	at com.networknt.jsonoverlay.ReferenceManager.getDocReference(ReferenceManager.java:62)
	at com.networknt.oas.OpenApiParser.parse(OpenApiParser.java:95)
	at com.networknt.oas.OpenApiParser.parse(OpenApiParser.java:85)
	at com.networknt.oas.OpenApiParser.parse(OpenApiParser.java:35)
	at com.networknt.oas.OpenApi3Parser.parse(OpenApi3Parser.java:29)
	at com.networknt.oas.OpenApi3Parser.parse(OpenApi3Parser.java:1)
	at com.networknt.oas.OpenApiParser.parse(OpenApiParser.java:28)
	at com.networknt.oas.OpenApi3Parser.parse(OpenApi3Parser.java:24)
	at com.networknt.oas.StringParseTest.testSelfContainedOasSpec(StringParseTest.java:57)

Here is a short test that I wrote:

public class StringParseTest {

	private final static String testTitle = "Test Title";
	private final static String testPath = "/test";

	private final String oas3String = //
		"openapi: 3.0.0\n" //
		+ "info:\n" //
		+ "  title: " + testTitle + "\n" //
		+ "  description: This is a description\n" //
		+ "  version: 1.0.0\n" //
		+ "servers:\n" //
		+ "  - url: https://localhost:8080\n" //
		+ "paths:\n" //
		+ "  " + testPath + ":\n" //
		+ "    get:\n" //
		+ "      description: Some endpoint\n" //
		+ "      operationId: testOps\n" //
		+ "      responses:\n" //
		+ "        '200':\n" //
		+ "          description: Successful operation\n";

	@Test
	public void testSelfContainedOasSpec() throws Exception {
		// no need to pass a resolutionBase since the spec is self contained
		OpenApi3 oas3 = new OpenApi3Parser().parse(this.oas3String, null);

		assertEquals(testTitle, oas3.getInfo().getTitle());

		Map<String, Path> paths = oas3.getPaths();
		assertEquals(1, paths.size());
		assertEquals("/test", paths.entrySet().iterator().next().getKey());
	}
}

Please also note that this is only one issue. It looks like there are more issues if I try to load more complicated OAS3 specs, like for instance here:

	@Test
	public void testYamlFileAsString() throws Exception {

		ClassLoader classLoader = ClassLoader.getSystemClassLoader();
		String oas3String;
		try (InputStream is = classLoader.getResourceAsStream("models/circular.yaml")) {
			assertNotNull(is);
			try (InputStreamReader isr = new InputStreamReader(is); BufferedReader reader = new BufferedReader(isr)) {
				oas3String = reader.lines().collect(Collectors.joining(System.lineSeparator()));
			}
		}		
		assertNotNull(oas3String);

		OpenApi3 oas3 = new OpenApi3Parser().parse(oas3String, null);
		// check oas3 object ...
	}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing StringParseTest.testSelfContainedOasSpec, then inspect ReferenceManager.java at getDocReference and the OpenApiParser.java parse overloads shown in the stack trace. The fix is done when a self-contained OAS3 string parses with a null resolutionBase and the title and path assertions pass without a NullPointerException.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.