From f6f7d6b4dfb68f71e3f66112f8e36ee085248550 Mon Sep 17 00:00:00 2001 From: Tim Yates Date: Thu, 31 Aug 2023 09:40:04 +0100 Subject: [PATCH] Add support for resolveTemplate and resolveTemplates This does not add support for the varients with encodeSlashInPath, nor the others. To support those, I believe we would need to rewrite this as the functionality differs from the core UriBuilder. Unless requested, I'm not sure it's worth the extra work to do that at this time. --- .../jaxrs/runtime/core/JaxRsUriBuilder.java | 10 ++++--- .../runtime/core/JaxRsUriBuilderSpec.groovy | 29 +++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 jaxrs-server/src/test/groovy/io/micronaut/jaxrs/runtime/core/JaxRsUriBuilderSpec.groovy diff --git a/jaxrs-server/src/main/java/io/micronaut/jaxrs/runtime/core/JaxRsUriBuilder.java b/jaxrs-server/src/main/java/io/micronaut/jaxrs/runtime/core/JaxRsUriBuilder.java index 2ed6ebf0..0131f58f 100644 --- a/jaxrs-server/src/main/java/io/micronaut/jaxrs/runtime/core/JaxRsUriBuilder.java +++ b/jaxrs-server/src/main/java/io/micronaut/jaxrs/runtime/core/JaxRsUriBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2020 original authors + * Copyright 2017-2023 original authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,7 +32,7 @@ */ public class JaxRsUriBuilder extends UriBuilder { - private final io.micronaut.http.uri.UriBuilder uriBuilder; + private io.micronaut.http.uri.UriBuilder uriBuilder; /** * Default constructor. @@ -180,7 +180,8 @@ public UriBuilder fragment(String fragment) { @Override public UriBuilder resolveTemplate(String name, Object value) { - throw new UnsupportedOperationException("Method resolveTemplate(..) not supported by implementation"); + uriBuilder = io.micronaut.http.uri.UriBuilder.of(uriBuilder.expand(Map.of(name, value))); + return this; } @Override @@ -195,7 +196,8 @@ public UriBuilder resolveTemplateFromEncoded(String name, Object value) { @Override public UriBuilder resolveTemplates(Map templateValues) { - throw new UnsupportedOperationException("Method resolveTemplates(..) not supported by implementation"); + uriBuilder = io.micronaut.http.uri.UriBuilder.of(uriBuilder.expand(templateValues)); + return this; } @Override diff --git a/jaxrs-server/src/test/groovy/io/micronaut/jaxrs/runtime/core/JaxRsUriBuilderSpec.groovy b/jaxrs-server/src/test/groovy/io/micronaut/jaxrs/runtime/core/JaxRsUriBuilderSpec.groovy new file mode 100644 index 00000000..a0e4ab72 --- /dev/null +++ b/jaxrs-server/src/test/groovy/io/micronaut/jaxrs/runtime/core/JaxRsUriBuilderSpec.groovy @@ -0,0 +1,29 @@ +package io.micronaut.jaxrs.runtime.core + +import spock.lang.Specification + +class JaxRsUriBuilderSpec extends Specification { + + def 'resolve template encodes the value'() { + given: + def builder = JaxRsUriBuilder.fromPath("/person") + + when: + def result = builder.path("/{name}").resolveTemplate("name", "Tim Yates").build() + + then: + result.toString() == '/person/Tim%20Yates' + } + + def 'resolve template accepts multiple values'() { + given: + def builder = JaxRsUriBuilder.fromPath("/person/{age}") + Map values = Map.of("name", "Tim Yates", "age", 43) + + when: + def result = builder.path("/{name}").resolveTemplates(values).build() + + then: + result.toString() == '/person/43/Tim%20Yates' + } +}