zentol commented on a change in pull request #17498: URL: https://github.com/apache/flink/pull/17498#discussion_r730916804
########## File path: flink-docs/src/main/java/org/apache/flink/docs/rest/OpenApiSpecGenerator.java ########## @@ -0,0 +1,427 @@ +/* + * 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.flink.docs.rest; + +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.annotation.docs.Documentation; +import org.apache.flink.api.common.JobID; +import org.apache.flink.runtime.clusterframework.types.ResourceID; +import org.apache.flink.runtime.jobgraph.IntermediateDataSetID; +import org.apache.flink.runtime.jobgraph.JobVertexID; +import org.apache.flink.runtime.rest.FileUploadHandler; +import org.apache.flink.runtime.rest.HttpMethodWrapper; +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; +import org.apache.flink.runtime.rest.messages.MessagePathParameter; +import org.apache.flink.runtime.rest.messages.MessageQueryParameter; +import org.apache.flink.runtime.rest.messages.TriggerId; +import org.apache.flink.runtime.rest.messages.job.JobSubmitHeaders; +import org.apache.flink.runtime.rest.util.DocumentingDispatcherRestEndpoint; +import org.apache.flink.runtime.rest.util.DocumentingRestEndpoint; +import org.apache.flink.runtime.rest.versioning.RestAPIVersion; +import org.apache.flink.runtime.util.EnvironmentInformation; +import org.apache.flink.runtime.webmonitor.handlers.JarUploadHeaders; +import org.apache.flink.util.ConfigurationException; + +import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.SerializationFeature; + +import io.swagger.v3.core.converter.AnnotatedType; +import io.swagger.v3.core.converter.ModelConverterContext; +import io.swagger.v3.core.converter.ModelConverterContextImpl; +import io.swagger.v3.core.jackson.ModelResolver; +import io.swagger.v3.core.util.Yaml; +import io.swagger.v3.oas.models.Components; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.Operation; +import io.swagger.v3.oas.models.PathItem; +import io.swagger.v3.oas.models.info.Contact; +import io.swagger.v3.oas.models.info.Info; +import io.swagger.v3.oas.models.info.License; +import io.swagger.v3.oas.models.media.ArraySchema; +import io.swagger.v3.oas.models.media.ComposedSchema; +import io.swagger.v3.oas.models.media.Content; +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 io.swagger.v3.oas.models.parameters.RequestBody; +import io.swagger.v3.oas.models.responses.ApiResponse; +import io.swagger.v3.oas.models.responses.ApiResponses; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.stream.Collectors; + +/** + * OpenAPI spec generator for the Rest API. + * + * <p>One OpenAPI yml file is generated for each {@link RestServerEndpoint} implementation that can + * be embedded into .md files using {@code {% include ${generated.docs.dir}/file.yml %}}. + */ +public class OpenApiSpecGenerator { + + private static final Logger LOG = LoggerFactory.getLogger(OpenApiSpecGenerator.class); + + private static final ModelConverterContext modelConverterContext; + + static { + final ObjectMapper mapper = + new ObjectMapper().configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true); + modelConverterContext = + new ModelConverterContextImpl(Collections.singletonList(new ModelResolver(mapper))); + } + + /** + * Generates the REST API OpenAPI spec. + * + * @param args args[0] contains the directory into which the generated files are placed + * @throws IOException if any file operation failed + */ + public static void main(String[] args) throws IOException, ConfigurationException { + String outputDirectory = args[0]; + + for (final RestAPIVersion apiVersion : RestAPIVersion.values()) { + if (apiVersion == RestAPIVersion.V0) { + // this version exists only for testing purposes + continue; + } + createDocumentationFile( + new DocumentingDispatcherRestEndpoint(), + apiVersion, + Paths.get( + outputDirectory, + "rest_" + apiVersion.getURLVersionPrefix() + "_dispatcher.yml")); + } + } + + @VisibleForTesting + static void createDocumentationFile( + DocumentingRestEndpoint restEndpoint, RestAPIVersion apiVersion, Path outputFile) + throws IOException { + final OpenAPI openApi = new OpenAPI(); + + // eagerly initialize some data-structures to simplify operations later on + openApi.setPaths(new io.swagger.v3.oas.models.Paths()); + openApi.setComponents(new Components()); + + setInfo(openApi, apiVersion); + + List<MessageHeaders> specs = + restEndpoint.getSpecs().stream() + .filter(spec -> spec.getSupportedAPIVersions().contains(apiVersion)) + .filter(OpenApiSpecGenerator::shouldBeDocumented) + .collect(Collectors.toList()); + specs.forEach(spec -> add(spec, openApi)); + + final List<Schema> asyncOperationSchemas = collectAsyncOperationResultVariants(specs); Review comment: Unfortunately we can't because `ComposedCheme#oneOf` only accepts a List<Schema>. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
