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 (C) 2018-2023 smart-doc
*
* 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 com.ly.doc.builder.openapi;
import com.ly.doc.constants.DocGlobalConstants;
import com.ly.doc.model.*;
import com.ly.doc.utils.DocUtil;
import com.ly.doc.utils.OpenApiSchemaUtil;
import com.power.common.util.CollectionUtil;
import com.power.common.util.FileUtil;
import com.power.common.util.StringUtil;
import com.ly.doc.helper.JavaProjectBuilderHelper;
import com.ly.doc.model.openapi.OpenApiTag;
import com.ly.doc.utils.JsonUtil;
import com.thoughtworks.qdox.JavaProjectBuilder;
import org.apache.commons.lang3.StringUtils;
import java.util.*;
/**
* @author xingzi
* Date 2022/9/17 15:16
*/
@SuppressWarnings("all")
public class SwaggerBuilder extends AbstractOpenApiBuilder {
private static final SwaggerBuilder INSTANCE = new SwaggerBuilder();
/**
* For unit testing
*
* @param config Configuration of smart-doc
*/
public static void buildOpenApi(ApiConfig config) {
JavaProjectBuilder javaProjectBuilder = JavaProjectBuilderHelper.create();
buildOpenApi(config, javaProjectBuilder);
}
/**
* Only for smart-doc maven plugin and gradle plugin.
*
* @param config Configuration of smart-doc
* @param projectBuilder JavaDocBuilder of QDox
*/
public static void buildOpenApi(ApiConfig config, JavaProjectBuilder projectBuilder) {
List apiDocList = INSTANCE.getOpenApiDocs(config, projectBuilder);
INSTANCE.openApiCreate(config, apiDocList);
}
@Override
String getModuleName() {
return DocGlobalConstants.OPENAPI_2_COMPONENT_KRY;
}
/**
* Build OpenApi
*
* @param config Configuration of smart-doc
*/
public void openApiCreate(ApiConfig config, List apiDocList) {
this.setComponentKey(getModuleName());
Map json = new HashMap<>(8);
json.put("swagger", "2.0");
json.put("info", buildInfo(config));
json.put("host", config.getServerUrl() == null ? "127.0.0.1" : config.getServerUrl());
json.put("basePath", StringUtils.isNotBlank(config.getPathPrefix()) ? config.getPathPrefix() : "/");
Set tags = new HashSet<>();
json.put("tags", tags);
json.put("paths", buildPaths(config, apiDocList, tags));
json.put("definitions", buildComponentsSchema(apiDocList));
String filePath = config.getOutPath();
filePath = filePath + DocGlobalConstants.OPEN_API_JSON;
String data = JsonUtil.toPrettyJson(json);
FileUtil.nioWriteFile(data, filePath);
}
/**
* Build openapi info
*
* @param apiConfig Configuration of smart-doc
*/
private static Map buildInfo(ApiConfig apiConfig) {
Map infoMap = new HashMap<>(8);
infoMap.put("title", apiConfig.getProjectName() == null ? "Project Name is Null." : apiConfig.getProjectName());
infoMap.put("version", "1.0.0");
return infoMap;
}
/**
* Build Servers
*
* @param config Configuration of smart-doc
*/
@Deprecated
private static List