spring boot swagger库-可以将OpenAPI 3转换为swagger 2吗?

jchrr9hc  于 2021-07-23  发布在  Java
关注(0)|答案(0)|浏览(559)

我有一个场景,在这个场景中,我需要openapi3和swagger 2格式的api文档。我目前使用gradle io.swagger.v3模型生成openapi3文档,并分别使用openapicustomiser和securityscheme提供相关的模式约束和oauth2定义。
我意识到我可以使用springfox.documentation中的docket类来返回swagger 2文档,但是我想包括我从openapicustomiser获得的oauth定义和模式约束,所有这些都可以翻译到swagger 2。这可能吗?
如果有帮助的话,我可以这样使用io.swagger库来定义openapi文档:
招摇过市

  1. @EnableSwagger2
  2. @Configuration
  3. public class SwaggerConfig {
  4. ...
  5. private final SwaggerConfigUtil util = new SwaggerConfigUtil();
  6. private SecurityScheme applicationIdSecurityScheme() {
  7. return new SecurityScheme().type(SecurityScheme.Type.OAUTH2)
  8. .flows(new OAuthFlows()
  9. .implicit(new OAuthFlow()
  10. .authorizationUrl(adV2TokenUrl)
  11. .scopes(new Scopes()
  12. .addString("openid", "openid")
  13. )
  14. )
  15. );
  16. }
  17. ...
  18. @Bean
  19. public OpenAPI customOpenAPI() {
  20. return new OpenAPI()
  21. .info(apiInfo())
  22. .schemaRequirement("OAuth2", applicationIdSecurityScheme())
  23. .addSecurityItem(new SecurityRequirement().addList("OAuth2"));
  24. }
  25. @Bean
  26. public OpenApiCustomiser openApiCustomiser() {
  27. return openApi -> {
  28. openApi.getPaths().forEach((s, p) -> {
  29. util.operationConstraints(p.getGet());
  30. util.operationConstraints(p.getDelete());
  31. util.operationConstraints(p.getHead());
  32. util.operationConstraints(p.getOptions());
  33. util.operationConstraints(p.getPatch());
  34. util.operationConstraints(p.getPost());
  35. util.operationConstraints(p.getPut());
  36. util.operationConstraints(p.getTrace());
  37. });
  38. openApi.getComponents().getSchemas().values().forEach(s -> {
  39. s.setAdditionalProperties(false);
  40. util.propertyContraints(s.getProperties());
  41. });
  42. };
  43. }
  44. }

招摇过市

  1. public class SwaggerConfigUtil {
  2. private final int INT_MIN = Integer.MIN_VALUE;
  3. private final int INT_MAX = Integer.MAX_VALUE;
  4. private final int DEFAULT_LENGTH = 255;
  5. private final String DEFAULT_PATTERN = "^.*$";
  6. private final Map<Class<?>, Function<Schema, Integer>> SET_SCHEMA_CONTRAINTS =
  7. Stream.of(
  8. new AbstractMap.SimpleEntry<Class<?>, Function<Schema, Integer>>(StringSchema.class, this::setStringContraints),
  9. new AbstractMap.SimpleEntry<Class<?>, Function<Schema, Integer>>(DateSchema.class, this::setStringContraints),
  10. new AbstractMap.SimpleEntry<Class<?>, Function<Schema, Integer>>(DateTimeSchema.class, this::setStringContraints),
  11. new AbstractMap.SimpleEntry<Class<?>, Function<Schema, Integer>>(ArraySchema.class, this::setArrayContraints),
  12. new AbstractMap.SimpleEntry<Class<?>, Function<Schema, Integer>>(IntegerSchema.class, this::setIntegerConstraints),
  13. new AbstractMap.SimpleEntry<Class<?>, Function<Schema, Integer>>(NumberSchema.class, this::setIntegerConstraints))
  14. .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
  15. private final ApiResponse responseDefault = new ApiResponse();
  16. private final ApiResponses defaultApiResponses = new ApiResponses().addApiResponse("default", new ApiResponse());
  17. public void operationConstraints(Operation operation){
  18. if(operation != null){
  19. setApiResponses(operation.getResponses());
  20. if(operation.getParameters() != null){
  21. paramConstraints(operation.getParameters());
  22. }
  23. }
  24. }
  25. public void setApiResponses(ApiResponses responses){
  26. if(responses != null){
  27. responseContraints(responses);
  28. }
  29. }
  30. public void paramConstraints(List<Parameter> parameters){
  31. parameters.forEach(p -> {
  32. Schema s = p.getSchema();
  33. if(s != null){
  34. schemaConstraints(s);
  35. }
  36. });
  37. }
  38. public void contentConstraints(Content content){
  39. MediaType m = content.get("application/json");
  40. System.out.println(m);
  41. if(m != null) {
  42. Schema s = m.getSchema();
  43. schemaConstraints(s);
  44. }
  45. }
  46. public void schemaConstraints(Schema s){
  47. Class c = s.getClass();
  48. SET_SCHEMA_CONTRAINTS.getOrDefault(c, (v)->1).apply(s);
  49. }
  50. public void propertyContraints(Map properties){
  51. Iterator<Map.Entry> iter = properties.entrySet().iterator();
  52. while (iter.hasNext()) {
  53. Map.Entry s = iter.next();
  54. Schema value = (Schema)s.getValue();
  55. schemaConstraints(value);
  56. }
  57. }
  58. public void responseContraints(ApiResponses responses){
  59. Iterator<Map.Entry<String,ApiResponse>> iter = responses.entrySet().iterator();
  60. while (iter.hasNext()) {
  61. Map.Entry a = iter.next();
  62. ApiResponse val = (ApiResponse)a.getValue();
  63. Content c = val.getContent();
  64. contentConstraints(c);
  65. }
  66. }
  67. public int setStringContraints(Schema schema){
  68. if(schema.getMaxLength() == null){
  69. schema.setMaxLength(DEFAULT_LENGTH);
  70. }
  71. if(schema.getPattern() == null){
  72. schema.setPattern(DEFAULT_PATTERN);
  73. }
  74. return 0;
  75. }
  76. public int setArrayContraints(Schema schema){
  77. ArraySchema arraySchema = (ArraySchema) schema;
  78. if(arraySchema.getMaxItems() == null){
  79. arraySchema.setMaxItems(DEFAULT_LENGTH);
  80. }
  81. Schema items = arraySchema.getItems();
  82. setItemConstraints(items);
  83. return 0;
  84. }
  85. public int setItemConstraints(Schema items){
  86. schemaConstraints(items);
  87. Map properties = items.getProperties();
  88. if(properties != null){
  89. propertyContraints(properties);
  90. }
  91. return 0;
  92. }
  93. public int setIntegerConstraints(Schema schema){
  94. if(schema.getMinimum() == null){
  95. schema.setMinimum((BigDecimal.valueOf(INT_MIN)));
  96. }
  97. if(schema.getMaximum() == null){
  98. schema.setMaximum((BigDecimal.valueOf(INT_MAX)));
  99. }
  100. return 0;
  101. }
  102. }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题