Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed 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 com.twosigma.znai.openapi;
import com.twosigma.znai.parser.MarkupParserResult;
import com.twosigma.znai.parser.commonmark.MarkdownParser;
import com.twosigma.znai.utils.JsonUtils;
import com.twosigma.znai.utils.YamlUtils;
import java.nio.file.Paths;
import java.util.*;
import static java.util.stream.Collectors.toList;
public class OpenApiSpec {
private static final String DESCRIPTION_KEY = "description";
private static final String REF_KEY = "$ref";
private static final String ALL_OFF_KEY = "allOf";
private final String basePath;
private MarkdownParser markdownParser;
private Map spec;
private List operations;
/**
* create open api spec representation from json.
* Markdown defaultParser is required explicitly as open api defines description in common mark.
*
* @param markdownParser instance of markdown defaultParser
* @param jsonSpec open api specification
* @return open api spec
*/
public static OpenApiSpec fromJson(MarkdownParser markdownParser, String jsonSpec) {
return new OpenApiSpec(markdownParser, JsonUtils.deserializeAsMap(jsonSpec));
}
/**
* create open api spec representation from yaml.
* Markdown defaultParser is required explicitly as open api defines description in common mark.
*
* @param markdownParser instance of markdown defaultParser
* @param yamlSpec open api specification
* @return open api spec
*/
public static OpenApiSpec fromYaml(MarkdownParser markdownParser, String yamlSpec) {
return new OpenApiSpec(markdownParser, YamlUtils.deserializeAsMap(yamlSpec));
}
public OpenApiSpec(MarkdownParser markdownParser, Map spec) {
this.markdownParser = markdownParser;
this.spec = spec;
this.basePath = extractBasePath(spec);
this.operations = new ArrayList<>();
parse();
}
public OpenApiOperation findOperationById(String operationId) {
return operations.stream().filter(o -> operationId.equals(o.getId())).findFirst()
.orElseThrow(() -> new RuntimeException("cannot find operation: " + operationId));
}
public OpenApiOperation findOperationByMethodAndPath(String method, String path) {
return operations.stream().filter(o -> o.matches(method, path)).findFirst()
.orElseThrow(() -> new RuntimeException("cannot find operation: " + method + " " + path));
}
public List findOperationsByTags(List tags) {
return operations.stream().filter(o -> o.hasTags(tags)).collect(toList());
}
public List getOperations() {
return operations;
}
@SuppressWarnings("unchecked")
private void parse() {
parsePaths((Map) spec.get("paths"));
}
@SuppressWarnings("unchecked")
private void parsePaths(Map paths) {
if (paths == null) {
throw new IllegalArgumentException("no paths definition found in the spec");
}
paths.forEach((path, methods) -> parseMethods(path, (Map) methods));
}
@SuppressWarnings("unchecked")
private void parseMethods(String path, Map methods) {
methods.forEach((method, definition) -> parseMethod(path, method, (Map) definition));
}
@SuppressWarnings("unchecked")
private void parseMethod(String path, String method, Map definition) {
OpenApiOperation operation = new OpenApiOperation();
operation.setId(Objects.toString(definition.get("operationId")));
operation.setTags((List) definition.get("tags"));
operation.setPath(fullPath(path));
operation.setMethod(method);
operation.setConsumes(listOrDefault(definition, "consumes"));
operation.setProduces(listOrDefault(definition, "produces"));
operation.setSummary(Objects.toString(definition.get("summary")));
operation.setDescription(parseMarkdown(definition.get("description")));
operation.setResponses(buildResponses((Map) definition.get("responses")));
operation.setParameters(buildParameters((List