Skip to content

NullPointerException when trying to parse OAS3 from a String #45

Description

@r-gruber

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 ...
	}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions