• <tfoot id='S0W02'></tfoot>
    <i id='S0W02'><tr id='S0W02'><dt id='S0W02'><q id='S0W02'><span id='S0W02'><b id='S0W02'><form id='S0W02'><ins id='S0W02'></ins><ul id='S0W02'></ul><sub id='S0W02'></sub></form><legend id='S0W02'></legend><bdo id='S0W02'><pre id='S0W02'><center id='S0W02'></center></pre></bdo></b><th id='S0W02'></th></span></q></dt></tr></i><div id='S0W02'><tfoot id='S0W02'></tfoot><dl id='S0W02'><fieldset id='S0W02'></fieldset></dl></div>
      <bdo id='S0W02'></bdo><ul id='S0W02'></ul>

      <legend id='S0W02'><style id='S0W02'><dir id='S0W02'><q id='S0W02'></q></dir></style></legend>
    1. <small id='S0W02'></small><noframes id='S0W02'>

      1. 如何从 OpenAPI 3.0 yaml 文件生成 JSON 示例?

        时间:2023-09-26
        <i id='x67En'><tr id='x67En'><dt id='x67En'><q id='x67En'><span id='x67En'><b id='x67En'><form id='x67En'><ins id='x67En'></ins><ul id='x67En'></ul><sub id='x67En'></sub></form><legend id='x67En'></legend><bdo id='x67En'><pre id='x67En'><center id='x67En'></center></pre></bdo></b><th id='x67En'></th></span></q></dt></tr></i><div id='x67En'><tfoot id='x67En'></tfoot><dl id='x67En'><fieldset id='x67En'></fieldset></dl></div>

        • <small id='x67En'></small><noframes id='x67En'>

        • <tfoot id='x67En'></tfoot>
          • <bdo id='x67En'></bdo><ul id='x67En'></ul>
              <legend id='x67En'><style id='x67En'><dir id='x67En'><q id='x67En'></q></dir></style></legend>

                    <tbody id='x67En'></tbody>
                  本文介绍了如何从 OpenAPI 3.0 yaml 文件生成 JSON 示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有我的 openapi: 3.0.0 YAML 文件,我正在寻找一种从模式生成测试数据响应(JSON 对象)的方法.这 是我正在寻找的,但我无法让它为 openapi: 3.0.0 工作,代码有效非常适合swagger":2.0"定义.我试图让代码与支持 OpenAPI 3.0 的 Swagger Java 库 2.x 一起工作.我知道我需要使用 Swagger 2.x 版.

                  I have my openapi: 3.0.0 YAML file, I'm looking for a way to generate test data response (JSON object) from schema. This is what I am looking for, but I can't get it working for openapi: 3.0.0, the code works perfectly for "swagger": "2.0" definitions. I have tried to get the code working with Swagger Java libraries 2.x, which support OpenAPI 3.0. I know I need to use version 2.x of Swagger.

                  import io.swagger.parser.SwaggerParser;
                  import io.swagger.models.*;
                  import io.swagger.inflector.examples.*;
                  import io.swagger.inflector.examples.models.Example;
                  import io.swagger.inflector.processors.JsonNodeExampleSerializer;
                  import io.swagger.util.Json;
                  import io.swagger.util.Yaml;
                  import java.util.Map;
                  import com.fasterxml.jackson.databind.module.SimpleModule;
                  
                  
                  // Load your OpenAPI/Swagger definition
                  Swagger swagger = new SwaggerParser().read("http://petstore.swagger.io/v2/swagger.json");
                  
                  // Create an Example object for the Pet model
                  Map<String, Model> definitions = swagger.getDefinitions();
                  Model pet = definitions.get("Pet");
                  Example example = ExampleBuilder.fromModel("Pet", pet, definitions, new HashSet<String>());
                  // Another way:
                  // Example example = ExampleBuilder.fromProperty(new RefProperty("Pet"), swagger.getDefinitions());
                  
                  // Configure example serializers
                  SimpleModule simpleModule = new SimpleModule().addSerializer(new JsonNodeExampleSerializer());
                  Json.mapper().registerModule(simpleModule);
                  
                  // Convert the Example object to string
                  
                  // JSON example
                  String jsonExample = Json.pretty(example);
                  System.out.println(jsonExample);
                  

                  此代码有效,只需要为 openapi 获取相同的代码:3.0.0.

                  This code is working, just need to get the same code working for openapi: 3.0.0.

                  推荐答案

                  找到了解决方案,

                  OpenAPI swagger = new OpenAPIV3Parser().read("url to Open API 3.0 Swagger")
                  Map < String, Schema > definitions = swagger.getComponents().getSchemas()
                  Schema model = definitions.get("Pet")
                  Example example = ExampleBuilder.fromSchema(model, definitions)
                  SimpleModule simpleModule = new SimpleModule().addSerializer(new JsonNodeExampleSerializer())
                  Json.mapper().registerModule(simpleModule)
                  String jsonExample = Json.pretty(example);
                  System.out.println(jsonExample);
                  

                  这篇关于如何从 OpenAPI 3.0 yaml 文件生成 JSON 示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将 JSON 转换为 YAML.将 JSON 解析为 YAML 下一篇:从 Java 中的 yaml 读取地图变为空

                  相关文章

                  最新文章

                      <legend id='aPgsI'><style id='aPgsI'><dir id='aPgsI'><q id='aPgsI'></q></dir></style></legend>
                      <tfoot id='aPgsI'></tfoot>
                      • <bdo id='aPgsI'></bdo><ul id='aPgsI'></ul>

                      <small id='aPgsI'></small><noframes id='aPgsI'>

                      <i id='aPgsI'><tr id='aPgsI'><dt id='aPgsI'><q id='aPgsI'><span id='aPgsI'><b id='aPgsI'><form id='aPgsI'><ins id='aPgsI'></ins><ul id='aPgsI'></ul><sub id='aPgsI'></sub></form><legend id='aPgsI'></legend><bdo id='aPgsI'><pre id='aPgsI'><center id='aPgsI'></center></pre></bdo></b><th id='aPgsI'></th></span></q></dt></tr></i><div id='aPgsI'><tfoot id='aPgsI'></tfoot><dl id='aPgsI'><fieldset id='aPgsI'></fieldset></dl></div>