Skip to content

Commit

Permalink
Merge pull request #2996 from IsuruMaduranga/snakeyaml
Browse files Browse the repository at this point in the history
Upgrade snakeyaml
  • Loading branch information
Isuru Wijesiri authored Oct 18, 2023
2 parents fa5f29d + 7638c7d commit 846effd
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
</Import-Package>
Expand All @@ -75,7 +75,7 @@
<artifactId>synapse-core</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.orbit.org.yaml</groupId>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<type>zip</type>
</dependency>
<dependency>
<groupId>org.wso2.orbit.org.yaml</groupId>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</dependency>
</dependencies>
Expand Down Expand Up @@ -96,7 +96,7 @@
<bundleDef>
org.wso2.ei:org.wso2.micro.integrator.registry:${project.version}
</bundleDef>
<bundleDef>org.wso2.orbit.org.yaml:snakeyaml:${orbit.version.snakeyaml}</bundleDef>
<bundleDef>org.yaml:snakeyaml:${snakeyaml.version}</bundleDef>
</bundles>
<importFeatures>
<importFeatureDef>org.apache.synapse.wso2:${synapse.version}</importFeatureDef>
Expand Down
11 changes: 5 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -556,9 +556,9 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.orbit.org.yaml</groupId>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>${orbit.version.snakeyaml}</version>
<version>${snakeyaml.version}</version>
</dependency>
<dependency>
<groupId>net.minidev</groupId>
Expand Down Expand Up @@ -1588,7 +1588,7 @@

<carbon.analytics.common.version>5.2.52</carbon.analytics.common.version>
<carbon.analytics.common.imp.pkg.version>[5.2.0, 6.0.0)</carbon.analytics.common.imp.pkg.version>
<fasterxml.jackson.version>2.15.0</fasterxml.jackson.version>
<fasterxml.jackson.version>2.15.2</fasterxml.jackson.version>
<carbon.commons.version>4.7.49</carbon.commons.version>
<carbon.rule.version>4.5.3</carbon.rule.version>
<carbon.deployment.version>4.9.11</carbon.deployment.version>
Expand Down Expand Up @@ -1672,7 +1672,6 @@
<org.wso2.orbit.sun.xml.ws.version>2.3.2.wso2v1</org.wso2.orbit.sun.xml.ws.version>
<apache.felix.scr.ds.annotations.version>1.2.4</apache.felix.scr.ds.annotations.version>
<org.wso2.carbon.caching.core.version>4.2.0</org.wso2.carbon.caching.core.version>
<orbit.version.snakeyaml>2.0.0.wso2v1</orbit.version.snakeyaml>
<json.smart.version>2.4.11</json.smart.version>
<google.guava.version>31.0.1-jre</google.guava.version>
<ca.uhn.hapi.wso2.version>2.1.0.wso2v1</ca.uhn.hapi.wso2.version>
Expand All @@ -1693,7 +1692,7 @@
<javax.xml.parsers.import.pkg.version>[0.0.0, 1.0.0)</javax.xml.parsers.import.pkg.version>
<imp.pkg.version.javax.servlet>[2.6.0, 3.0.0)</imp.pkg.version.javax.servlet>
<import.package.version.commons.logging>[1.2.0,2.0.0)</import.package.version.commons.logging>
<orbit.snakeyaml.import.version.range>[2.0.0,3.0.0)</orbit.snakeyaml.import.version.range>
<snakeyaml.import.version.range>[2.0.0,3.0.0)</snakeyaml.import.version.range>
<axiom.osgi.version.range>[1.2.11, 1.3.0)</axiom.osgi.version.range>
<imp.package.version.osgi.services>[1.2.0,2.0.0)</imp.package.version.osgi.services>

Expand Down Expand Up @@ -1868,7 +1867,7 @@
<!-- OAS3 Versions -->
<swagger.parser.orbit.version>2.0.30.wso2v1</swagger.parser.orbit.version>
<swagger.core.v2.version>1.6.5</swagger.core.v2.version>
<snakeyaml.version>2.0</snakeyaml.version>
<snakeyaml.version>2.2</snakeyaml.version>
<javax.validation.version>1.1.0.Final</javax.validation.version>
<version.disruptor>3.4.2</version.disruptor>
<version.geronimo.jta.spec>1.1</version.geronimo.jta.spec>
Expand Down

0 comments on commit 846effd

Please sign in to comment.