Skip to content

Commit

Permalink
Fix: NPE in case of response header without required attribute (#1171)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
ueberfuhr and Ralf Ueberfuhr authored May 30, 2024
1 parent d09aaa0 commit acf1a91
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public static Map<String, OasSchema> 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));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, OasSchema> 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<String, OasSchema> 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<String, OasSchema> result = Oas30ModelHelper.getRequiredHeaders(response);

Assert.assertEquals(result.size(), 0);
}

}

0 comments on commit acf1a91

Please sign in to comment.