From acf1a915e7bc81f65d16d18b099168f8cc5f6522 Mon Sep 17 00:00:00 2001 From: Ralf Ueberfuhr <40685729+ueberfuhr@users.noreply.github.com> Date: Thu, 30 May 2024 07:54:28 +0200 Subject: [PATCH] Fix: NPE in case of response header without required attribute (#1171) * fix: NPE in case of response header without required attribute in OpenAPI * Remove @author tag * Add test for Oas30ModelHelper.getRequiredHeaders(..) --------- Co-authored-by: Ralf Ueberfuhr --- .../openapi/model/v3/Oas30ModelHelper.java | 2 +- .../model/v3/Oas30ModelHelperTest.java | 54 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 connectors/citrus-openapi/src/test/java/org/citrusframework/openapi/model/v3/Oas30ModelHelperTest.java diff --git a/connectors/citrus-openapi/src/main/java/org/citrusframework/openapi/model/v3/Oas30ModelHelper.java b/connectors/citrus-openapi/src/main/java/org/citrusframework/openapi/model/v3/Oas30ModelHelper.java index 614b2de512..39bdd2486e 100644 --- a/connectors/citrus-openapi/src/main/java/org/citrusframework/openapi/model/v3/Oas30ModelHelper.java +++ b/connectors/citrus-openapi/src/main/java/org/citrusframework/openapi/model/v3/Oas30ModelHelper.java @@ -219,7 +219,7 @@ public static Map getRequiredHeaders(Oas30Response response) return response.headers.entrySet() .stream() - .filter(entry -> entry.getValue().required) + .filter(entry -> Boolean.TRUE.equals(entry.getValue().required)) .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().schema)); } diff --git a/connectors/citrus-openapi/src/test/java/org/citrusframework/openapi/model/v3/Oas30ModelHelperTest.java b/connectors/citrus-openapi/src/test/java/org/citrusframework/openapi/model/v3/Oas30ModelHelperTest.java new file mode 100644 index 0000000000..38d8b13fde --- /dev/null +++ b/connectors/citrus-openapi/src/test/java/org/citrusframework/openapi/model/v3/Oas30ModelHelperTest.java @@ -0,0 +1,54 @@ +package org.citrusframework.openapi.model.v3; + +import io.apicurio.datamodels.openapi.models.OasSchema; +import io.apicurio.datamodels.openapi.v3.models.Oas30Header; +import io.apicurio.datamodels.openapi.v3.models.Oas30Response; +import io.apicurio.datamodels.openapi.v3.models.Oas30Schema; +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.util.Map; + +public class Oas30ModelHelperTest { + + @Test + public void shouldNotFindRequiredHeadersWithoutRequiredAttribute() { + var header = new Oas30Header("X-TEST"); + header.schema = new Oas30Schema(); + header.required = null; // explicitely assigned because this is test case + var response = new Oas30Response("200"); + response.headers.put(header.getName(), header); + + Map result = Oas30ModelHelper.getRequiredHeaders(response); + + Assert.assertEquals(result.size(), 0); + } + + @Test + public void shouldFindRequiredHeaders() { + var header = new Oas30Header("X-TEST"); + header.schema = new Oas30Schema(); + header.required = Boolean.TRUE; // explicitely assigned because this is test case + var response = new Oas30Response("200"); + response.headers.put(header.getName(), header); + + Map result = Oas30ModelHelper.getRequiredHeaders(response); + + Assert.assertEquals(result.size(), 1); + Assert.assertSame(result.get(header.getName()), header.schema); + } + + @Test + public void shouldNotFindOptionalHeaders() { + var header = new Oas30Header("X-TEST"); + header.schema = new Oas30Schema(); + header.required = Boolean.FALSE; // explicitely assigned because this is test case + var response = new Oas30Response("200"); + response.headers.put(header.getName(), header); + + Map result = Oas30ModelHelper.getRequiredHeaders(response); + + Assert.assertEquals(result.size(), 0); + } + +} \ No newline at end of file