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

Commit

Permalink
Fixes #444 Validation fails when using uses inside a DataType fragmen…
Browse files Browse the repository at this point in the history
…t. (#445)

* Fixes #444 Validation fails when using uses inside a DataType fragment.

* Code review changes.

* Full qualified name removed.
  • Loading branch information
pacafuentes authored Dec 28, 2017
1 parent 2b536aa commit 23623e3
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
*/
package org.raml.v2.internal.impl.commons;

import static org.raml.v2.internal.impl.commons.RamlVersion.RAML_08;
import static org.raml.v2.internal.impl.commons.RamlVersion.RAML_10;

import java.util.StringTokenizer;

import javax.annotation.Nullable;

import org.raml.v2.api.model.v10.RamlFragment;
import org.raml.v2.internal.impl.commons.nodes.TypeDeclarationNodeFragment;
import org.raml.v2.internal.impl.v08.grammar.Raml08Grammar;
import org.raml.v2.internal.impl.v10.grammar.Raml10Grammar;
import org.raml.v2.internal.utils.StreamUtils;
import org.raml.yagi.framework.grammar.rule.Rule;

import javax.annotation.Nullable;
import java.util.StringTokenizer;

import static org.raml.v2.internal.impl.commons.RamlVersion.RAML_08;
import static org.raml.v2.internal.impl.commons.RamlVersion.RAML_10;

public class RamlHeader
{

Expand Down Expand Up @@ -124,7 +124,7 @@ public static Rule getFragmentRule(RamlFragment fragment)
return grammar.documentation()
.with(0, grammar.usesField());
case DataType:
return grammar.explicitType().with(0, grammar.usesField());
return grammar.explicitType().with(0, grammar.usesField()).then(TypeDeclarationNodeFragment.class);
case NamedExample:
return grammar.exampleFragment();
case ResourceType:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@
*/
package org.raml.v2.internal.impl.commons.nodes;

import org.raml.yagi.framework.nodes.AbstractObjectNode;
import org.raml.yagi.framework.nodes.ContextProviderNode;
import org.raml.yagi.framework.nodes.Node;
import org.raml.v2.api.model.v10.RamlFragment;
import org.raml.v2.internal.impl.commons.phase.RamlTypedFragment;
import org.raml.v2.internal.impl.v10.grammar.Raml10Grammar;
import org.raml.yagi.framework.nodes.AbstractObjectNode;
import org.raml.yagi.framework.nodes.Node;
import org.raml.yagi.framework.util.NodeSelector;

import javax.annotation.Nonnull;

public class RamlTypedFragmentNode extends AbstractObjectNode implements LibraryNodeProvider, ContextProviderNode
public class DefaultRamlTypedFragment extends AbstractObjectNode implements RamlTypedFragment
{

private final RamlFragment fragment;
private Node libraryNode;

public RamlTypedFragmentNode(RamlFragment fragment)
public DefaultRamlTypedFragment(RamlFragment fragment)
{
this.fragment = fragment;
}

private RamlTypedFragmentNode(RamlTypedFragmentNode node)
protected DefaultRamlTypedFragment(DefaultRamlTypedFragment node)
{
super(node);
this.fragment = node.getFragment();
Expand All @@ -46,7 +46,7 @@ private RamlTypedFragmentNode(RamlTypedFragmentNode node)
@Override
public Node copy()
{
return new RamlTypedFragmentNode(this);
return new DefaultRamlTypedFragment(this);
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.internal.impl.commons.nodes;

import org.raml.v2.api.model.v10.RamlFragment;
import org.raml.v2.internal.impl.commons.phase.RamlTypedFragment;
import org.raml.v2.internal.impl.v10.grammar.Raml10Grammar;
import org.raml.yagi.framework.nodes.Node;
import org.raml.yagi.framework.util.NodeSelector;
import org.raml.yagi.framework.util.NodeUtils;

import javax.annotation.Nonnull;

import static org.raml.v2.api.model.v10.RamlFragment.DataType;


public class TypeDeclarationNodeFragment extends TypeDeclarationNode implements RamlTypedFragment
{

private Node libraryNode;

@Nonnull
@Override
public Node getContextNode()
{
return getLibraryNode() != null || getParent() == null ? this : NodeUtils.getContextNode(getParent());
}

@Override
public Node getLibraryNode()
{
if (libraryNode == null)
{
libraryNode = NodeSelector.selectFrom(Raml10Grammar.USES_KEY_NAME, this);
}
return libraryNode;
}

public void
resolveLibraryReference()
{
libraryNode = NodeSelector.selectFrom(Raml10Grammar.USES_KEY_NAME, this);
if (libraryNode != null)
{
// The parent is the key value pair
final Node parent = libraryNode.getParent();
removeChild(parent);
}
}

@Override
public RamlFragment getFragment()
{
return DataType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@
*/
package org.raml.v2.internal.impl.commons.phase;

import java.io.InputStream;
import java.net.URI;

import org.apache.commons.io.IOUtils;
import org.raml.v2.api.loader.ResourceLoader;
import org.raml.v2.api.loader.ResourceLoaderExtended;
import org.raml.v2.api.loader.ResourceUriCallback;
import org.raml.yagi.framework.nodes.AbstractRamlNode;
import org.raml.yagi.framework.nodes.NullNodeImpl;
import org.raml.yagi.framework.nodes.ObjectNode;
import org.raml.v2.api.model.v10.RamlFragment;
import org.raml.v2.internal.impl.commons.RamlHeader;
import org.raml.v2.api.loader.ResourceLoader;
import org.raml.v2.internal.impl.commons.nodes.DefaultRamlTypedFragment;
import org.raml.v2.internal.utils.ResourcePathUtils;
import org.raml.v2.internal.utils.StreamUtils;
import org.raml.yagi.framework.nodes.AbstractRamlNode;
import org.raml.yagi.framework.nodes.IncludeErrorNode;
import org.raml.yagi.framework.nodes.Node;
import org.raml.yagi.framework.nodes.NullNodeImpl;
import org.raml.yagi.framework.nodes.ObjectNode;
import org.raml.yagi.framework.nodes.Position;
import org.raml.yagi.framework.nodes.StringNodeImpl;
import org.raml.yagi.framework.nodes.snakeyaml.NodeParser;
import org.raml.yagi.framework.nodes.snakeyaml.SYIncludeNode;
import org.raml.yagi.framework.phase.Transformer;
import org.raml.v2.internal.impl.commons.nodes.RamlTypedFragmentNode;
import org.raml.v2.api.model.v10.RamlFragment;
import org.raml.v2.internal.utils.ResourcePathUtils;
import org.raml.v2.internal.utils.StreamUtils;

import java.io.InputStream;
import java.net.URI;


public class IncludeResolver implements Transformer, ResourceUriCallback
Expand Down Expand Up @@ -89,7 +89,7 @@ public Node transform(Node node)
result = NodeParser.parse(resourceLoader, resourcePath, includeContent);
if (result != null && isTypedFragment(result, fragment))
{
final RamlTypedFragmentNode newNode = new RamlTypedFragmentNode(fragment);
final DefaultRamlTypedFragment newNode = new DefaultRamlTypedFragment(fragment);
result.replaceWith(newNode);
result = newNode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,24 @@
*/
package org.raml.v2.internal.impl.commons.phase;

import static org.raml.v2.internal.utils.PhaseUtils.applyPhases;

import org.raml.v2.api.loader.ResourceLoader;
import org.raml.v2.internal.impl.v10.Raml10Builder;
import org.raml.v2.internal.impl.commons.RamlHeader;
import org.raml.yagi.framework.grammar.rule.Rule;
import org.raml.yagi.framework.nodes.Node;
import org.raml.yagi.framework.phase.TransformationPhase;
import org.raml.yagi.framework.phase.Transformer;
import org.raml.v2.internal.impl.commons.RamlHeader;
import org.raml.v2.internal.impl.commons.nodes.RamlTypedFragmentNode;
import org.raml.v2.internal.impl.v10.phase.LibraryLinkingTransformation;

public class RamlFragmentGrammarTransformer implements Transformer
{

private final Raml10Builder builder;
private final ResourceLoader resourceLoader;

public RamlFragmentGrammarTransformer(Raml10Builder builder, ResourceLoader resourceLoader)
{
this.builder = builder;
this.resourceLoader = resourceLoader;
}

@Override
public boolean matches(Node node)
{
return node instanceof RamlTypedFragmentNode;
return node instanceof RamlTypedFragment && node.getParent() != null;
}

@Override
public Node transform(Node node)
{
final RamlTypedFragmentNode ramlTypedFragmentNode = (RamlTypedFragmentNode) node;
final RamlTypedFragment ramlTypedFragmentNode = (RamlTypedFragment) node;
final Rule rule = RamlHeader.getFragmentRule(ramlTypedFragmentNode.getFragment());
node = rule.apply(node);
final Node apply = applyPhases(node, new TransformationPhase(new LibraryLinkingTransformation(builder, resourceLoader)));
if (apply instanceof RamlTypedFragmentNode)
{
// Hack!!!!
((RamlTypedFragmentNode) apply).resolveLibraryReference();
}
return apply;
return rule.apply(node);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.internal.impl.commons.phase;

import org.raml.v2.api.loader.ResourceLoader;
import org.raml.v2.internal.impl.v10.Raml10Builder;
import org.raml.v2.internal.impl.v10.phase.LibraryLinkingTransformation;
import org.raml.v2.internal.impl.v10.phase.ReferenceResolverTransformer;
import org.raml.yagi.framework.nodes.Node;
import org.raml.yagi.framework.phase.TransformationPhase;
import org.raml.yagi.framework.phase.Transformer;

import static org.raml.v2.internal.utils.PhaseUtils.applyPhases;

public class RamlFragmentLibraryLinkingTransformer implements Transformer
{

private final Raml10Builder builder;
private final ResourceLoader resourceLoader;

public RamlFragmentLibraryLinkingTransformer(Raml10Builder builder, ResourceLoader resourceLoader)
{
this.builder = builder;
this.resourceLoader = resourceLoader;
}

@Override
public boolean matches(Node node)
{
return node instanceof RamlTypedFragment && node.getParent() != null;
}

@Override
public Node transform(Node node)
{
Node apply = applyPhases(node, new TransformationPhase(new LibraryLinkingTransformation(builder, resourceLoader)));

if (apply instanceof RamlTypedFragment)
{
// Hack!!!!
((RamlTypedFragment) apply).resolveLibraryReference();
}
apply = applyPhases(apply, new TransformationPhase(new ReferenceResolverTransformer()));
return apply;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.internal.impl.commons.phase;

import org.raml.v2.api.model.v10.RamlFragment;
import org.raml.v2.internal.impl.commons.nodes.LibraryNodeProvider;
import org.raml.yagi.framework.nodes.ContextProviderNode;

public interface RamlTypedFragment extends LibraryNodeProvider, ContextProviderNode
{
RamlFragment getFragment();
}
Loading

0 comments on commit 23623e3

Please sign in to comment.