-
Notifications
You must be signed in to change notification settings - Fork 1
Support multiple spec files #2
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
01bad71
Refactoring: Log level configuration
pboos f5f13a2
Formatting
pboos 81ee432
Remove unused import
pboos 47ed9d5
Refactoring: Wrap OpenApiInteractionValidator usage within OpenApiInt…
pboos 23a03c6
Add MultipleSpecOpenApiInteractionValidatorWrapper
pboos 992954e
Use MultipleSpecOpenApiInteractionValidatorWrapper based on configura…
pboos ae16248
Remove OpenApiValidationConfiguration (with `@AutoConfiguration`)
pboos 9403443
Remove unused imports
pboos c741e6e
Update README.md with instructions
pboos 55177a4
Remove unnecessary empty line
pboos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.