Skip to content

Commit

Permalink
[FLINK-16696][rest] Properly document async responses
Browse files Browse the repository at this point in the history
  • Loading branch information
zentol committed Apr 8, 2020
1 parent 446dda1 commit 2795af3
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 16 deletions.
27 changes: 24 additions & 3 deletions docs/_includes/generated/rest_v1_dispatcher.html
Original file line number Diff line number Diff line change
Expand Up @@ -2256,7 +2256,13 @@
"id" : "urn:jsonschema:org:apache:flink:runtime:rest:handler:async:AsynchronousOperationResult",
"properties" : {
"operation" : {
"type" : "any"
"type" : "object",
"id" : "urn:jsonschema:org:apache:flink:runtime:rest:handler:async:AsynchronousOperationInfo",
"properties" : {
"failure-cause" : {
"type" : "any"
}
}
},
"status" : {
"type" : "object",
Expand Down Expand Up @@ -2387,7 +2393,16 @@
"id" : "urn:jsonschema:org:apache:flink:runtime:rest:handler:async:AsynchronousOperationResult",
"properties" : {
"operation" : {
"type" : "any"
"type" : "object",
"id" : "urn:jsonschema:org:apache:flink:runtime:rest:messages:job:savepoints:SavepointInfo",
"properties" : {
"failure-cause" : {
"type" : "any"
},
"location" : {
"type" : "string"
}
}
},
"status" : {
"type" : "object",
Expand Down Expand Up @@ -3760,7 +3775,13 @@
"id" : "urn:jsonschema:org:apache:flink:runtime:rest:handler:async:AsynchronousOperationResult",
"properties" : {
"operation" : {
"type" : "any"
"type" : "object",
"id" : "urn:jsonschema:org:apache:flink:runtime:rest:handler:async:AsynchronousOperationInfo",
"properties" : {
"failure-cause" : {
"type" : "any"
}
}
},
"status" : {
"type" : "object",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.apache.flink.docs.rest;

import org.apache.flink.runtime.rest.RestServerEndpoint;
import org.apache.flink.runtime.rest.handler.async.AsynchronousOperationResult;
import org.apache.flink.runtime.rest.handler.async.AsynchronousOperationStatusMessageHeaders;
import org.apache.flink.runtime.rest.messages.EmptyRequestBody;
import org.apache.flink.runtime.rest.messages.EmptyResponseBody;
import org.apache.flink.runtime.rest.messages.MessageHeaders;
Expand All @@ -40,6 +42,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -129,11 +133,17 @@ private static void createHtmlFile(DocumentingRestEndpoint restEndpoint, RestAPI
}

private static String createHtmlEntry(MessageHeaders<?, ?, ?> spec) {
Class<?> nestedAsyncOperationResultClass = null;
if (spec instanceof AsynchronousOperationStatusMessageHeaders) {
nestedAsyncOperationResultClass = ((AsynchronousOperationStatusMessageHeaders<?, ?>) spec).getValueClass();
}
String requestEntry = createMessageHtmlEntry(
spec.getRequestClass(),
null,
EmptyRequestBody.class);
String responseEntry = createMessageHtmlEntry(
spec.getResponseClass(),
nestedAsyncOperationResultClass,
EmptyResponseBody.class);

String pathParameterList = createPathParameterHtmlList(spec.getUnresolvedMessageParameters().getPathParameters());
Expand Down Expand Up @@ -237,13 +247,12 @@ private static String createQueryParameterHtmlList(Collection<MessageQueryParame
return queryParameterList.toString();
}

private static String createMessageHtmlEntry(Class<?> messageClass, Class<?> emptyMessageClass) {
JsonSchema schema;
try {
schema = schemaGen.generateSchema(messageClass);
} catch (JsonProcessingException e) {
LOG.error("Failed to generate message schema for class {}.", messageClass, e);
throw new RuntimeException("Failed to generate message schema for class " + messageClass.getCanonicalName() + ".", e);
private static String createMessageHtmlEntry(Class<?> messageClass, @Nullable Class<?> nestedAsyncOperationResultClass, Class<?> emptyMessageClass) {
JsonSchema schema = generateSchema(messageClass);

if (nestedAsyncOperationResultClass != null) {
JsonSchema innerSchema = generateSchema(nestedAsyncOperationResultClass);
schema.asObjectSchema().getProperties().put(AsynchronousOperationResult.FIELD_NAME_OPERATION, innerSchema);
}

String json;
Expand All @@ -262,6 +271,15 @@ private static String createMessageHtmlEntry(Class<?> messageClass, Class<?> emp
return json;
}

private static JsonSchema generateSchema(Class<?> messageClass) {
try {
return schemaGen.generateSchema(messageClass);
} catch (JsonProcessingException e) {
LOG.error("Failed to generate message schema for class {}.", messageClass, e);
throw new RuntimeException("Failed to generate message schema for class " + messageClass.getCanonicalName() + ".", e);
}
}

/**
* Create character escapes for HTML when generating JSON request/response string.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class AsynchronousOperationResult<V> implements AsynchronouslyCreatedReso

private static final String FIELD_NAME_STATUS = "status";

private static final String FIELD_NAME_OPERATION = "operation";
public static final String FIELD_NAME_OPERATION = "operation";

@JsonProperty(FIELD_NAME_STATUS)
private final QueueStatus queueStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public abstract class AsynchronousOperationStatusMessageHeaders<V, M extends Mes
*
* @return value class
*/
protected abstract Class<V> getValueClass();
public abstract Class<V> getValueClass();

@Override
public Class<AsynchronousOperationResult<V>> getResponseClass() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class RescalingStatusHeaders extends
private RescalingStatusHeaders() {}

@Override
protected Class<AsynchronousOperationInfo> getValueClass() {
public Class<AsynchronousOperationInfo> getValueClass() {
return AsynchronousOperationInfo.class;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static SavepointDisposalStatusHeaders getInstance() {
}

@Override
protected Class<AsynchronousOperationInfo> getValueClass() {
public Class<AsynchronousOperationInfo> getValueClass() {
return AsynchronousOperationInfo.class;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static SavepointStatusHeaders getInstance() {
}

@Override
protected Class<SavepointInfo> getValueClass() {
public Class<SavepointInfo> getValueClass() {
return SavepointInfo.class;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ private static final class TestingStatusMessageHeaders extends AsynchronousOpera
private TestingStatusMessageHeaders() {}

@Override
protected Class<OperationResult> getValueClass() {
public Class<OperationResult> getValueClass() {
return OperationResult.class;
}

Expand Down

0 comments on commit 2795af3

Please sign in to comment.