Skip to content

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -16,19 +16,19 @@ types:
enum: [SFO, LAX, CLE]
responses:
200:
body:
body:
application/json:
type: AmericanFlight[]
examples: !include exchange_modules/68ef9520-24e9-4cf2-b2f5-620025690913/training-american-flights-example/1.0.1/AmericanFlightsExample.raml
post:
displayName: Add a flight
body:
body:
application/json:
type: AmericanFlight
examples: !include examples/AmericanFlightNoIDExample.raml
responses:
201:
body:
body:
application/json:
example:
message: Flight added (but not really)
Expand All @@ -42,7 +42,7 @@ types:
displayName: Get a flight by ID
responses:
200:
body:
body:
application/json:
type: AmericanFlight
examples: !include examples/AmericanFlightExample.raml
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
get?:
responses:
200:
body:
body:
application/atom+xml:
schema: atom
post?:
responses:
200?:
body:
body:
application/atom+xml:
schema: atom
201?:
body:
body:
application/atom+xml:
schema: atom
put?:
responses:
200:
body:
body:
application/atom+xml:
schema: atom
412:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
get?:
responses:
200:
body:
body:
application/atom+xml:
schema: atom
post?:
responses:
200?:
body:
body:
application/atom+xml:
schema: atom
201?:
body:
body:
application/atom+xml:
schema: atom
put?:
responses:
200:
body:
body:
application/atom+xml:
schema: atom
412:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ types:
get:
responses:
200:
body:
body:
application/json:
type: !include schemas/atom.xsd

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ types:
get:
responses:
200:
body:
body:
application/json:


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ get?:
200:
description: |
Request succeeded
body:
body:
text/xml:
application/json:
schema: !include ../more spaces/schema.json
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<module>interface-impl-v2</module>
<module>interface-impl-amf</module>
<module>parser-service</module>
<module>validation</module>
</modules>

<properties>
Expand Down
61 changes: 61 additions & 0 deletions validation/pom.xml
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 validation/src/main/java/org/mule/validation/ActionValidator.java
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
Copy link
Contributor

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

Copy link
Contributor Author

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

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 validation/src/main/java/org/mule/validation/RequestValidator.java
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add ticket

Copy link
Contributor Author

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

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();
}

}
Loading