Java Code Examples for io.swagger.v3.oas.models.OpenAPI#setServers()

The following examples show how to use io.swagger.v3.oas.models.OpenAPI#setServers() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenAPIBuilder.java    From springdoc-openapi with Apache License 2.0 6 votes vote down vote up
/**
 * Build open api with open api definition.
 *
 * @param openAPI the open api
 * @param apiDef the api def
 */
private void buildOpenAPIWithOpenAPIDefinition(OpenAPI openAPI, OpenAPIDefinition apiDef) {
	// info
	AnnotationsUtils.getInfo(apiDef.info()).map(this::resolveProperties).ifPresent(openAPI::setInfo);
	// OpenApiDefinition security requirements
	securityParser.getSecurityRequirements(apiDef.security()).ifPresent(openAPI::setSecurity);
	// OpenApiDefinition external docs
	AnnotationsUtils.getExternalDocumentation(apiDef.externalDocs()).ifPresent(openAPI::setExternalDocs);
	// OpenApiDefinition tags
	AnnotationsUtils.getTags(apiDef.tags(), false).ifPresent(tags -> openAPI.setTags(new ArrayList<>(tags)));
	// OpenApiDefinition servers
	Optional<List<Server>> optionalServers = AnnotationsUtils.getServers(apiDef.servers());
	if (optionalServers.isPresent()) {
		openAPI.setServers(optionalServers.get());
		this.isServersPresent = true;
	}
	// OpenApiDefinition extensions
	if (apiDef.extensions().length > 0) {
		openAPI.setExtensions(AnnotationsUtils.getExtensions(apiDef.extensions()));
	}
}
 
Example 2
Source File: TestUtils.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
public static OpenAPI createOpenAPI() {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());
    openAPI.setPaths(new Paths());

    final Info info = new Info();
    info.setDescription("API under test");
    info.setVersion("1.0.7");
    info.setTitle("My title");
    openAPI.setInfo(info);

    final Server server = new Server();
    server.setUrl("https://localhost:9999/root");
    openAPI.setServers(Collections.singletonList(server));
    return openAPI;
}
 
Example 3
Source File: ApiDocControllerTest.java    From api-layer with Eclipse Public License 2.0 6 votes vote down vote up
private OpenAPI getDummyOpenApiObject() {
    List<Server> servers = new ArrayList<>();
    servers.add(0, new Server().url("/api/v1/apicatalog"));
    OpenAPI openAPI = new OpenAPI();
    openAPI.setPaths(new Paths());
    openAPI.setTags(new ArrayList<>());
    openAPI.setOpenapi("3.0.0");
    openAPI.setServers(servers);

    Info info = new Info();
    info.setTitle("Service title");
    info.setDescription("Service description");
    info.setVersion("1.0.0");
    openAPI.setInfo(info);

    return openAPI;
}
 
Example 4
Source File: AbstractApiDocServiceTest.java    From api-layer with Eclipse Public License 2.0 6 votes vote down vote up
private OpenAPI getDummyOpenApiObject(List<Server> servers) {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setPaths(new Paths());
    openAPI.setTags(new ArrayList<>());
    openAPI.setOpenapi("3.0.0");
    openAPI.setServers(servers);

    Info info = new Info();
    info.setTitle("API Catalog");
    info.setDescription("REST API for the API Catalog service which is a component of the API Mediation Layer. Use this API to retrieve information regarding catalog dashboard tiles, tile contents and its status, API documentation and status for the registered services.");
    info.setVersion("1.0.0");
    openAPI.setInfo(info);

    Tag tag = new Tag();
    tag.setName("API Catalog");
    tag.setDescription("Current state information");
    openAPI.getTags().add(tag);

    openAPI.getPaths().put("/api1", new PathItem());
    openAPI.getPaths().put("/api2", new PathItem());
    return openAPI;
}
 
