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

Commit

Permalink
Fix for #314
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbelang committed Jan 26, 2018
1 parent b02ad5c commit 39b2dbf
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ uses:
/Attachments:
uriParameters:
viewingSessionId:
type: string
type: integer
get:
responses:
200:
Expand Down Expand Up @@ -290,3 +290,9 @@ uses:
# application/xml:
# type: !include schemas/songs.xsd
# example: !include examples/songs.xml

/foo:
/bar:
get:
/bar/{id}:
get:
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@
*/
package org.raml.jaxrs.generator;

import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import org.raml.jaxrs.generator.ramltypes.GMethod;
import org.raml.jaxrs.generator.ramltypes.GParameter;
import org.raml.jaxrs.generator.ramltypes.GRequest;
import org.raml.jaxrs.generator.ramltypes.GResource;
import org.raml.jaxrs.generator.ramltypes.GResponse;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.TypeName;
import org.hamcrest.Matchers;
import org.raml.jaxrs.generator.ramltypes.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Created by Jean-Philippe Belanger on 12/4/16. Just potential zeroes and ones
Expand Down Expand Up @@ -60,19 +62,147 @@ public static void fillInBodiesAndResponses(GResource resource,

}

private static final Pattern PARAM = Pattern.compile(".*?\\{([^}]+?)\\}");

public static List<GParameter> accumulateUriParameters(GResource resource) {

Set<String> seenHere = extractSeen(new HashSet<String>(), resource);

List<GParameter> parameters = new ArrayList<>();
parameters.addAll(Lists.reverse(resource.uriParameters()));
parameters.addAll(Lists.reverse(FluentIterable.from(resource.uriParameters())
.append(allMatches(seenHere, resource.relativePath())).toList()));

while (resource.parentResource() != null) {

resource = resource.parentResource();
parameters.addAll(Lists.reverse(resource.uriParameters()));
seenHere = extractSeen(seenHere, resource);
parameters.addAll(Lists.reverse(FluentIterable.from(resource.uriParameters())
.append(allMatches(seenHere, resource.relativePath())).toList()));
}

Collections.reverse(parameters);

return parameters;
}

private static ImmutableSet<String> extractSeen(Set<String> seen, GResource resource) {
return FluentIterable.from(resource.uriParameters())
.transform(new Function<GParameter, String>() {

@Nullable
@Override
public String apply(@Nullable GParameter gParameter) {
return gParameter.name();
}
}).append(seen).toSet();
}

private static List<GParameter> allMatches(Set<String> seenHere, String path) {

List<GParameter> allMatches = new ArrayList<>();
Matcher m = PARAM.matcher(path);
while (m.find()) {
final String group = m.group(1);
if (!seenHere.contains(group)) {
allMatches.add(new ImplicitStringGParameter(group));
}
}

return allMatches;
}

private static class ImplicitStringGParameter implements GParameter {

private final String group;

public ImplicitStringGParameter(String group) {
this.group = group;
}

@Override
public String defaultValue() {
return null;
}

@Override
public String name() {
return group;
}

@Override
public GType type() {
return new GType() {

@Override
public String type() {
return "string";
}

@Override
public String name() {
return "string";
}

@Override
public TypeName defaultJavaTypeName(String pack) {
return ClassName.get(String.class);
}

@Override
public boolean isJson() {
return false;
}

@Override
public boolean isXml() {
return false;
}

@Override
public boolean isArray() {
return false;
}

@Override
public boolean isEnum() {
return false;
}

@Override
public boolean isScalar() {
return true;
}

@Override
public String schema() {
return null;
}

@Override
public GType arrayContents() {
return null;
}

@Override
public void construct(CurrentBuild currentBuild, GObjectType objectType) {

}

@Override
public void setJavaType(TypeName generatedJavaType) {

}

@Override
public Object implementation() {
return null;
}
};
}

@Override
public Object implementation() {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public void mockito() {
@Test
public void accumulateUriParameters() throws Exception {

when(resource.relativePath()).thenReturn("/fun");
when(one.name()).thenReturn("one");
when(two.name()).thenReturn("two");
when(resource.uriParameters()).thenReturn(Arrays.asList(one, two));

List<GParameter> parameters = ResourceUtils.accumulateUriParameters(resource);
Expand All @@ -67,9 +70,17 @@ public void accumulateUriParameters() throws Exception {
assertEquals(two, parameters.get(1));
}


@Test
public void accumulateUriParametersFromParent() throws Exception {

when(one.name()).thenReturn("one");
when(two.name()).thenReturn("two");
when(three.name()).thenReturn("three");

when(topResource.relativePath()).thenReturn("/fun/");
when(resource.relativePath()).thenReturn("/allo");

when(resource.parentResource()).thenReturn(topResource);
when(topResource.uriParameters()).thenReturn(Arrays.asList(one, two));
when(resource.uriParameters()).thenReturn(Collections.singletonList(three));
Expand All @@ -82,4 +93,47 @@ public void accumulateUriParametersFromParent() throws Exception {
assertEquals(three, parameters.get(2));
}

@Test
public void accumulateImplicitParameter() throws Exception {

when(resource.relativePath()).thenReturn("/fun/{id}");
when(resource.uriParameters()).thenReturn(Collections.<GParameter>emptyList());

List<GParameter> parameters = ResourceUtils.accumulateUriParameters(resource);

assertEquals(1, parameters.size());
assertEquals("id", parameters.get(0).name());
}

@Test
public void accumulateImplicitUriParametersFromParent() throws Exception {

when(resource.parentResource()).thenReturn(topResource);
when(topResource.relativePath()).thenReturn("/fun/{id}/{color}");
when(resource.relativePath()).thenReturn("/{goo}");

List<GParameter> parameters = ResourceUtils.accumulateUriParameters(resource);

assertEquals(3, parameters.size());
assertEquals("id", parameters.get(0).name());
assertEquals("color", parameters.get(1).name());
assertEquals("goo", parameters.get(2).name());
}

@Test
public void accumulateDeclaredLater() throws Exception {

when(resource.parentResource()).thenReturn(topResource);
when(topResource.relativePath()).thenReturn("/fun/{id}");
when(resource.relativePath()).thenReturn("/fun");
when(one.name()).thenReturn("id");
when(resource.uriParameters()).thenReturn(Collections.singletonList(one));


List<GParameter> parameters = ResourceUtils.accumulateUriParameters(resource);

assertEquals(1, parameters.size());
assertEquals("id", parameters.get(0).name());
}

}

0 comments on commit 39b2dbf

Please sign in to comment.