Skip to content

Commit

Permalink
2.x: Fix bug with empty Accept header (#7536) (#8696)
Browse files Browse the repository at this point in the history
  • Loading branch information
Captain1653 authored May 1, 2024
1 parent 8be9b66 commit f4b8eed
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
4 changes: 2 additions & 2 deletions openapi/src/main/java/io/helidon/openapi/OpenAPISupport.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
* Copyright (c) 2020, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -520,7 +520,7 @@ private Optional<MediaType> chooseResponseMediaType(ServerRequest req) {
*/
RequestHeaders headers = req.headers();
if (headers.acceptedTypes().isEmpty()) {
headers.add(Http.Header.ACCEPT, DEFAULT_RESPONSE_MEDIA_TYPE.toString());
return Optional.of(DEFAULT_RESPONSE_MEDIA_TYPE);
}
return headers
.bestAccepted(preferredMediaTypeOrdering);
Expand Down
20 changes: 19 additions & 1 deletion openapi/src/test/java/io/helidon/openapi/ServerTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2022 Oracle and/or its affiliates.
* Copyright (c) 2019, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,9 @@
*/
package io.helidon.openapi;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Map;
import java.util.function.Consumer;
Expand Down Expand Up @@ -78,6 +80,22 @@ public static void shutdown() {
TestUtil.shutdownServer(timeWebServer);
}

@Test
void testWithEmptyAccept() throws IOException {
URL url = new URL("http://localhost:" + greetingWebServer.port() + GREETING_PATH);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", ""); // Yes, to test a bug fix specify Accept with nothing.
Map<String, Object> openAPIDocument = TestUtil.yamlFromResponse(conn);

// Check one simple value.
ArrayList<Map<String, Object>> servers = TestUtil.as(
ArrayList.class, openAPIDocument.get("servers"));
Map<String, Object> server = servers.get(0);
assertThat("unexpected URL", server.get("url"), is("http://localhost:8000"));
assertThat("unexpected description", server.get("description"), is("Local test server"));
}


/**
* Accesses the OpenAPI endpoint, requesting a YAML response payload, and
Expand Down

0 comments on commit f4b8eed

Please sign in to comment.