From 29f26d34957217d964ab8a2f40c3c4f3a66e1aad Mon Sep 17 00:00:00 2001
From: Phillip Kruger <phillip.kruger@gmail.com>
Date: Mon, 2 Oct 2023 09:58:52 +1100
Subject: [PATCH] OpenAPI different default content type for pojo return and
 primitives

Signed-off-by: Phillip Kruger <phillip.kruger@gmail.com>
---
 bom/application/pom.xml                       |  2 +-
 .../deployment/SmallRyeOpenApiProcessor.java  |  2 ++
 .../jaxrs/DefaultContentTypeResource.java     |  6 ++++
 .../test/jaxrs/DefaultContentTypeTest.java    |  2 ++
 .../test/jaxrs/NoDefaultSecurityResource.java | 31 +++++++++++++++++++
 .../test/jaxrs/NoDefaultSecurityTest.java     | 26 ++++++++++++++++
 .../deployment/HttpSecurityProcessor.java     |  5 +--
 .../io/quarkus/it/main/OpenApiTestCase.java   |  4 ++-
 8 files changed, 74 insertions(+), 4 deletions(-)
 create mode 100644 extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/NoDefaultSecurityResource.java
 create mode 100644 extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/NoDefaultSecurityTest.java

diff --git a/bom/application/pom.xml b/bom/application/pom.xml
index b072d8b3533c9..cd0af8cbc7618 100644
--- a/bom/application/pom.xml
+++ b/bom/application/pom.xml
@@ -54,7 +54,7 @@
         <smallrye-config.version>3.3.4</smallrye-config.version>
         <smallrye-health.version>4.0.4</smallrye-health.version>
         <smallrye-metrics.version>4.0.0</smallrye-metrics.version>
-        <smallrye-open-api.version>3.6.0</smallrye-open-api.version>
+        <smallrye-open-api.version>3.6.1</smallrye-open-api.version>
         <smallrye-graphql.version>2.4.0</smallrye-graphql.version>
         <smallrye-opentracing.version>3.0.3</smallrye-opentracing.version>
         <smallrye-fault-tolerance.version>6.2.6</smallrye-fault-tolerance.version>
