This is an automated email from the ASF dual-hosted git repository. jamesnetherton pushed a commit to branch camel-3.11.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.11.x by this push: new c6d1664 CAMEL-16923: Fix NPE when configuring OpenAPI license & contact info properties c6d1664 is described below commit c6d1664ada67d1516a00be993831c6aaa419f184 Author: James Netherton <jamesnether...@gmail.com> AuthorDate: Fri Sep 3 15:58:29 2021 +0100 CAMEL-16923: Fix NPE when configuring OpenAPI license & contact info properties --- .../org/apache/camel/openapi/OpenApiHelper.java | 3 +- .../apache/camel/openapi/RestOpenApiSupport.java | 16 ++--- .../camel/openapi/RestOpenApiLicenseInfoTest.java | 71 ++++++++++++++++++++++ 3 files changed, 79 insertions(+), 11 deletions(-) diff --git a/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/OpenApiHelper.java b/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/OpenApiHelper.java index 10e945c..958ddf6 100644 --- a/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/OpenApiHelper.java +++ b/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/OpenApiHelper.java @@ -53,7 +53,8 @@ public final class OpenApiHelper { if (openApi instanceof Oas20Document) { openApi.clearExtensions(); - if (((Oas20Document) openApi).definitions.getDefinitions() != null) { + if (((Oas20Document) openApi).definitions != null + && ((Oas20Document) openApi).definitions.getDefinitions() != null) { for (Oas20SchemaDefinition schemaDefinition : ((Oas20Document) openApi).definitions.getDefinitions()) { schemaDefinition.clearExtensions(); } diff --git a/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiSupport.java b/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiSupport.java index 97ed492..be4675c 100644 --- a/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiSupport.java +++ b/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiSupport.java @@ -31,15 +31,13 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; +import io.apicurio.datamodels.core.models.common.Contact; +import io.apicurio.datamodels.core.models.common.License; import io.apicurio.datamodels.openapi.models.OasDocument; -import io.apicurio.datamodels.openapi.v2.models.Oas20Contact; import io.apicurio.datamodels.openapi.v2.models.Oas20Document; import io.apicurio.datamodels.openapi.v2.models.Oas20Info; -import io.apicurio.datamodels.openapi.v2.models.Oas20License; -import io.apicurio.datamodels.openapi.v3.models.Oas30Contact; import io.apicurio.datamodels.openapi.v3.models.Oas30Document; import io.apicurio.datamodels.openapi.v3.models.Oas30Info; -import io.apicurio.datamodels.openapi.v3.models.Oas30License; import io.apicurio.datamodels.openapi.v3.models.Oas30Server; import org.apache.camel.CamelContext; import org.apache.camel.Exchange; @@ -312,19 +310,18 @@ public class RestOpenApiSupport { info.termsOfService = termsOfService; if (licenseName != null || licenseUrl != null) { - Oas30License license = new Oas30License(); + License license = info.createLicense(); license.name = licenseName; license.url = licenseUrl; info.license = license; } if (contactName != null || contactUrl != null || contactEmail != null) { - Oas30Contact contact = new Oas30Contact(); + Contact contact = info.createContact(); contact.name = contactName; contact.url = contactUrl; contact.email = contactEmail; info.contact = contact; - contact._parent = info; } openApiConfig.setInfo(info); } @@ -340,19 +337,18 @@ public class RestOpenApiSupport { info.termsOfService = termsOfService; if (licenseName != null || licenseUrl != null) { - Oas20License license = new Oas20License(); + License license = info.createLicense(); license.name = licenseName; license.url = licenseUrl; info.license = license; } if (contactName != null || contactUrl != null || contactEmail != null) { - Oas20Contact contact = new Oas20Contact(); + Contact contact = info.createContact(); contact.name = contactName; contact.url = contactUrl; contact.email = contactEmail; info.contact = contact; - contact._parent = info; } openApiConfig.setInfo(info); } diff --git a/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiLicenseInfoTest.java b/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiLicenseInfoTest.java new file mode 100644 index 0000000..b9ef5ad --- /dev/null +++ b/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiLicenseInfoTest.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.openapi; + +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.spi.RestConfiguration; +import org.apache.camel.support.DefaultExchange; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class RestOpenApiLicenseInfoTest { + + @ParameterizedTest + @ValueSource(strings = { "3.0", "2.0" }) + public void testLicenseInfo(String openApiVersion) throws Exception { + CamelContext context = new DefaultCamelContext(); + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + restConfiguration() + .apiProperty("openapi.version", openApiVersion) + .apiProperty("api.contact.name", "Mr Camel") + .apiProperty("api.contact.email", "ca...@apache.org") + .apiProperty("api.contact.url", "https://camel.apache.org") + .apiProperty("api.license.name", "Apache V2") + .apiProperty("api.license.url", "https://www.apache.org/licenses/LICENSE-2.0"); + + rest("/api") + .get("/api") + .route() + .setBody().constant("Hello World") + .end(); + } + }); + + RestConfiguration restConfiguration = context.getRestConfiguration(); + RestOpenApiProcessor processor + = new RestOpenApiProcessor(null, false, restConfiguration.getApiProperties(), restConfiguration); + Exchange exchange = new DefaultExchange(context); + processor.process(exchange); + + String json = exchange.getMessage().getBody(String.class); + assertNotNull(json); + + assertTrue(json.contains("\"url\" : \"https://www.apache.org/licenses/LICENSE-2.0\"")); + assertTrue(json.contains("\"name\" : \"Apache V2\"")); + assertTrue(json.contains("\"name\" : \"Mr Camel\"")); + assertTrue(json.contains("\"email\" : \"ca...@apache.org\"")); + assertTrue(json.contains("\"url\" : \"https://camel.apache.org\"")); + } +}