Skip to content

Commit

Permalink
Issue mulesoft-labs#418: String patterns not recognized in jaxrs Path
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaiser1989 committed Jan 13, 2020
1 parent e29ad55 commit 16f3333
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.squareup.javapoet.*;
import com.squareup.javapoet.MethodSpec.Builder;

import org.raml.jaxrs.generator.*;
import org.raml.jaxrs.generator.builders.CodeContainer;
Expand All @@ -31,6 +30,7 @@
import org.raml.jaxrs.generator.ramltypes.*;
import org.raml.jaxrs.generator.v10.V10GType;
import org.raml.v2.api.model.v10.datamodel.ObjectTypeDeclaration;
import org.raml.v2.api.model.v10.datamodel.StringTypeDeclaration;
import org.raml.v2.api.model.v10.datamodel.TypeDeclaration;
import org.raml.v2.api.model.v10.methods.Method;
import org.raml.v2.api.model.v10.resources.Resource;
Expand Down Expand Up @@ -563,8 +563,13 @@ private MethodSpec.Builder createMethodBuilder(GMethod gMethod, String methodNam
.builder(Path.class)
.addMember("value",
"$S",
gMethod.resource().resourcePath()
.substring(findTopResource(gMethod.resource().parentResource()).resourcePath().length()))
generatePathString(
gMethod
.resource()
.resourcePath()
.substring(findTopResource(gMethod.resource().parentResource()).resourcePath()
.length()),
ResourceUtils.accumulateUriParameters(gMethod.resource())))
.build());
}

Expand Down Expand Up @@ -597,6 +602,31 @@ private MethodSpec.Builder createMethodBuilder(GMethod gMethod, String methodNam
return methodSpec;
}

private String generatePathString(final String pathString, final List<GParameter> uriParams) {
String generatedPathString = pathString;
// update regex of path string pattern variables (if needed)
for (GParameter parameter : uriParams) {
final String name = Names.methodName(parameter.name());
// check if path contains param
if (pathString.contains("{" + name + "}")) {
// check if param is a string value
if (parameter.implementation() instanceof StringTypeDeclaration) {
StringTypeDeclaration typeDecl = (StringTypeDeclaration) parameter.implementation();
// check if this string has a pattern to register
if (typeDecl.pattern() != null) {
// remove ^...$ from pattern if existing
String pattern = typeDecl.pattern();
if (pattern.startsWith("^") && pattern.endsWith("$") && pattern.length() > 2) {
pattern = pattern.substring(1, pattern.length() - 1);
}
generatedPathString = pathString.replace("{" + name + "}", "{" + name + ":" + pattern + "}");
}
}
}
}
return generatedPathString;
}

private GResource findTopResource(GResource gResource) {

if (gResource.parentResource() == null) {
Expand Down Expand Up @@ -670,7 +700,7 @@ public TypeSpec.Builder onResource(ResourceContext context, GResource resource,
TypeSpec.Builder typeSpec = TypeSpec.interfaceBuilder(Names.typeName(name))
.addModifiers(Modifier.PUBLIC)
.addAnnotation(AnnotationSpec.builder(Path.class)
.addMember("value", "$S", uri).build());
.addMember("value", "$S", generatePathString(uri, resource.uriParameters())).build());

buildResource(typeSpec, topResource);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,40 @@ public void into(TypeSpec g) throws IOException {
}, "foo", "/fun");
}

@Test
public void build_with_path_param_and_pattern() throws Exception {

// https://github.com/mulesoft-labs/raml-for-jax-rs/issues/252
RamlV10.buildResourceV10(this, "resource_entity_no_response_pattern.raml",
new CodeContainer<TypeSpec>() {

@Override
public void into(TypeSpec g) throws IOException {

assertEquals("Foo", g.name);
assertEquals(1, g.methodSpecs.size());
MethodSpec methodSpec = g.methodSpecs.get(0);
assertEquals("putViewingSessionAttachmentsByViewingSessionId", methodSpec.name);
assertEquals(2, methodSpec.annotations.size());
assertEquals(ClassName.get(PUT.class), methodSpec.annotations.get(0).type);
assertEquals(ClassName.get(Path.class), methodSpec.annotations.get(1).type);
assertEquals("\"/{viewingSessionId:(;[^/;]+)*}/Attachments\"",
methodSpec.annotations.get(1).members.get("value").get(0).toString());

assertEquals(1, methodSpec.parameters.size());

ParameterSpec paramOneSpec = methodSpec.parameters.get(0);
assertEquals("viewingSessionId", paramOneSpec.name);
assertEquals(ClassName.get(String.class), paramOneSpec.type);
assertEquals(1, paramOneSpec.annotations.size());
assertEquals(ClassName.get(PathParam.class), paramOneSpec.annotations.get(0).type);
assertEquals("\"viewingSessionId\"", paramOneSpec.annotations.get(0).members.get("value").get(0)
.toString());

}
}, "foo", "/fun");
}

@Test
public void build_with_query_param_with_complex_types() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#%RAML 1.0
title: GitHub API
version: v3
baseUri: https://api.github.com
/ViewingSession:
/{viewingSessionId}:
/Attachments:
uriParameters:
viewingSessionId:
type: string
pattern: ^(;[^/;]+)*$
put:

0 comments on commit 16f3333

Please sign in to comment.