diff --git a/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java b/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java
index fceacd28f3e58..b2eec17ab2c7a 100644
--- a/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java
+++ b/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java
@@ -164,6 +164,8 @@ public class SmallRyeOpenApiProcessor {
     static {
         System.setProperty(io.smallrye.openapi.api.constants.OpenApiConstants.DEFAULT_PRODUCES, "application/json");
         System.setProperty(io.smallrye.openapi.api.constants.OpenApiConstants.DEFAULT_CONSUMES, "application/json");
+        System.setProperty(io.smallrye.openapi.api.constants.OpenApiConstants.DEFAULT_PRODUCES_PRIMITIVES, "plain/text");
+        System.setProperty(io.smallrye.openapi.api.constants.OpenApiConstants.DEFAULT_CONSUMES_PRIMITIVES, "plain/text");
     }
 
     @BuildStep
diff --git a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/DefaultContentTypeResource.java b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/DefaultContentTypeResource.java
index 0715898c0ecc2..ae5f2ffe4a0d0 100644
--- a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/DefaultContentTypeResource.java
+++ b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/DefaultContentTypeResource.java
@@ -9,6 +9,12 @@
 @Path("/greeting")
 public class DefaultContentTypeResource {
 
+    @GET
+    @Path("/foo")
+    public String foo() {
+        return "bar";
+    }
+
     @GET
     @Path("/hello")
     public Greeting hello() {
diff --git a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/DefaultContentTypeTest.java b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/DefaultContentTypeTest.java
index 31858d0cfbddc..379d8697955ca 100644
--- a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/DefaultContentTypeTest.java
+++ b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/DefaultContentTypeTest.java
@@ -28,6 +28,8 @@ public void testOpenApiPathAccessResource() {
                         Matchers.containsString("#/components/schemas/Greeting"))
                 .body("paths.'/greeting/hello'.get.responses.'200'.content.'application/json'.schema.$ref",
                         Matchers.containsString("#/components/schemas/Greeting"))
+                .body("paths.'/greeting/foo'.get.responses.'200'.content.'plain/text'.schema.type",
+                        Matchers.equalTo("string"))
                 .body("paths.'/greeting/hello'.post.responses.'200'.content.'application/json'.schema.$ref",
                         Matchers.containsString("#/components/schemas/Greeting"));
 
diff --git a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/NoDefaultSecurityResource.java b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/NoDefaultSecurityResource.java
new file mode 100644
index 0000000000000..8dda3dcba43b2
--- /dev/null
+++ b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/NoDefaultSecurityResource.java
@@ -0,0 +1,31 @@
+package io.quarkus.smallrye.openapi.test.jaxrs;
+
+import jakarta.ws.rs.GET;
+import jakarta.ws.rs.POST;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.Produces;
+import jakarta.ws.rs.core.MediaType;
+
+@Path("/greeting")
+public class NoDefaultSecurityResource {
+
+    @GET
+    @Path("/hello")
+    public Greeting hello() {
+        return new Greeting("Hello there");
+    }
+
+    @POST
+    @Path("/hello")
+    public Greeting hello(Greeting greeting) {
+        return greeting;
+    }
+
+    @GET
+    @Path("/goodbye")
+    @Produces(MediaType.APPLICATION_XML)
+    public Greeting byebye() {
+        return new Greeting("Good Bye !");
+    }
+
+}
diff --git a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/NoDefaultSecurityTest.java b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/NoDefaultSecurityTest.java
new file mode 100644
index 0000000000000..7fd78992bee3c
--- /dev/null
+++ b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/NoDefaultSecurityTest.java
@@ -0,0 +1,26 @@
+package io.quarkus.smallrye.openapi.test.jaxrs;
+
+import org.hamcrest.Matchers;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import io.quarkus.test.QuarkusUnitTest;
+import io.restassured.RestAssured;
+
+public class NoDefaultSecurityTest {
+    private static final String OPEN_API_PATH = "/q/openapi";
+
+    @RegisterExtension
+    static QuarkusUnitTest runner = new QuarkusUnitTest()
+            .withApplicationRoot((jar) -> jar
+                    .addClasses(NoDefaultSecurityResource.class, Greeting.class));
+
+    @Test
+    public void testOpenApiNoSecurity() {
+        RestAssured.given().queryParam("format", "JSON")
+                .when().get(OPEN_API_PATH)
+                .then()
+                .body("components.securitySchemes.SecurityScheme.type", Matchers.nullValue());
+    }
+
+}
diff --git a/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/HttpSecurityProcessor.java b/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/HttpSecurityProcessor.java
index 124bd5dc59e57..84b2aaca206dc 100644
--- a/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/HttpSecurityProcessor.java
+++ b/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/HttpSecurityProcessor.java
@@ -1,7 +1,6 @@
 package io.quarkus.vertx.http.deployment;
 
 import static io.quarkus.arc.processor.DotNames.APPLICATION_SCOPED;
-import static org.jboss.jandex.AnnotationTarget.Kind.CLASS;
 
 import java.security.Permission;
 import java.util.HashMap;
@@ -216,7 +215,9 @@ SyntheticBeanBuildItem initBasicAuth(
                 && !buildTimeConfig.auth.basic.orElse(false)) {
             //if not explicitly enabled we make this a default bean, so it is the fallback if nothing else is defined
             configurator.defaultBean();
-            securityInformationProducer.produce(SecurityInformationBuildItem.BASIC());
+            if (buildTimeConfig.auth.basic.isPresent() && buildTimeConfig.auth.basic.get()) {
+                securityInformationProducer.produce(SecurityInformationBuildItem.BASIC());
+            }
         }
 
         return configurator.done();
diff --git a/integration-tests/main/src/test/java/io/quarkus/it/main/OpenApiTestCase.java b/integration-tests/main/src/test/java/io/quarkus/it/main/OpenApiTestCase.java
index 501f425922b7d..c8dd713012e78 100644
--- a/integration-tests/main/src/test/java/io/quarkus/it/main/OpenApiTestCase.java
+++ b/integration-tests/main/src/test/java/io/quarkus/it/main/OpenApiTestCase.java
@@ -22,6 +22,7 @@
 public class OpenApiTestCase {
 
     private static final String DEFAULT_MEDIA_TYPE = "application/json";
+    private static final String DEFAULT_MEDIA_TYPE_PRIMITAVE = "plain/text";
 
     @TestHTTPResource("q/openapi")
     URL uri;
@@ -63,7 +64,8 @@ public void testOpenAPIJSON() throws Exception {
         // test RESTEasy extensions
 
         JsonObject schemasObj = obj.getJsonObject("components").getJsonObject("schemas");
-        String testSchemaType = schemaType("200", DEFAULT_MEDIA_TYPE, testObj.getJsonObject("get").getJsonObject("responses"),
+        String testSchemaType = schemaType("200", DEFAULT_MEDIA_TYPE_PRIMITAVE,
+                testObj.getJsonObject("get").getJsonObject("responses"),
                 schemasObj);
         String rxSchemaType = schemaType("200", DEFAULT_MEDIA_TYPE,
                 injectionObj.getJsonObject("get").getJsonObject("responses"),