Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Fix bug with empty Accept header #7535

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,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, 2023 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,10 @@
*/
package io.helidon.openapi;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Map;
import java.util.function.Consumer;
Expand Down Expand Up @@ -80,6 +83,21 @@ 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
Loading