Skip to content

[validation] add error if path do not start with a slash #997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public void run() {
SwaggerParseResult result = new OpenAPIParser().readLocation(spec, null, null);
List<String> messageList = result.getMessages();
Set<String> errors = new HashSet<String>(messageList);
errors.addAll(ModelUtils.getErrorMessages(result.getOpenAPI()));
Set<String> warnings = new HashSet<String>();

StringBuilder sb = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ public ClientOptInput toClientOptInput() {

Set<String> validationMessages = new HashSet<>(result.getMessages());
OpenAPI specification = result.getOpenAPI();
validationMessages.addAll(ModelUtils.getErrorMessages(specification));

// NOTE: We will only expose errors+warnings if there are already errors in the spec.
if (validationMessages.size() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -692,4 +693,21 @@ public static Header getHeader(OpenAPI openAPI, String name) {
}
return null;
}

/**
* Compute additional error messages that are not detected by the parser
* @param openAPI specification being checked
* @return a list of errors (items that are not conform)
*/
public static List<String> getErrorMessages(OpenAPI openAPI) {
List<String> errors = new ArrayList<>();
if (openAPI.getPaths() != null) {
List<String> pathErrors = openAPI.getPaths().keySet().stream()
.filter(path -> !path.startsWith("/"))
.map(path -> String.format(Locale.ROOT, "'%s' must begin with a slash, change it to '/%s'", path, path))
.collect(Collectors.toList());
errors.addAll(pathErrors);
}
return errors;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@

import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.media.*;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.responses.ApiResponses;
import io.swagger.v3.parser.core.models.ParseOptions;

import org.openapitools.codegen.TestUtils;
Expand Down Expand Up @@ -193,4 +196,25 @@ public void testComposedSchemasAreNotUnaliased() {

Assert.assertEquals(refToComposedSchema, ModelUtils.unaliasSchema(allSchemas, refToComposedSchema));
}

@Test
public void testGetErrorMessages() {
//empty case:
OpenAPI openAPI1 = TestUtils.createOpenAPI();
List<String> errors1 = ModelUtils.getErrorMessages(openAPI1);
Assert.assertEquals(errors1.size(), 0);

//wrong path:
OpenAPI openAPI2 = TestUtils.createOpenAPI();
openAPI2.path("some/path", new PathItem().get(new Operation().operationId("op1").responses(new ApiResponses().addApiResponse("201", new ApiResponse().description("OK")))));
List<String> errors2 = ModelUtils.getErrorMessages(openAPI2);
Assert.assertEquals(errors2.size(), 1);
Assert.assertEquals(errors2.get(0), "'some/path' must begin with a slash, change it to '/some/path'");

//correct path:
OpenAPI openAPI3 = TestUtils.createOpenAPI();
openAPI3.path("/path2", new PathItem().get(new Operation().operationId("op1").responses(new ApiResponses().addApiResponse("201", new ApiResponse().description("OK")))));
List<String> errors3 = ModelUtils.getErrorMessages(openAPI3);
Assert.assertEquals(errors3.size(), 0);
}
}