Skip to content

Commit

Permalink
Merge pull request #7 from jamezp/issue5
Browse files Browse the repository at this point in the history
[5] Only allow injection of the UriBuilder as a method parameter or c…
  • Loading branch information
jamezp authored Aug 15, 2024
2 parents e4fdad9 + a859f1e commit 455c25b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ This can be used with the `@RequestPath` qualifier. It creates a `WebTarget` fro
* `@RestClientConfig` (optional)

|`jakarta.ws.rs.core.UriBuilder`
|X
|
|X
|X
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import dev.resteasy.junit.extension.api.ConfigurationProvider;
import dev.resteasy.junit.extension.extensions.InjectionProducerExtension;
import dev.resteasy.junit.extension.extensions.SeBootstrapExtension;
import dev.resteasy.junit.extension.extensions.UriBuilderParameterResolver;

/**
* An annotation which starts a {@link jakarta.ws.rs.SeBootstrap.Instance} for unit testing.
Expand All @@ -38,7 +39,8 @@
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith({
SeBootstrapExtension.class,
InjectionProducerExtension.class
InjectionProducerExtension.class,
UriBuilderParameterResolver.class,
})
public @interface RestBootstrap {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright The RESTEasy Authors
* SPDX-License-Identifier: Apache-2.0
*/

package dev.resteasy.junit.extension.extensions;

import jakarta.ws.rs.core.UriBuilder;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;

/**
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
public class UriBuilderParameterResolver implements ParameterResolver {
@Override
public boolean supportsParameter(final ParameterContext parameterContext, final ExtensionContext extensionContext)
throws ParameterResolutionException {
return UriBuilder.class.isAssignableFrom(parameterContext.getParameter().getType());
}

@Override
public Object resolveParameter(final ParameterContext parameterContext, final ExtensionContext extensionContext)
throws ParameterResolutionException {
if (UriBuilder.class.isAssignableFrom(parameterContext.getParameter().getType())) {
return InstanceManager.getInstance(extensionContext)
.map(im -> im.instance().configuration().baseUriBuilder())
.orElseThrow(() -> new ParameterResolutionException("Failed to lookup URI builder instance."));
}
throw new ParameterResolutionException(String.format("Type %s is not assignable to %s",
parameterContext.getParameter().getType().getName(), UriBuilder.class.getName()));
}
}

This file was deleted.

0 comments on commit 455c25b

Please sign in to comment.