-
Notifications
You must be signed in to change notification settings - Fork 4
APIKIT-2511: Move validation to Parser Wrapper #64
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
Open
JuanAller
wants to merge
1
commit into
main
Choose a base branch
from
validator-poc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions
8
parser-service/src/test/resources/apis/api-with-exchange/08/exchange_modules/base.raml
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
8 changes: 4 additions & 4 deletions
8
parser-service/src/test/resources/apis/api-with-references/08/resourceTypes/base.raml
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ types: | |
get: | ||
responses: | ||
200: | ||
body: | ||
body: | ||
application/json: | ||
type: !include schemas/atom.xsd | ||
|
||
|
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 |
---|---|---|
|
@@ -11,7 +11,7 @@ types: | |
get: | ||
responses: | ||
200: | ||
body: | ||
body: | ||
application/json: | ||
|
||
|
||
|
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
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,61 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>raml-parser-interface-parent</artifactId> | ||
<groupId>org.mule.apikit</groupId> | ||
<version>2.1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>validation</artifactId> | ||
|
||
<properties> | ||
<http.connector.version>1.5.6</http.connector.version> | ||
<mule.version>4.1.5</mule.version> | ||
<httpmime.version>4.5.10</httpmime.version> | ||
<fileupload.version>1.4</fileupload.version> | ||
|
||
<licensePath>../LICENSE_HEADER.txt</licensePath> | ||
<formatterConfigPath>../formatter.xml</formatterConfigPath> | ||
<sonar.jacoco.reportPaths>../target/jacoco.exec</sonar.jacoco.reportPaths> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.mule.apikit</groupId> | ||
<artifactId>parser-service</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mule.connectors</groupId> | ||
<artifactId>mule-http-connector</artifactId> | ||
<classifier>mule-plugin</classifier> | ||
<version>${http.connector.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mule.runtime</groupId> | ||
<artifactId>mule-core</artifactId> | ||
<version>${mule.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.httpcomponents</groupId> | ||
<artifactId>httpmime</artifactId> | ||
<version>${httpmime.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-fileupload</groupId> | ||
<artifactId>commons-fileupload</artifactId> | ||
<version>${fileupload.version}</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>commons-io</groupId> | ||
<artifactId>commons-io</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
73 changes: 73 additions & 0 deletions
73
validation/src/main/java/org/mule/validation/ActionValidator.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,73 @@ | ||
package org.mule.validation;/* | ||
* Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com | ||
* The software in this package is published under the terms of the CPAL v1.0 | ||
* license, a copy of which has been included with this distribution in the | ||
* LICENSE.txt file. | ||
*/ | ||
import org.mule.validation.attributes.AttributesValidator; | ||
import org.mule.validation.attributes.DefaultsHandler; | ||
import org.mule.validation.attributes.HeadersDefaultsHandler; | ||
import org.mule.validation.attributes.HeadersValidator; | ||
import org.mule.validation.attributes.QueryParameterValidator; | ||
import org.mule.validation.attributes.QueryParamsDefaultsHandler; | ||
import org.mule.validation.attributes.QueryStringValidator; | ||
import org.mule.validation.attributes.UriParametersValidator; | ||
import org.mule.validation.body.BodyValidator; | ||
import org.mule.validation.body.BodyValidatorFactory; | ||
import com.google.common.collect.ImmutableList; | ||
import org.mule.validation.config.Configuration; | ||
import org.mule.validation.exception.BadRequestException; | ||
import org.mule.validation.exception.NotAcceptableException; | ||
import org.mule.validation.exception.UnsupportedMediaTypeException; | ||
import java.util.List; | ||
import org.mule.apikit.model.Action; | ||
import org.mule.extension.http.api.HttpRequestAttributes; | ||
import org.mule.runtime.api.metadata.TypedValue; | ||
|
||
public class ActionValidator { | ||
// TODO : cache org.mule.validation.body validator | ||
private final List<AttributesValidator> attributesValidators; | ||
private final List<DefaultsHandler> defaultsHandlers; | ||
private final BodyValidatorFactory bodyValidatorFactory; | ||
|
||
public ActionValidator(Action action, Configuration config) { | ||
this.attributesValidators = initialiseValidators(action, config); | ||
this.defaultsHandlers = initialiseDefaultsHandlers(action); | ||
this.bodyValidatorFactory = new BodyValidatorFactory(action, config.getApiSpecification(), config.getExpressionManager()); | ||
} | ||
|
||
public HttpRequestAttributes validateAttributes(HttpRequestAttributes attributes) | ||
throws BadRequestException, NotAcceptableException { | ||
|
||
HttpRequestAttributes withDefaults = attributes; | ||
|
||
for (DefaultsHandler defaultsHandler : defaultsHandlers) { | ||
withDefaults = defaultsHandler.addDefaultValues(withDefaults); | ||
} | ||
|
||
for (AttributesValidator attributesValidator : attributesValidators) { | ||
attributesValidator.validate(withDefaults); | ||
} | ||
return withDefaults; | ||
} | ||
|
||
public TypedValue validateBody(String requestMimeType, String charset, TypedValue payload) | ||
throws UnsupportedMediaTypeException, BadRequestException { | ||
BodyValidator bodyValidator = bodyValidatorFactory.resolveValidator(requestMimeType); | ||
return bodyValidator.validate(charset, payload); | ||
} | ||
|
||
private List<AttributesValidator> initialiseValidators(Action action, Configuration configuration) { | ||
return ImmutableList.of( | ||
new UriParametersValidator(action), | ||
new HeadersValidator(action, configuration.isHeadersStrictValidation()), | ||
new QueryStringValidator(action), | ||
new QueryParameterValidator(action, configuration.isQueryParamsStrictValidation())); | ||
} | ||
|
||
private List<DefaultsHandler> initialiseDefaultsHandlers(Action action) { | ||
return ImmutableList.of( | ||
new QueryParamsDefaultsHandler(action), | ||
new HeadersDefaultsHandler(action)); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
validation/src/main/java/org/mule/validation/RequestValidator.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,66 @@ | ||
/* | ||
* Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com | ||
* The software in this package is published under the terms of the CPAL v1.0 | ||
* license, a copy of which has been included with this distribution in the | ||
* LICENSE.txt file. | ||
*/ | ||
package org.mule.validation; | ||
|
||
import static org.mule.validation.helpers.AttributesHelper.getMediaType; | ||
import static org.mule.validation.helpers.CharsetUtils.getCharset; | ||
import static org.mule.validation.helpers.UrlUtils.getRequestPath; | ||
|
||
import org.mule.validation.config.Configuration; | ||
import org.mule.validation.exception.BadRequestException; | ||
import org.mule.validation.exception.NotAcceptableException; | ||
import org.mule.validation.exception.ResourceNotFoundException; | ||
import org.mule.validation.exception.UnsupportedMediaTypeException; | ||
import java.util.Map; | ||
import javafx.util.Pair; | ||
import org.apache.commons.collections.MapUtils; | ||
import org.mule.apikit.model.Action; | ||
import org.mule.extension.http.api.HttpRequestAttributes; | ||
import org.mule.extension.http.api.HttpRequestAttributesBuilder; | ||
import org.mule.runtime.api.metadata.TypedValue; | ||
|
||
public class RequestValidator { | ||
// TODO : cache actions validators | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add ticket There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done , related to APIKIT-2511 |
||
private final Configuration config; | ||
private final ResourceFinder resourceFinder; | ||
|
||
public RequestValidator(Configuration config) { | ||
this.config = config; | ||
this.resourceFinder = new ResourceFinder(config.getApiSpecification()); | ||
} | ||
|
||
public ValidRequest validateRequest(HttpRequestAttributes attributes, TypedValue payload) | ||
throws ResourceNotFoundException, BadRequestException, NotAcceptableException, UnsupportedMediaTypeException { | ||
|
||
String path = getRequestPath(attributes); | ||
|
||
Pair<Action, Map<String, String>> pair = resourceFinder.findAction(attributes.getMethod().toLowerCase(), path); | ||
|
||
HttpRequestAttributes attributesWithUriParams = addUriParams(attributes, pair.getValue()); | ||
|
||
ActionValidator actionValidator = new ActionValidator(pair.getKey(), config); | ||
|
||
HttpRequestAttributes validatedAttributes = actionValidator.validateAttributes(attributesWithUriParams); | ||
|
||
if (pair.getKey().hasBody()) { | ||
payload = actionValidator | ||
.validateBody(getMediaType(attributes), getCharset(attributes.getHeaders(), payload), | ||
payload); | ||
} | ||
|
||
return new ValidRequest(validatedAttributes, payload); | ||
} | ||
|
||
|
||
private HttpRequestAttributes addUriParams(HttpRequestAttributes attributes, Map<String, String> uriParams) { | ||
if (MapUtils.isEmpty(uriParams)) { | ||
return attributes; | ||
} | ||
return new HttpRequestAttributesBuilder(attributes).uriParams(uriParams).build(); | ||
} | ||
|
||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add ticket related to this task
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done , related to APIKIT-2511