Last active
May 12, 2023 11:28
-
-
Save jmini/05fd39304bc6d9307793d484a4ac603a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///usr/bin/env jbang "$0" "$@" ; exit $? | |
// Script to search all the operationId of a given OpenAPI spec. | |
// Usage: jbang run OperationIdsInOpenAPI.java <path to the OpenAPI spec> | |
// Example taken from https://openapitools.github.io/empoa/ | |
//DEPS org.openapitools.empoa:empoa-util:2.1.0 | |
//DEPS org.openapitools.empoa:empoa-swagger-core:2.1.0 | |
//DEPS io.swagger.parser.v3:swagger-parser:2.0.20 | |
//DEPS org.slf4j:slf4j-simple:1.7.36 | |
import java.util.Collections; | |
import java.util.HashMap; | |
import java.util.Map; | |
import org.eclipse.microprofile.openapi.models.Components; | |
import org.eclipse.microprofile.openapi.models.Operation; | |
import org.eclipse.microprofile.openapi.models.OpenAPI; | |
import org.openapitools.empoa.util.visitor.OASVisitorAdapter; | |
import org.openapitools.empoa.util.visitor.OASVisitResult; | |
import org.openapitools.empoa.util.visitor.OASAccept; | |
import org.openapitools.empoa.swagger.core.internal.SwAdapter; | |
import io.swagger.parser.OpenAPIParser; | |
import io.swagger.v3.parser.core.models.ParseOptions; | |
import io.swagger.v3.parser.core.models.SwaggerParseResult; | |
import static java.lang.System.*; | |
public class OperationIdsInOpenAPI { | |
public static void main(String... args) { | |
if(args.length == 0) { | |
System.out.println("jbang " + OperationIdsInOpenAPI.class.getSimpleName() + ".java <path of the OpenAPI file>"); | |
System.exit(1); | |
} | |
OpenAPIParser openApiParser = new OpenAPIParser(); | |
ParseOptions options = new ParseOptions(); | |
SwaggerParseResult parserResult = openApiParser.readLocation(args[0], null, options); | |
io.swagger.v3.oas.models.OpenAPI swaggerOpenAPI = parserResult.getOpenAPI(); | |
OpenAPI openAPI = SwAdapter.toOpenAPI(swaggerOpenAPI); | |
OperationIdVisitor visitor = new OperationIdVisitor(); | |
OASAccept.accept(visitor, openAPI); | |
Map<String, String> operationIds = visitor.getOperationIds(); | |
operationIds.forEach((k, v) -> { | |
out.println(k + ": " + v); | |
}); | |
} | |
public static class OperationIdVisitor extends OASVisitorAdapter { | |
private Map<String, String> operationIds = new HashMap<>(); | |
public Map<String, String> getOperationIds() { | |
return Collections.unmodifiableMap(operationIds); | |
} | |
@Override | |
public OASVisitResult visit(Operation operation, String jsonPath) { | |
operationIds.put(jsonPath + ".operationId", operation.getOperationId()); | |
return OASVisitResult.CONTINUE; | |
} | |
@Override | |
public OASVisitResult visit(Components components, String jsonPath) { | |
return OASVisitResult.TERMINATE; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example:
For this OpenAPI spec: todoapp.yaml
Output: