Skip to content
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

Tenant Argument Binder #343

Merged
merged 12 commits into from
Nov 12, 2024
Merged

Tenant Argument Binder #343

merged 12 commits into from
Nov 12, 2024

Conversation

sdelamo
Copy link
Contributor

@sdelamo sdelamo commented Oct 30, 2024

This adds an api Tenant, which represents the abstract notion of a tenant.

Moreover, it adds a RequestFilter to resolve the tenant and set it as a request attribute.

This pull request adds an argument binder that uses the request attribute resolved in the filter. Because of this, a user can bind the tenant as a controller method parameter


@RequestFilter
void filter(HttpRequest<?> request) {
Optional<Serializable> tenantOptional = resolveTenant(request);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I attempt first to resolve with a HttpRequestTenantResolver to avoid using ServerRequestContext if possible.

This adds an api `Tenant`, which represents the abstract notion of a tenant.

Moreover, it adds a RequestFilter to resolve the tenant and set it as a request attribute.

This pull request adds an argument binder that uses the request attribute resolved in the filter. Because of this, a user can bind the tenant as a controller method parameter
@sdelamo sdelamo force-pushed the tenant-argument-binding branch from 7140d2d to ceabab7 Compare October 30, 2024 13:06
@sdelamo sdelamo requested a review from graemerocher October 30, 2024 15:26
@sdelamo sdelamo requested a review from graemerocher October 31, 2024 10:52
* @param <T> The type of the tenant identifier
*/

public interface Tenant<T extends Serializable> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh this is why.

I don't think this should be bound to Serializable, nobody uses java serialization.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My original design return type for the id method was String.
ceabab7

@graemerocher thinks we should use a generic. And since tenant resolver returns serialisable, I set T extends Serializable

Maybe it makes sense to get back to return a String. We control how we set the attribute in the filter and we can call TenatnResolver::resolveIdentifier().toString. thoughts @graemerocher @yawkat ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not even Hibernate requires a serializable ID

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use Serializable

Copy link
Contributor

@graemerocher graemerocher Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the choice was because TenatnResolver::resolveIdentifier uses serialisable and changing that now would be a breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a String resolveId method to both TenantResolver and HttpTenantResolver. I have deprecated the Serializable resolveIdentifier methods. I use the resolveId in the filter now. Tenant is now simply :

public interface Tenant {
    String id();
}

…nantResolverFilterConfigurationProperties.java

Co-authored-by: Jonas Konrad <me@yawk.at>
@sdelamo sdelamo requested a review from yawkat November 7, 2024 06:01
value = "${" + TenantResolverFilterConfigurationProperties.PREFIX + ".regex-pattern:" + TenantResolverFilterConfigurationProperties.DEFAULT_REGEX_PATTERN + "}")
@Internal
@Requires(condition = TenantResolverExistsCondition.class)
final class TenantResolverFilter implements Ordered {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you add HTTP stuff in multinenancy? This should be in some HTTP module

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

micronaut-multitenancy already depended on http-server. Most tenant resolvers were already http related. E.g.
CookieTenantResolver, HttpHeaderTenantResolver, `SubdomainTenantResolver

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

micronaut-http-server is compileOnly and I have added a test which verifies you can load the module without http

@@ -0,0 +1,6 @@
The module ships a filter which resolves the current tenant and sets it as a request attribute named `tenantIdentifier`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to have some TenantHttpAttributes.ID

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand what you are requesting. There is an already a constant with value tenantIdentifier and name TenantResolverFilter.ATTRIBUTE_TENANT.

@sdelamo sdelamo requested review from dstepanov and yawkat November 12, 2024 11:15
@sdelamo sdelamo added the type: enhancement New feature or request label Nov 12, 2024
@sdelamo sdelamo merged commit 47618d3 into 5.5.x Nov 12, 2024
11 checks passed
@sdelamo sdelamo deleted the tenant-argument-binding branch November 12, 2024 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement New feature or request
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants