yl-backend/src/main/java/com/guwan/backend/swagger/SwaggerResponseParser.java

33 lines
1.2 KiB
Java

package com.guwan.backend.swagger;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.responses.ApiResponses;
import io.swagger.v3.oas.models.OpenAPI;
import java.util.Map;
public class SwaggerResponseParser {
public static Schema<?> extractResponseSchema(ApiResponses responses, OpenAPI openAPI) {
if (responses != null && responses.containsKey("200")) {
ApiResponse response = responses.get("200");
if (response.getContent() != null) {
for (Map.Entry<String, io.swagger.v3.oas.models.media.MediaType> entry : response.getContent().entrySet()) {
Schema<?> schema = entry.getValue().getSchema();
return resolveSchema(schema, openAPI);
}
}
}
return null;
}
private static Schema<?> resolveSchema(Schema<?> schema, OpenAPI openAPI) {
if (schema != null && schema.get$ref() != null) {
String refName = schema.get$ref().replace("#/components/schemas/", "");
return openAPI.getComponents().getSchemas().get(refName);
}
return schema;
}
}