Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request from GHSA-x9rj-m26h-656j
Browse files Browse the repository at this point in the history
* Begin the fix

* Potential road.

* Almost there.  I don't have tests (manually tested)

* Ok, 12 tests don't pass.

* All pass, fix is to not parse includes:  they wil be parsed in second pass.

* Testing

* Testing

* Better error messages, fix requested (can't get test, though).

* Aligned with AMF

* Cycles would have crashed, I think.

* Simpler solution, however it'll need to be adjusted for 0.8

* Added flag for cycle.

* Change distributionManagement to use mule internal repos

* Using factories to abstract where resource created. Not brilliant, but it works.

* Update version to 1.0.49

* Update version to 1.0.50-SNAPSHOT

* Update version to 1.0.50

* Update version to 1.0.51-SNAPSHOT

* A couple of null checks.

* Simple space fixing solution. Actually the simplest solution.

* issue-back-dates (#687)

Options to allow more flexible verification.

* Typo

* Made CompositeResourceLoader constructor public

* Back patch for issue 688.

* Removed the dependency on Paths.

* More publicness.

* nillable simplification

* Bad import slipped in there....

Co-authored-by: anosenzo <alejandro.nosenzo@mulesoft.com>
  • Loading branch information
jpbelang and anosenzo authored Jun 8, 2020
1 parent 5decb5f commit 99730e7
Show file tree
Hide file tree
Showing 48 changed files with 651 additions and 155 deletions.
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.raml</groupId>
<artifactId>raml-parser-parent</artifactId>
<version>1.0.48-SNAPSHOT</version>
<version>1.0.51-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Raml Java Parser 2nd generation parent</name>
<description>Raml parser implemented in java</description>
Expand Down Expand Up @@ -226,15 +226,15 @@

<distributionManagement>
<repository>
<id>mulesoft-releases</id>
<id>mule-ee-releases</id>
<name>MuleSoft Releases Repository</name>
<url>https://repository-master.mulesoft.org/releases/</url>
<url>https://repository-master.mulesoft.org/nexus/content/repositories/ci-releases/</url>
</repository>
<snapshotRepository>
<id>mulesoft-snapshots</id>
<id>mule-ee-snapshots</id>
<name>MuleSoft Snapshots Repository</name>
<url>https://repository-master.mulesoft.org/snapshots/</url>
<url>https://repository-master.mulesoft.org/nexus/content/repositories/ci-snapshots</url>
</snapshotRepository>
</distributionManagement>

</project>
</project>
2 changes: 1 addition & 1 deletion raml-parser-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.raml</groupId>
<artifactId>raml-parser-parent</artifactId>
<version>1.0.48-SNAPSHOT</version>
<version>1.0.51-SNAPSHOT</version>
</parent>
<artifactId>raml-parser-2</artifactId>
<packaging>jar</packaging>
Expand Down
60 changes: 47 additions & 13 deletions raml-parser-2/src/main/java/org/raml/v2/api/RamlModelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nonnull;

import org.apache.commons.io.IOUtils;
import org.raml.v2.api.loader.CompositeResourceLoader;
import org.raml.v2.api.loader.DefaultResourceLoader;
import org.raml.v2.api.loader.FileResourceLoader;
import org.raml.v2.api.loader.ResourceLoader;
import org.raml.v2.api.loader.*;
import org.raml.v2.api.model.PermissiveURI;
import org.raml.v2.api.model.common.ValidationResult;
import org.raml.v2.api.model.v08.parameters.Parameter;
import org.raml.v2.api.model.v10.RamlFragment;
Expand Down Expand Up @@ -69,17 +70,18 @@ public class RamlModelBuilder
{

public static final String MODEL_PACKAGE = "org.raml.v2.internal.impl.commons.model";
private ResourceLoader resourceLoader;
private final ResourceLoaderFactory resourceLoaderFactory;
private RamlBuilder builder = new RamlBuilder();

public RamlModelBuilder()
{
this(new DefaultResourceLoader());
resourceLoaderFactory = ResourceLoaderFactories.defaultResourceLoaderFactory(); // new DefaultResourceLoader(RootDirectoryFileAccessGuard.fromRootDir(Sets.newHashSet("file", "http", "https"),
// ".")));
}

public RamlModelBuilder(ResourceLoader resourceLoader)
public RamlModelBuilder(ResourceLoader resourceLoaderFactory)
{
this.resourceLoader = resourceLoader;
this.resourceLoaderFactory = ResourceLoaderFactories.identityFactory(resourceLoaderFactory);
}

@Nonnull
Expand Down Expand Up @@ -122,8 +124,24 @@ public RamlModelResult buildApi(String content, String ramlLocation)
{
return buildApi(ramlLocation);
}
Node ramlNode = builder.build(content, resourceLoader, ramlLocation);
return generateRamlApiResult(ramlNode, getFragment(content));

if (ramlLocation.matches("^[a-z]+:.*"))
{

String actualName = PermissiveURI.create(ramlLocation).toString();
if (ramlLocation.startsWith("file:"))
{
actualName = new File(PermissiveURI.create(ramlLocation).getPath()).getParent();
}
Node ramlNode = builder.build(content, resourceLoaderFactory.createResourceLoader(actualName), ramlLocation);
return generateRamlApiResult(ramlNode, getFragment(content));
}
else
{

Node ramlNode = builder.build(content, resourceLoaderFactory.createResourceLoader(Paths.get(ramlLocation).toAbsolutePath().getParent().toString()), ramlLocation);
return generateRamlApiResult(ramlNode, getFragment(content));
}
}

private RamlFragment getFragment(String content)
Expand Down Expand Up @@ -249,7 +267,8 @@ private String getRamlContent(File ramlFile)
{
return null;
}
ResourceLoader fileLoader = new CompositeResourceLoader(resourceLoader, new FileResourceLoader(ramlFile.getParent()));

ResourceLoader fileLoader = resourceLoaderFactory.createResourceLoader(ramlFile.getAbsoluteFile().getParent());
return getRamlContent(ramlFile.getName(), fileLoader);
}

Expand All @@ -275,7 +294,23 @@ private String getRamlContent(Reader ramlReader)

private String getRamlContent(String ramlLocation)
{
return getRamlContent(ramlLocation, resourceLoader);

if (ramlLocation == null)
{

return null;
}

if (ramlLocation.startsWith("file:"))
{
Path p = Paths.get(URI.create(ramlLocation)).toAbsolutePath().getParent();
return getRamlContent(ramlLocation, resourceLoaderFactory.createResourceLoader(p.toString()));
}
else
{
Path p = Paths.get(ramlLocation).toAbsolutePath().getParent();
return getRamlContent(ramlLocation, resourceLoaderFactory.createResourceLoader(p.toString()));
}
}

private String getRamlContent(String ramlLocation, ResourceLoader loader)
Expand All @@ -291,5 +326,4 @@ private String getRamlContent(String ramlLocation, ResourceLoader loader)
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2013 (c) MuleSoft, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.raml.v2.api.model;

import java.net.URI;

/**
* Created. There, you have it.
*/
public class PermissiveURI
{

public static URI create(String s)
{

return URI.create(s.replace(" ", "%20"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,9 @@
import java.io.StringReader;
import java.util.List;

import com.google.common.collect.Sets;
import org.apache.commons.io.IOUtils;
import org.raml.v2.api.loader.CacheResourceLoader;
import org.raml.v2.api.loader.CompositeResourceLoader;
import org.raml.v2.api.loader.DefaultResourceLoader;
import org.raml.v2.api.loader.ResourceLoader;
import org.raml.v2.api.loader.RootRamlFileResourceLoader;
import org.raml.v2.api.loader.RootRamlUrlResourceLoader;
import org.raml.v2.api.loader.*;
import org.raml.v2.internal.impl.commons.RamlHeader;
import org.raml.v2.internal.impl.v08.Raml08Builder;
import org.raml.v2.internal.impl.v10.Raml10Builder;
Expand Down Expand Up @@ -60,8 +56,6 @@ public class RamlBuilder

private int maxPhaseNumber;

private ResourceLoader resourceLoader = null;

private String actualPath = null;

public RamlBuilder()
Expand All @@ -76,7 +70,7 @@ public RamlBuilder(int maxPhaseNumber)

public Node build(File ramlFile)
{
return build(ramlFile, new DefaultResourceLoader());
return build(ramlFile, ResourceLoaderFactories.defaultResourceLoaderFactory().createResourceLoader(ramlFile.getAbsoluteFile().getParent()));
}


Expand Down Expand Up @@ -160,22 +154,24 @@ public Node build(Reader content, ResourceLoader resourceLoader, String resource
private ResourceLoader addRootRamlResourceLoaders(ResourceLoader resourceLoader, String resourceLocation)
{

File parentFile = new File(actualPath != null ? actualPath : resourceLocation).getParentFile();
if (parentFile != null)
{
resourceLoader = new CompositeResourceLoader(new RootRamlFileResourceLoader(parentFile), resourceLoader);
}
String rootRamlPath = getRootPath(resourceLocation);
if (!Strings.isNullOrEmpty(rootRamlPath))
{
resourceLoader = new CompositeResourceLoader(new RootRamlUrlResourceLoader(rootRamlPath), resourceLoader);
}
return resourceLoader;

// File parentFile = new File(actualPath != null ? actualPath : resourceLocation).getParentFile();
// if (parentFile != null) {
// resourceLoader = CompositeResourceLoader.compose(new RootRamlFileResourceLoader(parentFile), resourceLoader);
// }
// String rootRamlPath = getRootPath(resourceLocation);
// if (!Strings.isNullOrEmpty(rootRamlPath)) {
// resourceLoader =
// CompositeResourceLoader.compose(new RootRamlUrlResourceLoader(rootRamlPath), resourceLoader);
// }
//
// this.resourceLoader = new CacheResourceLoader(resourceLoader);
// return this.resourceLoader;

this.resourceLoader = new CacheResourceLoader(resourceLoader);
return this.resourceLoader;
}

private String getRootPath(String rootRamlFileUrl)
private static String getRootPath(String rootRamlFileUrl)
{
final List<String> urlSegments = Splitter.on("/").splitToList(rootRamlFileUrl);
if (urlSegments.isEmpty())
Expand All @@ -190,15 +186,15 @@ private String normalizeResourceLocation(String resourceLocation)
return resourceLocation.replace("\\", "/");
}

public ResourceLoader getResourceLoader()
{
return this.resourceLoader;
}

public String getActualPath()
{
return actualPath;
}

public static void main(String[] args)
{

System.err.println(getRootPath("file:/fun/goo/a"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.google.common.collect.Sets;
import org.raml.v2.api.loader.DefaultResourceLoader;
import org.raml.v2.api.loader.ResourceLoader;
import org.raml.yagi.framework.grammar.rule.Rule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public List<RamlValidationResult> validate(String payload)
typeDeclarationNode.addChild(new KeyValueNodeImpl(new StringNodeImpl("type"), getResolvedType().getTypeExpressionNode()));
}

final ResourceLoader resourceLoader = typeDeclarationNode.getStartPosition().getResourceLoader();
final ResourceLoader resourceLoader = typeDeclarationNode.getStartPosition().createResourceLoader();
final ExampleValidationPhase exampleValidationPhase = new ExampleValidationPhase(resourceLoader);
final Node validate = exampleValidationPhase.validate(typeDeclarationNode, payload);
if (NodeUtils.isErrorResult(validate))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public boolean isRequired()

public KeyValueRule getFacetRule()
{
final TypeToRuleVisitor typeToRuleVisitor = new TypeToRuleVisitor(new DefaultResourceLoader());
final TypeToRuleVisitor typeToRuleVisitor = new TypeToRuleVisitor(getStartPosition().createResourceLoader());
final Rule value = getFacetType().getResolvedType().visit(typeToRuleVisitor);
return new KeyValueRule(new StringValueRule(getFacetName()), value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private Node copy(Node node)
if (node instanceof NullNode)
{
node = new SYObjectNode(new MappingNode(Tag.MAP, new ArrayList<NodeTuple>(), DumperOptions.FlowStyle.AUTO),
node.getStartPosition().getResourceLoader(),
node.getStartPosition().createResourceLoader(),
node.getStartPosition().getPath());
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ else if ((mightBeAnObjectType(resolvedType) || (resolvedType instanceof ArrayRes
return validateJson(exampleValue, resolvedType, value);
}
}

if (exampleValue instanceof ErrorNode)
{
return exampleValue;
}

if (exampleValue != null)
{
final Rule rule = visitAppropriately(resolvedType);
Expand All @@ -145,10 +151,14 @@ else if ((mightBeAnObjectType(resolvedType) || (resolvedType instanceof ArrayRes
return null;
}

private Rule visitAppropriately(ResolvedType resolvedType) {
if (NILLABLE_TYPES) {
private Rule visitAppropriately(ResolvedType resolvedType)
{
if (NILLABLE_TYPES)
{
return new AnyOfRule(resolvedType.visit(new TypeToRuleVisitor(resourceLoader, false)), new NullValueRule());
} else {
}
else
{

return resolvedType.visit(new TypeToRuleVisitor(resourceLoader, false));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2013 (c) MuleSoft, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.raml.v2.api.model;

import org.junit.Test;

import java.net.URI;
import java.nio.file.Paths;

import static org.junit.Assert.*;

/**
* Created. There, you have it.
*/
public class PermissiveURITest
{

@Test
public void simple() throws Exception
{

URI i = PermissiveURI.create("file:/funk");
assertEquals("/funk", Paths.get(i).toString());
}

@Test
public void undone() throws Exception
{

URI i = PermissiveURI.create("file:/file important");
assertEquals("/file important", Paths.get(i).toString());
}
}
Loading

0 comments on commit 99730e7

Please sign in to comment.