io.swagger.v3.oas.models.OpenAPI类的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(14.7k)|赞(0)|评价(0)|浏览(1065)

本文整理了Java中io.swagger.v3.oas.models.OpenAPI类的一些代码示例,展示了OpenAPI类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。OpenAPI类的具体详情如下:
包路径:io.swagger.v3.oas.models.OpenAPI
类名称:OpenAPI

OpenAPI介绍

[英]OpenAPI
[中]开放应用程序接口

代码示例

代码示例来源:origin: swagger-api/swagger-core

OpenAPI clone = new OpenAPI();
clone.info(filteredOpenAPI.getInfo());
clone.openapi(filteredOpenAPI.getOpenapi());
clone.setExtensions(openAPI.getExtensions());
clone.setExternalDocs(openAPI.getExternalDocs());
clone.setSecurity(openAPI.getSecurity());
clone.setServers(openAPI.getServers());
clone.tags(filteredOpenAPI.getTags() == null ? null : new ArrayList<>(openAPI.getTags()));
for (String resourcePath : openAPI.getPaths().keySet()) {
  PathItem pathItem = openAPI.getPaths().get(resourcePath);
clone.paths(clonedPaths);
final List<Tag> tags = clone.getTags();
if (tags != null && !filteredTags.isEmpty()) {
  for (Iterator<Tag> it = tags.iterator(); it.hasNext(); ) {
  if (clone.getTags().isEmpty()) {
    clone.setTags(null);
if (filteredOpenAPI.getComponents() != null) {
  clone.components(new Components());
  clone.getComponents().setSchemas(filterComponentsSchema(filter, filteredOpenAPI.getComponents().getSchemas(), params, cookies, headers));
  clone.getComponents().setSecuritySchemes(filteredOpenAPI.getComponents().getSecuritySchemes());
  clone.getComponents().setCallbacks(filteredOpenAPI.getComponents().getCallbacks());
  clone.getComponents().setExamples(filteredOpenAPI.getComponents().getExamples());
  clone.getComponents().setExtensions(filteredOpenAPI.getComponents().getExtensions());
  clone.getComponents().setHeaders(filteredOpenAPI.getComponents().getHeaders());

代码示例来源:origin: swagger-api/swagger-core

AnnotationsUtils.getInfo(openAPIDefinition.info()).ifPresent(info -> openAPI.setInfo(info));
      .ifPresent(s -> openAPI.setSecurity(s));
      .ifPresent(docs -> openAPI.setExternalDocs(docs));
  AnnotationsUtils.getServers(openAPIDefinition.servers()).ifPresent(servers -> openAPI.setServers(servers));
    openAPI.setExtensions(AnnotationsUtils
        .getExtensions(openAPIDefinition.extensions()));
      if (openAPI.getPaths() != null && openAPI.getPaths().get(operationPath) != null) {
        pathItemObject = openAPI.getPaths().get(operationPath);
      } else {
        pathItemObject = new PathItem();
      if (openAPI.getPaths() != null) {
        this.paths.putAll(openAPI.getPaths());
      openAPI.setPaths(this.paths);
if (!isEmptyComponents(components) && openAPI.getComponents() == null) {
  openAPI.setComponents(components);
  if (openAPI.getTags() != null) {
    for (Tag tag : openAPI.getTags()) {
      if (tagsSet.stream().noneMatch(t -> t.getName().equals(tag.getName()))) {
        tagsSet.add(tag);

代码示例来源:origin: swagger-api/swagger-core

protected OpenAPI removeBrokenReferenceDefinitions(OpenAPI openApi) {
  if (openApi == null || openApi.getComponents() == null || openApi.getComponents().getSchemas() == null) {
    return openApi;
  }
  Set<String> referencedDefinitions = new TreeSet<>();
  if (openApi.getPaths() != null) {
    for (String resourcePath : openApi.getPaths().keySet()) {
      PathItem pathItem = openApi.getPaths().get(resourcePath);
      addPathItemSchemaRef(pathItem, referencedDefinitions);
    }
  }
  Set<String> nestedReferencedDefinitions = new TreeSet<>();
  for (String ref : referencedDefinitions) {
    locateReferencedDefinitions(ref, nestedReferencedDefinitions, openApi);
  }
  referencedDefinitions.addAll(nestedReferencedDefinitions);
  openApi.getComponents().getSchemas().keySet().retainAll(referencedDefinitions.stream().map(s -> (String) RefUtils.extractSimpleName(s).getLeft()).collect(Collectors.toSet()));
  return openApi;
}

代码示例来源:origin: swagger-api/swagger-core

@Test
public void testExtension() throws Exception {
  OpenAPI swagger = new OpenAPI();
  swagger.addExtension("x-foo-bar", "foo bar");
  swagger.setInfo(new Info());
  swagger.getInfo().addExtension("x-foo-bar", "foo bar");
  String swaggerJson = Json.mapper().writeValueAsString(swagger);
  assertFalse(swaggerJson.contains("extensions"));
  OpenAPI rebuilt = Json.mapper().readValue(swaggerJson, OpenAPI.class);
  assertEquals(rebuilt.getExtensions().values().iterator().next(), "foo bar");
  assertEquals(rebuilt.getInfo().getExtensions().values().iterator().next(), "foo bar");
}

代码示例来源:origin: io.swagger.parser.v3/swagger-parser-v3

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;
    openAPI.setOpenapi(value);
      openAPI.setInfo(info);
    if (obj != null) {
      Paths paths = getPaths(obj, "paths", result);
      openAPI.setPaths(paths);
    if (obj != null) {
      Components components = getComponents(obj, "components", result);
      openAPI.setComponents(components);
      openAPI.setServers(getServersList(array, String.format("%s.%s", location, "servers"), result, path));
    }else {
      Server defaultServer = new Server();
      List<Server>  servers = new ArrayList<>();
      servers.add(defaultServer);
      openAPI.setServers(servers);
    if (obj != null) {
      ExternalDocumentation externalDocs = getExternalDocs(obj, "externalDocs", result);
      openAPI.setExternalDocs(externalDocs);

代码示例来源:origin: apache/cxf

customizer.customize(oas);
if (!Objects.equals(configuration.getOpenAPI().getInfo(), oas.getInfo())) {
  configuration.getOpenAPI().setInfo(oas.getInfo());
if (!Objects.equals(configuration.getOpenAPI().getComponents(), oas.getComponents())) {
  configuration.getOpenAPI().setComponents(oas.getComponents());
if (!Objects.equals(configuration.getOpenAPI().getExternalDocs(), oas.getExternalDocs())) {
  configuration.getOpenAPI().setExternalDocs(oas.getExternalDocs());
if (!Objects.equals(configuration.getOpenAPI().getPaths(), oas.getPaths())) {
  configuration.getOpenAPI().setPaths(oas.getPaths());
if (!Objects.equals(configuration.getOpenAPI().getTags(), oas.getTags())) {
  configuration.getOpenAPI().setTags(oas.getTags());
if (!Objects.equals(configuration.getOpenAPI().getExtensions(), oas.getExtensions())) {
  configuration.getOpenAPI().setExtensions(oas.getExtensions());

代码示例来源:origin: org.openapitools/openapi-generator

if (value != null) {
  gen.writeStartObject();
  gen.writeStringField("openapi", value.getOpenapi());
  if(value.getInfo() != null) {
    gen.writeObjectField("info", value.getInfo());
  if(value.getExternalDocs() != null) {
    gen.writeObjectField("externalDocs", value.getExternalDocs());
  if(value.getServers() != null) {
    gen.writeObjectField("servers", value.getServers());
  if(value.getSecurity() != null) {
    gen.writeObjectField("security", value.getSecurity());
  if(value.getTags() != null) {
    gen.writeObjectField("tags", value.getTags());
  if(value.getPaths() != null) {
    gen.writeObjectField("paths", value.getPaths());
  if(value.getComponents() != null) {
    gen.writeObjectField("components", value.getComponents());
  if(value.getExtensions() != null) {
    for (Entry<String, Object> e : value.getExtensions().entrySet()) {
      gen.writeObjectField(e.getKey(), e.getValue());

代码示例来源:origin: swagger-api/swagger-core

@Override
public void setConfiguration(OpenAPIConfiguration openApiConfiguration) {
  if (openApiConfiguration != null) {
    this.config = ContextUtils.deepCopy(openApiConfiguration);
    if (openApiConfiguration.getOpenAPI() != null) {
      this.openAPI = this.config.getOpenAPI();
      if (this.openAPI.getComponents() != null) {
        this.components = this.openAPI.getComponents();
      }
    }
  }
}

代码示例来源:origin: swagger-api/swagger-core

private boolean existOperationId(String operationId) {
  if (openAPI == null) {
    return false;
  }
  if (openAPI.getPaths() == null || openAPI.getPaths().isEmpty()) {
    return false;
  }
  for (PathItem path : openAPI.getPaths().values()) {
    Set<String> pathOperationIds = extractOperationIdFromPathItem(path);
    if (pathOperationIds.contains(operationId)) {
      return true;
    }
  }
  return false;
}

代码示例来源:origin: io.swagger/swagger-codegen-generators

public void preprocessOpenAPI(OpenAPI openAPI) {
  Info info = openAPI.getInfo();
  info.setDescription(toHtml(info.getDescription()));
  info.setTitle(toHtml(info.getTitle()));
  if (openAPI.getComponents() == null || openAPI.getComponents().getSchemas() == null) {
    return;
  }
  Map<String, Schema> schemas = openAPI.getComponents().getSchemas();
  for (Schema schema : schemas.values()) {
    schema.setDescription(toHtml(schema.getDescription()));
    schema.setTitle(toHtml(schema.getTitle()));
  }
}

代码示例来源:origin: noboomu/proteus

@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);
  config.setModelConverterClassess(Collections.singleton(ServerModelResolver.class.getName()));
  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);
}

代码示例来源:origin: org.ballerinalang/swagger-to-ballerina-generator

/**
 * Build a {@link BallerinaOpenApi} object from a {@link OpenAPI} object.
 * All non iterable objects using handlebars library is converted into
 * supported iterable object types.
 *
 * @param openAPI {@link OpenAPI} type object to be converted
 * @return Converted {@link BallerinaOpenApi} object
 * @throws BallerinaOpenApiException when OpenAPI to BallerinaOpenApi parsing failed
 */
@Override
public BallerinaOpenApi buildContext(OpenAPI openAPI) throws BallerinaOpenApiException {
  this.openapi = openAPI.getOpenapi();
  this.info = openAPI.getInfo();
  this.externalDocs = openAPI.getExternalDocs();
  this.tags = openAPI.getTags();
  this.components = openAPI.getComponents();
  this.extensions = openAPI.getExtensions();
  setPaths(openAPI);
  setSecurityRequirements(openAPI);
  setServers(openAPI);
  setSchemas(openAPI);
  return this;
}

代码示例来源:origin: io.gravitee.management/gravitee-management-api-service

private NewApiEntity mapOpenApiToNewApi(SwaggerParseResult swagger) {
  if (swagger == null || swagger.getOpenAPI() == null || swagger.getOpenAPI().getInfo() == null) {
    return null;
  }
  NewApiEntity apiEntity = new NewApiEntity();
  apiEntity.setName(swagger.getOpenAPI().getInfo().getTitle());
  apiEntity.setDescription(swagger.getOpenAPI().getInfo().getDescription());
  apiEntity.setVersion(swagger.getOpenAPI().getInfo().getVersion());
  if (!swagger.getOpenAPI().getServers().isEmpty()) {
    apiEntity.setEndpoint(swagger.getOpenAPI().getServers().get(0).getUrl());
  }
  apiEntity.setPaths(new ArrayList<>(
      swagger.getOpenAPI().getPaths().keySet()
          .stream()
          .map(path -> path.replaceAll("\\{(.[^/]*)\\}", ":$1"))
          .collect(Collectors.toList())));
  return apiEntity;
}

代码示例来源:origin: swagger-api/swagger-core

@Test
public void testSerializeASpecWithPathReferences() throws Exception {
  OpenAPI swagger = new OpenAPI()
      .addServersItem(new Server().url("http://petstore.swagger.io"));
  PathItem expectedPath = new PathItem().$ref("http://my.company.com/paths/health.json");
  swagger.path("/health", expectedPath);
  String swaggerJson = Json.mapper().writeValueAsString(swagger);
  OpenAPI rebuilt = Json.mapper().readValue(swaggerJson, OpenAPI.class);
  final PathItem path = rebuilt.getPaths().get("/health");
  assertEquals(path, expectedPath);
}

代码示例来源:origin: apache/cxf

oas.getPaths().forEach((pathKey, pathItem) -> {
  Tag tag = null;
  if (replaceTags && operations.containsKey(pathKey)) {
if (replaceTags && oas.getTags() != null) {
  oas.setTags(tags);

代码示例来源:origin: com.holon-platform.jaxrs/documentation-jaxrs

public ApiConfigV3Group1() {
  super();
  setOpenAPI(new OpenAPI().info(new Info().title("API group 1")));
  setResourcePackages(Collections.singleton("my.resource.package.group1"));
}

代码示例来源:origin: com.atlassian.swagger/atlassian-swagger-doclet

private void fixPathAndServerUrls(OpenAPI openAPI) {
  String baseUrl = getBaseUrlFromServerUrls(openAPI);
  Map<String, PathItem> prefixedPaths = openAPI.getPaths().entrySet().stream()
      .map(entry -> new AbstractMap.SimpleEntry<>(baseUrl + entry.getKey(), entry.getValue()))
      .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
  Paths paths = new Paths();
  paths.putAll(prefixedPaths);
  openAPI.setPaths(paths);
  List<Server> servers = openAPI.getServers()
      .stream()
      .map(server -> {
        String url = server.getUrl();
        return new Server()
            .url(url.substring(0, url.indexOf(baseUrl) > 0 ? url.indexOf(baseUrl) : 0))
            .description(server.getDescription());
      })
      .collect(Collectors.toList());
  openAPI.setServers(servers);
}

代码示例来源:origin: org.openapitools/openapi-generator

public void preprocessOpenAPI(OpenAPI openAPI) {
  super.preprocessOpenAPI(openAPI);
  if (openAPI == null || openAPI.getPaths() == null) {
    return;
  if (openAPI.getTags() != null) {
    List<ResourcePath> resourcePaths = new ArrayList<>();
    for (Tag tag : openAPI.getTags()) {
      ResourcePath resourcePath = new ResourcePath();
      resourcePath.setPath(tag.getName());
  Map<String, Object> vendorExt = openAPI.getInfo().getExtensions();
  if (vendorExt != null && !vendorExt.toString().equals("")) {
    if (vendorExt.containsKey("x-codegen")) {
    String title = openAPI.getInfo().getTitle();
  this.additionalProperties.put("serverPort", URLPathUtils.getPort(url, 8080));
  if (openAPI.getPaths() != null) {
    for (String pathname : openAPI.getPaths().keySet()) {
      PathItem path = openAPI.getPaths().get(pathname);
      if (path.readOperations() != null) {
        for (Operation operation : path.readOperations()) {

代码示例来源:origin: swagger-api/swagger-core

info.setContact(contact);
final OpenAPI swagger = new OpenAPI()
    .info(info)
    .addServersItem(new Server().url("http://petstore.swagger.io"))
    .schema("Person", personModel);
    .components(new Components().addParameters("Foo", parameter))
    .path("/pets", new PathItem().get(get));

代码示例来源:origin: ppdai-incubator/raptor

/**
   * https://github.com/OAI/OpenAPI-SpecOpenApiification/blob/master/versions/3.0.1.md#openapi-object
   *
   * @param service
   * @param protoFile
   * @return
   */
  protected OpenAPI getOpenApi(Service service, ProtoFile protoFile) {
    refHelper.set(new RefHelper(schmea, protoFile, service));

    OpenAPI openApi = new OpenAPI();

    //required
    openApi.info(getInfo(protoFile, service));
    openApi.paths(getPath(protoFile, service));

    //optional
    openApi.servers(getServers());
    openApi.components(getComponents());
//        openApi.security()
    openApi.tags(getTags());
//        openApi.externalDocs(getExternalDocumentation());

    return openApi;
  }

相关文章