Skip to content

Support Helidon Path Variable Patterns #387

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

Merged
merged 3 commits into from
Feb 17, 2024

Conversation

SentryMan
Copy link
Collaborator

Now can do:

@Path("test")
@Controller
public class TestController {

  @Get("/peppermint/{patty:\\d+}")
  String pattern(String patty) {
    return patty;
  }

  @Get("/minus/{+plus}")
  String patternPlus(String plus) {
    return plus;
  }
}

and the controller will generate like

@Generated("avaje-helidon-generator")
@Component
public class TestController$Route implements HttpFeature {

  private final TestController controller;
  private final JsonType<String> stringJsonType;

  public TestController$Route(TestController controller, Jsonb jsonb) {
    this.controller = controller;
    this.stringJsonType = jsonb.type(String.class);
  }

  @Override
  public void setup(HttpRouting.Builder routing) {
    routing.get("/test/peppermint/{patty:\\d+}", this::_pattern);
    routing.get("/test/minus/{+plus}", this::_patternPlus);
  }

  private void _pattern(ServerRequest req, ServerResponse res) throws Exception {
    res.status(OK_200);
    var pathParams = req.path().pathParameters();
    var patty = pathParams.first("patty").get();
    var result = controller.pattern(patty);
    if (result == null) {
      res.status(NO_CONTENT_204).send();
    } else {
      res.headers().contentType(MediaTypes.APPLICATION_JSON);
      res.send(result);
    }
  }

  private void _patternPlus(ServerRequest req, ServerResponse res) throws Exception {
    res.status(OK_200);
    var pathParams = req.path().pathParameters();
    var plus = pathParams.first("plus").get();
    var result = controller.patternPlus(plus);
    if (result == null) {
      res.status(NO_CONTENT_204).send();
    } else {
      res.headers().contentType(MediaTypes.APPLICATION_JSON);
      res.send(result);
    }
  }
}

Fixes #386

@rbygrave rbygrave self-requested a review February 17, 2024 21:08
@rbygrave rbygrave added this to the 2.2 milestone Feb 17, 2024
@SentryMan SentryMan merged commit 10a46b1 into avaje:master Feb 17, 2024
@SentryMan SentryMan deleted the helidon-path-patterns branch February 17, 2024 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support other forms of Helidon path parameters
2 participants