-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
399 additions
and
18 deletions.
There are no files selected for viewing
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
11 changes: 9 additions & 2 deletions
11
...i/src/main/java/com/getyourguide/openapi/validation/api/model/ValidatorConfiguration.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
package com.getyourguide.openapi.validation.api.model; | ||
|
||
import com.getyourguide.openapi.validation.api.log.LogLevel; | ||
import java.util.List; | ||
import java.util.Map; | ||
import lombok.Builder; | ||
import java.util.regex.Pattern; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Builder | ||
@AllArgsConstructor | ||
@Getter | ||
public class ValidatorConfiguration { | ||
private final LogLevel levelResolverDefaultLevel; | ||
private final Map<String, LogLevel> levelResolverLevels; | ||
|
||
private final List<PathPatternSpec> specificationPaths; | ||
|
||
public record PathPatternSpec(Pattern pathPattern, String specificationFilePath) { | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...ain/java/com/getyourguide/openapi/validation/api/model/ValidatorConfigurationBuilder.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.getyourguide.openapi.validation.api.model; | ||
|
||
import com.getyourguide.openapi.validation.api.log.LogLevel; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.regex.Pattern; | ||
|
||
public class ValidatorConfigurationBuilder { | ||
private LogLevel levelResolverDefaultLevel; | ||
private Map<String, LogLevel> levelResolverLevels; | ||
private List<ValidatorConfiguration.PathPatternSpec> specificationPaths; | ||
|
||
public ValidatorConfigurationBuilder levelResolverDefaultLevel(LogLevel levelResolverDefaultLevel) { | ||
this.levelResolverDefaultLevel = levelResolverDefaultLevel; | ||
return this; | ||
} | ||
|
||
public ValidatorConfigurationBuilder levelResolverLevel(String messageKey, LogLevel level) { | ||
if (this.levelResolverLevels == null) { | ||
this.levelResolverLevels = new HashMap<>(); | ||
} | ||
this.levelResolverLevels.put(messageKey, level); | ||
return this; | ||
} | ||
|
||
public ValidatorConfigurationBuilder specificationPath(Pattern pathPattern, String specPath) { | ||
if (this.specificationPaths == null) { | ||
this.specificationPaths = new ArrayList<>(); | ||
} | ||
this.specificationPaths.add(new ValidatorConfiguration.PathPatternSpec(pathPattern, specPath)); | ||
return this; | ||
} | ||
|
||
public ValidatorConfiguration build() { | ||
return new ValidatorConfiguration( | ||
levelResolverDefaultLevel, | ||
levelResolverLevels, | ||
specificationPaths | ||
); | ||
} | ||
|
||
public String toString() { | ||
return "ValidatorConfigurationBuilder(" | ||
+ "levelResolverDefaultLevel=" + this.levelResolverDefaultLevel + ", " | ||
+ "levelResolverLevels=" + this.levelResolverLevels | ||
+ ")"; | ||
} | ||
} |
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
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
125 changes: 125 additions & 0 deletions
125
...ide/openapi/validation/core/validator/MultipleSpecOpenApiInteractionValidatorWrapper.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package com.getyourguide.openapi.validation.core.validator; | ||
|
||
import com.atlassian.oai.validator.model.Request; | ||
import com.atlassian.oai.validator.model.SimpleRequest; | ||
import com.atlassian.oai.validator.model.SimpleResponse; | ||
import com.atlassian.oai.validator.report.ValidationReport; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.regex.Pattern; | ||
import javax.annotation.Nonnull; | ||
import lombok.AllArgsConstructor; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
|
||
public class MultipleSpecOpenApiInteractionValidatorWrapper implements OpenApiInteractionValidatorWrapper { | ||
public static final String MESSAGE_KEY_VALIDATOR_FOUND = "zopenapi-validator-java.noValidatorFound"; | ||
private final List<Pair<Pattern, OpenApiInteractionValidatorWrapper>> validators; | ||
|
||
public MultipleSpecOpenApiInteractionValidatorWrapper( | ||
List<Pair<Pattern, OpenApiInteractionValidatorWrapper>> validators | ||
) { | ||
assert validators != null && validators.size() > 0; | ||
|
||
this.validators = validators; | ||
} | ||
|
||
@Override | ||
public ValidationReport validateRequest(SimpleRequest request) { | ||
return getValidatorForPath(request.getPath()) | ||
.map(validator -> validator.validateRequest(request)) | ||
.orElse(new SimpleValidationReport(List.of(buildNoValidatorFoundMessage(request.getPath())))); | ||
} | ||
|
||
@Override | ||
public ValidationReport validateResponse(String path, Request.Method method, SimpleResponse response) { | ||
return getValidatorForPath(path) | ||
.map(validator -> validator.validateResponse(path, method, response)) | ||
.orElse(new SimpleValidationReport(List.of(buildNoValidatorFoundMessage(path)))); | ||
} | ||
|
||
private Optional<OpenApiInteractionValidatorWrapper> getValidatorForPath(String path) { | ||
for (var validator : validators) { | ||
if (validator.getLeft().matcher(path).matches()) { | ||
return Optional.of(validator.getRight()); | ||
} | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
private static SimpleMessage buildNoValidatorFoundMessage(String path) { | ||
return new SimpleMessage( | ||
MESSAGE_KEY_VALIDATOR_FOUND, | ||
"No validator found for path: " + path, | ||
ValidationReport.Level.WARN | ||
); | ||
} | ||
|
||
@AllArgsConstructor | ||
private static class SimpleValidationReport implements ValidationReport { | ||
private final List<Message> messages; | ||
|
||
@Nonnull | ||
@Override | ||
public List<Message> getMessages() { | ||
return messages; | ||
} | ||
|
||
@Override | ||
public ValidationReport withAdditionalContext(MessageContext context) { | ||
return this; | ||
} | ||
} | ||
|
||
@AllArgsConstructor | ||
private static class SimpleMessage implements ValidationReport.Message { | ||
private final String key; | ||
private final String message; | ||
private final ValidationReport.Level level; | ||
|
||
@Override | ||
public String getKey() { | ||
return key; | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
@Override | ||
public ValidationReport.Level getLevel() { | ||
return level; | ||
} | ||
|
||
@Override | ||
public List<String> getAdditionalInfo() { | ||
return List.of(); | ||
} | ||
|
||
@Override | ||
public Optional<ValidationReport.MessageContext> getContext() { | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public ValidationReport.Message withLevel(ValidationReport.Level level) { | ||
return this; | ||
} | ||
|
||
@Override | ||
public ValidationReport.Message withAdditionalInfo(String info) { | ||
return this; | ||
} | ||
|
||
@Override | ||
public ValidationReport.Message withAdditionalContext(ValidationReport.MessageContext context) { | ||
return this; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return message; | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...om/getyourguide/openapi/validation/core/validator/OpenApiInteractionValidatorWrapper.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.getyourguide.openapi.validation.core.validator; | ||
|
||
import com.atlassian.oai.validator.model.Request; | ||
import com.atlassian.oai.validator.model.SimpleRequest; | ||
import com.atlassian.oai.validator.model.SimpleResponse; | ||
import com.atlassian.oai.validator.report.ValidationReport; | ||
|
||
public interface OpenApiInteractionValidatorWrapper { | ||
ValidationReport validateRequest(SimpleRequest request); | ||
|
||
ValidationReport validateResponse(String path, Request.Method method, SimpleResponse response); | ||
} |
Oops, something went wrong.