Example 5
Source File: OpenApiObjectGenerator.java    From flow with Apache License 2.0 6 votes vote down vote up
private OpenAPI createBasicModel() {
    OpenAPI openAPI = new OpenAPI();

    Info info = new Info();
    info.setTitle(configuration.getApplicationTitle());
    info.setVersion(configuration.getApplicationApiVersion());
    openAPI.setInfo(info);

    Paths paths = new Paths();
    openAPI.setPaths(paths);

    Server server = new Server();
    server.setUrl(configuration.getServerUrl());
    server.setDescription(configuration.getServerDescription());
    openAPI.setServers(Collections.singletonList(server));
    Components components = new Components();
    SecurityScheme vaadinConnectOAuth2Scheme = new SecurityScheme()
            .type(SecurityScheme.Type.OAUTH2)
            .flows(new OAuthFlows().password(new OAuthFlow()
                    .tokenUrl(VAADIN_CONNECT_OAUTH2_TOKEN_URL)
                    .scopes(new Scopes())));
    components.addSecuritySchemes(VAADIN_CONNECT_OAUTH2_SECURITY_SCHEME,
            vaadinConnectOAuth2Scheme);
    openAPI.components(components);
    return openAPI;
}
 
Example 6
Source File: Swagger3RestDocGenerator.java    From RestDoc with Apache License 2.0 5 votes vote down vote up
private void convertServers(RootModel rootModel, OpenAPI openApi) {
    var servers = new ArrayList<Server>();
    for (var server : _config.getServers()) {
        var serverInfo = new Server();
        serverInfo.setDescription(server.getDescription());
        serverInfo.setUrl(server.getUrl());

        servers.add(serverInfo);
    }
    openApi.setServers(servers);
}
 
Example 7
Source File: OpenAPIBuilder.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
/**
 * Update servers open api.
 *
 * @param openAPI the open api
 * @return the open api
 */
public OpenAPI updateServers(OpenAPI openAPI) {
	if (!isServersPresent)        // default server value
	{
		Server server = new Server().url(serverBaseUrl).description(DEFAULT_SERVER_DESCRIPTION);
		List<Server> servers = new ArrayList();
		servers.add(server);
		openAPI.setServers(servers);
	}
	return openAPI;
}
 
Example 8
Source File: ApiDocV3Service.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
private void updateServerUrl(OpenAPI openAPI, Server server, String basePath) {
    if (server != null) {
        server.setUrl(basePath);
        openAPI.setServers(Collections.singletonList(server));
    } else {
        openAPI.addServersItem(new Server().url(basePath));
    }
}
 
Example 9
Source File: OpenAPIConfiguration.java    From openapi-petstore with Apache License 2.0 5 votes vote down vote up
@Bean
public OpenAPI openapi(
        @Value("classpath:/openapi.yaml") Resource openapiResource,
        @Value("${openapi.openAPIPetstore.base-path:/v3}") String apiBasePath) throws IOException {
    try(InputStream is = openapiResource.getInputStream()) {
        OpenAPI openAPI = new OpenAPIParser()
                .readContents(StreamUtils.copyToString(is, Charset.defaultCharset()), null, new ParseOptions())
                .getOpenAPI();
        ObjectNode node = Yaml.mapper().readValue(openapiResource.getInputStream(), ObjectNode.class);
        if (node.get("servers") == null) {
            openAPI.setServers(Collections.singletonList(new Server().url(apiBasePath)));
        }
        return openAPI;
    }
}
 
