From 7638c7daef3b0b5ac746b28e6da9c36e7f1ab1ef Mon Sep 17 00:00:00 2001 From: Isuru Wijesiri Date: Wed, 18 Oct 2023 10:26:18 +0530 Subject: [PATCH] Upgrade snakeyaml This commit adds following changes 1. Upgarde snakeyaml to v2.2 2. Remove snakeyaml orbit dependency 3. Upgrade jakson dependencies to v2.15.2 4. Fixes for test failures --- .../pom.xml | 4 +- .../swagger/format/MediaTypeMixin.java | 66 +++++++++++++++++++ .../swagger/format/SwaggerGenerator.java | 56 +++++++++++++++- .../pom.xml | 4 +- pom.xml | 11 ++-- 5 files changed, 130 insertions(+), 11 deletions(-) create mode 100644 components/mediation/extensions/org.wso2.micro.integrator.transport.handlers/src/main/java/org/wso2/micro/integrator/transport/handlers/requestprocessors/swagger/format/MediaTypeMixin.java diff --git a/components/mediation/extensions/org.wso2.micro.integrator.transport.handlers/pom.xml b/components/mediation/extensions/org.wso2.micro.integrator.transport.handlers/pom.xml index a796cabfbc..823f56d946 100644 --- a/components/mediation/extensions/org.wso2.micro.integrator.transport.handlers/pom.xml +++ b/components/mediation/extensions/org.wso2.micro.integrator.transport.handlers/pom.xml @@ -53,7 +53,7 @@ javax.servlet.http;version="${imp.pkg.version.javax.servlet}", org.apache.commons.logging; version="${import.package.version.commons.logging}", org.apache.axis2.*; version="${imp.pkg.version.axis2}", - org.yaml.snakeyaml.*; version="${orbit.snakeyaml.import.version.range}", + org.yaml.snakeyaml.*; version="${snakeyaml.import.version.range}", org.wso2.micro.core.*;version="${project.version}", *;resolution:=optional @@ -75,7 +75,7 @@ synapse-core - org.wso2.orbit.org.yaml + org.yaml snakeyaml diff --git a/components/mediation/extensions/org.wso2.micro.integrator.transport.handlers/src/main/java/org/wso2/micro/integrator/transport/handlers/requestprocessors/swagger/format/MediaTypeMixin.java b/components/mediation/extensions/org.wso2.micro.integrator.transport.handlers/src/main/java/org/wso2/micro/integrator/transport/handlers/requestprocessors/swagger/format/MediaTypeMixin.java new file mode 100644 index 0000000000..e345ab634b --- /dev/null +++ b/components/mediation/extensions/org.wso2.micro.integrator.transport.handlers/src/main/java/org/wso2/micro/integrator/transport/handlers/requestprocessors/swagger/format/MediaTypeMixin.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 LLC. 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.wso2.micro.integrator.transport.handlers.requestprocessors.swagger.format; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import io.swagger.v3.oas.models.parameters.Parameter; + +import java.util.Set; + +public abstract class MediaTypeMixin { + public MediaTypeMixin() { + + } + + @JsonInclude(JsonInclude.Include.NON_NULL) + public abstract Object getExample(); + + public void setExample(Object example) { + if (example != null) { + setExampleSetFlag(true); + } + } + + @JsonIgnore + abstract boolean getExampleSetFlag(); + + @JsonIgnore + public abstract void setExampleSetFlag(boolean exampleSetFlag); + + public abstract String getType(); + + @JsonIgnore + public abstract Set getTypes(); + + @JsonIgnore + public boolean shouldIgnoreTypes() { + return getType() != null; + } + + @JsonIgnore + public boolean shouldSerializeTypes() { + return getTypes() != null && !shouldIgnoreTypes(); + } + + @JsonIgnore + public abstract Parameter.StyleEnum getStyle(); + + @JsonIgnore + public abstract void setStyle(Parameter.StyleEnum style); +} diff --git a/components/mediation/extensions/org.wso2.micro.integrator.transport.handlers/src/main/java/org/wso2/micro/integrator/transport/handlers/requestprocessors/swagger/format/SwaggerGenerator.java b/components/mediation/extensions/org.wso2.micro.integrator.transport.handlers/src/main/java/org/wso2/micro/integrator/transport/handlers/requestprocessors/swagger/format/SwaggerGenerator.java index 3033274207..853c86b70e 100644 --- a/components/mediation/extensions/org.wso2.micro.integrator.transport.handlers/src/main/java/org/wso2/micro/integrator/transport/handlers/requestprocessors/swagger/format/SwaggerGenerator.java +++ b/components/mediation/extensions/org.wso2.micro.integrator.transport.handlers/src/main/java/org/wso2/micro/integrator/transport/handlers/requestprocessors/swagger/format/SwaggerGenerator.java @@ -17,6 +17,18 @@ */ package org.wso2.micro.integrator.transport.handlers.requestprocessors.swagger.format; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import io.swagger.v3.core.util.Json; +import io.swagger.v3.core.util.Yaml; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.media.MediaType; +import io.swagger.v3.oas.models.media.Schema; +import io.swagger.v3.oas.models.parameters.Parameter; import org.apache.axiom.ext.io.StreamCopyException; import org.apache.axiom.util.blob.BlobOutputStream; import org.apache.axis2.AxisFault; @@ -52,8 +64,14 @@ public class SwaggerGenerator { */ protected void updateResponse(CarbonHttpResponse response, String responseString, String contentType) throws AxisFault { + String updatesResponseString = getOpenAPIJsonString(responseString, contentType); try { - byte[] responseStringBytes = responseString.getBytes(SwaggerConstants.DEFAULT_ENCODING); + if (updatesResponseString == null) { + // Parsing to OpenAPI model failed. Return the original response. + // This can happen when user save a custom swagger in registry. + updatesResponseString = responseString; + } + byte[] responseStringBytes = updatesResponseString.getBytes(SwaggerConstants.DEFAULT_ENCODING); ((BlobOutputStream) response.getOutputStream()).getBlob() .readFrom(new ByteArrayInputStream(responseStringBytes), responseStringBytes.length); } catch (StreamCopyException streamCopyException) { @@ -109,4 +127,40 @@ protected void handleException(String errorMsg) throws AxisFault { log.error(errorMsg); throw new AxisFault(errorMsg); } + + /** + * Ignore example:null from the response + * + * @param responseString String response to be updated in response + * @param contentType Content type of the response to be updated in response headers + */ + public static String getOpenAPIJsonString(String responseString, String contentType) { + if (contentType.contains(SwaggerConstants.CONTENT_TYPE_YAML)) { + try { + JsonNode yamlMapper = new ObjectMapper(new YAMLFactory()).readTree(responseString); + responseString = Json.mapper().writeValueAsString(yamlMapper); + } catch (JsonProcessingException e) { + log.error("Error while converting a yaml definition to a json " + e.getMessage()); + } + } + ObjectMapper mapper = new ObjectMapper(); + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + mapper.enable(SerializationFeature.INDENT_OUTPUT); + // this is to ignore "example" and "exampleSetFlag" objects when generating the OpenAPI + mapper.addMixIn(Parameter.class, MediaTypeMixin.class); + mapper.addMixIn(MediaType.class, MediaTypeMixin.class); + mapper.addMixIn(Schema.class, MediaTypeMixin.class); + try { + OpenAPI openAPI = mapper.readValue(responseString, OpenAPI.class); + String updatedResponseString = mapper.writeValueAsString(openAPI); + if (contentType.contains(SwaggerConstants.CONTENT_TYPE_JSON)) { + return updatedResponseString; + } + JsonNode jsonNodeTree = new ObjectMapper().readTree(updatedResponseString); + return Yaml.mapper().writeValueAsString(jsonNodeTree); + } catch (JsonProcessingException e) { + log.error("Error while generating Swagger JSON from model", e); + return null; + } + } } diff --git a/features/org.wso2.micro.integrator.initializer.feature/pom.xml b/features/org.wso2.micro.integrator.initializer.feature/pom.xml index 4098e2f2d2..0091de7083 100644 --- a/features/org.wso2.micro.integrator.initializer.feature/pom.xml +++ b/features/org.wso2.micro.integrator.initializer.feature/pom.xml @@ -55,7 +55,7 @@ zip - org.wso2.orbit.org.yaml + org.yaml snakeyaml @@ -96,7 +96,7 @@ org.wso2.ei:org.wso2.micro.integrator.registry:${project.version} - org.wso2.orbit.org.yaml:snakeyaml:${orbit.version.snakeyaml} + org.yaml:snakeyaml:${snakeyaml.version} org.apache.synapse.wso2:${synapse.version} diff --git a/pom.xml b/pom.xml index 83c0ef3a5f..53b1ce8b01 100644 --- a/pom.xml +++ b/pom.xml @@ -556,9 +556,9 @@ provided - org.wso2.orbit.org.yaml + org.yaml snakeyaml - ${orbit.version.snakeyaml} + ${snakeyaml.version} net.minidev @@ -1588,7 +1588,7 @@ 5.2.52 [5.2.0, 6.0.0) - 2.15.0 + 2.15.2 4.7.49 4.5.3 4.9.11 @@ -1672,7 +1672,6 @@ 2.3.2.wso2v1 1.2.4 4.2.0 - 2.0.0.wso2v1 2.4.11 31.0.1-jre 2.1.0.wso2v1 @@ -1693,7 +1692,7 @@ [0.0.0, 1.0.0) [2.6.0, 3.0.0) [1.2.0,2.0.0) - [2.0.0,3.0.0) + [2.0.0,3.0.0) [1.2.11, 1.3.0) [1.2.0,2.0.0) @@ -1868,7 +1867,7 @@ 2.0.30.wso2v1 1.6.5 - 2.0 + 2.2 1.1.0.Final 3.4.2 1.1