Skip to content

Commit 72f35c8

Browse files
committed
Add a null check for oas path in tool integration
1 parent 99642b0 commit 72f35c8

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

openapi-bal-task-plugin/src/main/java/io/ballerina/openapi/bal/tool/Constants.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public enum DiagnosticMessages {
7171
ERROR_WHILE_UPDATING_TOML("OAS_CLIENT_11", "error occurred when updating Ballerina.toml " +
7272
"file with the client native dependency.", DiagnosticSeverity.ERROR),
7373
OPENAPI_EXCEPTION("OAS_CLIENT_12", "exception occurred while reading the openapi contract: %s",
74-
DiagnosticSeverity.ERROR);
74+
DiagnosticSeverity.ERROR),
75+
CONTRACT_PATH_NOT_PROVIDED("OAS_CLIENT_13", "OpenAPI file path is not provided.", DiagnosticSeverity.ERROR);
7576

7677
private final String code;
7778
private final String description;

openapi-bal-task-plugin/src/main/java/io/ballerina/openapi/bal/tool/OpenAPICodeGeneratorTool.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,19 @@ public void execute(ToolContext toolContext) {
113113
ImmutablePair<OASClientConfig, OASServiceMetadata> codeGeneratorConfig;
114114
TomlNodeLocation location = toolContext.currentPackage().ballerinaToml().get().tomlAstNode().location();
115115
try {
116+
// Check the file path
117+
String oasPath = toolContext.filePath();
118+
if (Objects.isNull(oasPath)) {
119+
DiagnosticMessages error = DiagnosticMessages.CONTRACT_PATH_NOT_PROVIDED;
120+
createDiagnostics(toolContext, error, location);
121+
return;
122+
}
123+
if (oasPath.isBlank()) {
124+
DiagnosticMessages error = DiagnosticMessages.EMPTY_CONTRACT_PATH;
125+
createDiagnostics(toolContext, error, location);
126+
}
116127
// Validate the OAS file whether we can handle within OpenAPI tool
117-
if (!canHandle(toolContext)) {
128+
if (!canHandle(oasPath)) {
118129
DiagnosticMessages error = DiagnosticMessages.WARNING_FOR_UNSUPPORTED_CONTRACT;
119130
createDiagnostics(toolContext, error, location);
120131
return;
@@ -199,14 +210,7 @@ private void handleCodeGenerationMode(ToolContext toolContext,
199210
* This method uses to check whether given specification can be handled via the openapi client generation tool.
200211
* This includes basic requirements like file extension check.
201212
*/
202-
private boolean canHandle(ToolContext toolContext) {
203-
String oasPath = toolContext.filePath();
204-
if (oasPath.isBlank()) {
205-
TomlNodeLocation location = toolContext.currentPackage().ballerinaToml().get().tomlAstNode().location();
206-
DiagnosticMessages error = DiagnosticMessages.EMPTY_CONTRACT_PATH;
207-
createDiagnostics(toolContext, error, location);
208-
return false;
209-
}
213+
private boolean canHandle(String oasPath) {
210214
return (oasPath.endsWith(YAML_EXTENSION) || oasPath.endsWith(JSON_EXTENSION) ||
211215
oasPath.endsWith(YML_EXTENSION));
212216
}

0 commit comments

Comments
 (0)