Example 10
Source File: OpenAPIDeserializer.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
public OpenAPI parseRoot(JsonNode node, ParseResult result, String path) {
    String location = "";
    OpenAPI openAPI = new OpenAPI();
    if (node.getNodeType().equals(JsonNodeType.OBJECT)) {
        ObjectNode rootNode = (ObjectNode) node;

        // required
        String value = getString("openapi", rootNode, true, location, result);

        // we don't even try if the version isn't there
        if(value == null || !value.startsWith("3.0")) {
            return null;
        }
        openAPI.setOpenapi(value);

        ObjectNode obj = getObject("info", rootNode, true, location, result);
        if (obj != null) {
            Info info = getInfo(obj, "info", result);
            openAPI.setInfo(info);
        }

        obj = getObject("components", rootNode, false, location, result);
        if (obj != null) {
            Components components = getComponents(obj, "components", result);
            openAPI.setComponents(components);
            this.components=components;
        }

        obj = getObject("paths", rootNode, true, location, result);
        if (obj != null) {
            Paths paths = getPaths(obj, "paths", result);
            openAPI.setPaths(paths);
        }


        ArrayNode array = getArray("servers", rootNode, false, location, result);
        if (array != null && array.size() > 0) {
            openAPI.setServers(getServersList(array, String.format("%s.%s", location, "servers"), result, path));
        }else {
            Server defaultServer = new Server();
            defaultServer.setUrl("/");
            List<Server>  servers = new ArrayList<>();
            servers.add(defaultServer);
            openAPI.setServers(servers);
        }

        obj = getObject("externalDocs", rootNode, false, location, result);
        if (obj != null) {
            ExternalDocumentation externalDocs = getExternalDocs(obj, "externalDocs", result);
            openAPI.setExternalDocs(externalDocs);
        }

        array = getArray("tags", rootNode, false, location, result);
        if (array != null && array.size() > 0) {
            openAPI.setTags(getTagList(array, "tags", result));
        }

        array = getArray("security", rootNode, false, location, result);
        if (array != null && array.size() > 0) {
            List<SecurityRequirement> securityRequirements = getSecurityRequirementsList(array, "security", result);
            if (securityRequirements != null && securityRequirements. size() > 0) {
                openAPI.setSecurity(securityRequirements);
            }
        }

        Map <String,Object> extensions = getExtensions(rootNode);
        if(extensions != null && extensions.size() > 0) {
            openAPI.setExtensions(extensions);
        }

        Set<String> keys = getKeys(rootNode);
        for(String key : keys) {
            if(!ROOT_KEYS.contains(key) && !key.startsWith("x-")) {
                result.extra(location, key, node.get(key));
            }
        }

    } else {
        result.invalidType(location, "openapi", "object", node);
        result.invalid();
        return null;
    }

    return openAPI;
}
 
Example 11
Source File: OpenAPIService.java    From proteus with Apache License 2.0 2 votes vote down vote up
@SuppressWarnings("rawtypes")
protected void generateSpec() throws Exception
{
	Set<Class<?>> classes = this.registeredControllers;

	OpenAPIExtensions.setExtensions(Collections.singletonList(new ServerParameterExtension()));

	OpenAPI openApi = new OpenAPI();
	Info info = mapper.convertValue(openAPIConfig.getValue("info").unwrapped(), Info.class);

	openApi.setInfo(info);

	Map<String, SecurityScheme> securitySchemes = mapper.convertValue(	openAPIConfig.getValue("securitySchemes").unwrapped(),new TypeReference<Map<String, SecurityScheme>>(){});

	if (openApi.getComponents() == null)
	{
		openApi.setComponents(new Components());
	}

	openApi.getComponents().setSecuritySchemes(securitySchemes);

	List<Server> servers = mapper.convertValue(openAPIConfig.getValue("servers").unwrapped(), new TypeReference<List<Server>>(){});

	openApi.setServers(servers);

	SwaggerConfiguration config = new SwaggerConfiguration().resourceClasses(classes.stream().map(Class::getName).collect(Collectors.toSet())).openAPI(openApi);

	if(jsonViewQueryParameterName != null) {

		if(config.getUserDefinedOptions() == null)
		{
			config.setUserDefinedOptions(new HashMap<>());
		}

		config.getUserDefinedOptions().put("jsonViewQueryParameterName", jsonViewQueryParameterName);
	}

	Set<String> modelConverterClasses = new HashSet<>();

	modelConverterClasses.add(ServerModelResolver.class.getName());

	List<String> additionalConverterClasses = openAPIConfig.getStringList("converterClasses");

	modelConverterClasses.addAll(additionalConverterClasses);

	config.setModelConverterClassess(modelConverterClasses);

	OpenApiContext ctx = new GenericOpenApiContext().openApiConfiguration(config)
			.openApiReader(new Reader(config))
			.openApiScanner(new JaxrsApplicationAndAnnotationScanner().openApiConfiguration(config))
			.init();

	openApi = ctx.read();
	this.openApi = openApi;
	this.spec = writer.writeValueAsString(openApi);
}