From a455a816fe394ecc0a3bde7cf14df9e15738e7b2 Mon Sep 17 00:00:00 2001 From: Tim Quinn Date: Wed, 6 Sep 2023 18:48:15 -0500 Subject: [PATCH 1/2] Fix bug with empty Accept header --- .../io/helidon/openapi/OpenAPISupport.java | 2 +- .../java/io/helidon/openapi/ServerTest.java | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/openapi/src/main/java/io/helidon/openapi/OpenAPISupport.java b/openapi/src/main/java/io/helidon/openapi/OpenAPISupport.java index 20fab27e2d9..222f1313580 100644 --- a/openapi/src/main/java/io/helidon/openapi/OpenAPISupport.java +++ b/openapi/src/main/java/io/helidon/openapi/OpenAPISupport.java @@ -549,7 +549,7 @@ private Optional 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); diff --git a/openapi/src/test/java/io/helidon/openapi/ServerTest.java b/openapi/src/test/java/io/helidon/openapi/ServerTest.java index e16cf0ef4a0..8e8553bb0fc 100644 --- a/openapi/src/test/java/io/helidon/openapi/ServerTest.java +++ b/openapi/src/test/java/io/helidon/openapi/ServerTest.java @@ -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; @@ -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 openAPIDocument = TestUtil.yamlFromResponse(conn); + + // Check one simple value. + ArrayList> servers = TestUtil.as( + ArrayList.class, openAPIDocument.get("servers")); + Map 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 From 616d616ec1fb0255e7a0d92ecc0043af6d1613de Mon Sep 17 00:00:00 2001 From: Tim Quinn Date: Wed, 6 Sep 2023 19:50:52 -0500 Subject: [PATCH 2/2] Copyright --- openapi/src/test/java/io/helidon/openapi/ServerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openapi/src/test/java/io/helidon/openapi/ServerTest.java b/openapi/src/test/java/io/helidon/openapi/ServerTest.java index 8e8553bb0fc..2486bfe6045 100644 --- a/openapi/src/test/java/io/helidon/openapi/ServerTest.java +++ b/openapi/src/test/java/io/helidon/openapi/ServerTest.java @@ -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.