flink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ches...@apache.org
Subject [12/18] flink git commit: [FLINK-9104][docs] Update generator and regenerate REST API docs
Date Wed, 04 Apr 2018 12:22:30 GMT
http://git-wip-us.apache.org/repos/asf/flink/blob/83fffdf3/flink-docs/src/main/java/org/apache/flink/docs/rest/RestAPIDocGenerator.java
----------------------------------------------------------------------
diff --git a/flink-docs/src/main/java/org/apache/flink/docs/rest/RestAPIDocGenerator.java
b/flink-docs/src/main/java/org/apache/flink/docs/rest/RestAPIDocGenerator.java
index be5f677..8ece7b1 100644
--- a/flink-docs/src/main/java/org/apache/flink/docs/rest/RestAPIDocGenerator.java
+++ b/flink-docs/src/main/java/org/apache/flink/docs/rest/RestAPIDocGenerator.java
@@ -20,6 +20,7 @@ package org.apache.flink.docs.rest;
 
 import org.apache.flink.api.java.tuple.Tuple2;
 import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.RestOptions;
 import org.apache.flink.runtime.concurrent.Executors;
 import org.apache.flink.runtime.dispatcher.DispatcherGateway;
 import org.apache.flink.runtime.dispatcher.DispatcherRestEndpoint;
@@ -43,6 +44,9 @@ import org.apache.flink.util.ConfigurationException;
 import org.apache.flink.shaded.netty4.io.netty.channel.ChannelInboundHandler;
 
 import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.SerializableString;
+import com.fasterxml.jackson.core.io.CharacterEscapes;
+import com.fasterxml.jackson.core.io.SerializedString;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
 import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator;
@@ -56,7 +60,9 @@ import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.Collection;
 import java.util.Comparator;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.UUID;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.Executor;
@@ -99,6 +105,7 @@ public class RestAPIDocGenerator {
 
 	static {
 		mapper = new ObjectMapper();
+		mapper.getFactory().setCharacterEscapes(new HTMLCharacterEscapes());
 		schemaGen = new JsonSchemaGenerator(mapper);
 	}
 
@@ -259,6 +266,39 @@ public class RestAPIDocGenerator {
 	}
 
 	/**
+	 * Create character escapes for HTML when generating JSON request/response string.
+	 *
+	 * <p>This is to avoid exception when generating JSON with Field schema contains generic
types.
+	 */
+	private static class HTMLCharacterEscapes extends CharacterEscapes {
+		private final int[] asciiEscapes;
+		private final Map<Integer, SerializableString> escapeSequences;
+
+		public HTMLCharacterEscapes() {
+			int[] esc = CharacterEscapes.standardAsciiEscapesForJSON();
+			esc['<'] = CharacterEscapes.ESCAPE_CUSTOM;
+			esc['>'] = CharacterEscapes.ESCAPE_CUSTOM;
+			esc['&'] = CharacterEscapes.ESCAPE_CUSTOM;
+			Map<Integer, SerializableString> escMap = new HashMap<>();
+			escMap.put((int) '<', new SerializedString("&lt;"));
+			escMap.put((int) '>', new SerializedString("&gt;"));
+			escMap.put((int) '&', new SerializedString("&amp;"));
+			asciiEscapes = esc;
+			escapeSequences = escMap;
+		}
+
+		@Override
+		public int[] getEscapeCodesForAscii() {
+			return asciiEscapes;
+		}
+
+		@Override
+		public SerializableString getEscapeSequence(int i) {
+			return escapeSequences.getOrDefault(i, null);
+		}
+	}
+
+	/**
 	 * Utility class to extract the {@link MessageHeaders} that the {@link DispatcherRestEndpoint}
supports.
 	 */
 	private static class DocumentingDispatcherRestEndpoint extends DispatcherRestEndpoint implements
DocumentingRestEndpoint {
@@ -273,6 +313,7 @@ public class RestAPIDocGenerator {
 
 		static {
 			config = new Configuration();
+			config.setString(RestOptions.REST_ADDRESS, "localhost");
 			try {
 				restConfig = RestServerEndpointConfiguration.fromConfiguration(config);
 			} catch (ConfigurationException e) {


Mime
View raw message