Skip to content

Add SpEL support for @Table, @Column and @MappedCollection #1461

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

Closed
wants to merge 5 commits into from

Conversation

kurtn718
Copy link
Contributor

If SpEl expressions are specified in the @table or @column annotation, they will be evaluated and the output will be sanitized to prevent SQL Injections.

The default sanitization only allows digits, alphabetic characters, and _ character. (i.e. [0-9, a-z, A-Z, _])

Closes #1325

@kurtn718 kurtn718 requested review from schauder and mp911de March 24, 2023 19:19
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 24, 2023
@mp911de mp911de self-assigned this Mar 27, 2023
kurtn718 and others added 4 commits April 11, 2023 15:23
If SpEl expressions are specified in the @table or @column annotation, they will be evaluated and the output will be sanitized to prevent SQL Injections.

The default sanitization only allows digits, alphabetic characters, and _ character. (i.e. [0-9, a-z, A-Z, _])

Closes #1325
… inject a custom sanitizer in the RelationMappingContext and JdbcMappingContext classes.
Reuse existing EvaluationContextProvider infrastructure and static parser/parser context instances. Parse expressions early. Update Javadoc to reflect SpEL support.

Reformat code to use tabs instead of spaces. Rename types for consistency. Rename SpelExpressionResultSanitizer to SqlIdentifierSanitizer to express its intended usage.
@mp911de
Copy link
Member

mp911de commented Apr 11, 2023

I applied a round of polishing. Besides some stylistic changes, please make sure to configure your formatter to use tabs instead of spaces. See https://github.com/spring-projects/spring-data-build/blob/main/etc/ide/eclipse-formatting.xml the formatter config we use across our projects.

@schauder schauder removed their request for review April 12, 2023 06:23

Table table = getRequiredAnnotation(Table.class);

// TODO: support expressions for schema
Copy link
Contributor

Choose a reason for hiding this comment

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

@mp911de can we please not sprinkle TODOs in our code. Those should be either review comments or issue tickets.

@kurtn718 For this one I'd leave it to you, create a ticket from it, or implement the change as part of this ticket.

Copy link
Member

Choose a reason for hiding this comment

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

I'm in favor of supporting SpEL for schema within the scope of this ticket.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will see what the scope of changes is - and if small add to this ticket.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just pushed up an update. Adding this was pretty straight-forward.

@@ -38,25 +38,26 @@
public @interface Table {

/**
* The mapping table name.
* The table name. The attribute supports SpEL expressions to dynamically calculate the table name on a per-operation
Copy link
Contributor

Choose a reason for hiding this comment

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

This is currently not correct, since SQL statements get cached.

We would need to disable the caching or change the JavaDoc comments.

@mp911de What is your opinion on this?

See also: #435

Copy link
Member

Choose a reason for hiding this comment

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

The cache should be updated to consider dynamic table and column names. This should be possible through a corresponding cache key.

Add SpEL support for schema property in @table annotation
@mp911de mp911de changed the title Issue #1325: Add SpEL support for @Table and @Column names Add SpEL support for @Table, @Column and @MappedCollection May 31, 2023
@mp911de mp911de added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels May 31, 2023
@mp911de mp911de added this to the 3.2 M1 (2023.1.0) milestone May 31, 2023
mp911de pushed a commit that referenced this pull request May 31, 2023
If SpEl expressions are specified in the `@Table` or `@Column` annotation, they will be evaluated and the output will be sanitized to prevent SQL Injections.

The default sanitization only allows digits, alphabetic characters, and _ character. (i.e. [0-9, a-z, A-Z, _])

Closes #1325
Original pull request: #1461
mp911de added a commit that referenced this pull request May 31, 2023
Reuse existing EvaluationContextProvider infrastructure and static parser/parser context instances. Parse expressions early. Update Javadoc to reflect SpEL support.

Reformat code to use tabs instead of spaces. Rename types for consistency. Rename SpelExpressionResultSanitizer to SqlIdentifierSanitizer to express its intended usage.

Eagerly initialize entities where applicable. Simplify code.

See #1325
Original pull request: #1461
mp911de pushed a commit that referenced this pull request May 31, 2023
Add SpEL support for schema property in `@Table` annotation

See #1325
Original pull request: #1461
mp911de added a commit that referenced this pull request May 31, 2023
@mp911de
Copy link
Member

mp911de commented May 31, 2023

That's merged and polished now.

@mp911de mp911de closed this May 31, 2023
@mp911de mp911de deleted the issue/1325 branch June 1, 2023 08:16
@goafabric
Copy link

goafabric commented Jun 30, 2023

Hello .. I've got a question concerning this ticket
I've been eagerly following that one, to find a lightweight alternative to Hibernates Tenant Resolver
for Multi Schema
By having a spel expression inside the schema part of the Table annotation
I've been using the same approach successfully for spring data mongo for a while now.

And while i can happily report that this works fine for queries / select stetements,
it seems to fail for insert statements.

While the spel expression is always evaluated correctly to e.g. "tenant-0" or "tenant-5",
and always taken into account for the select from queries ...
For "insert into" it will always use the first one "insert into tenant-0" .. maybe there is some kind of caching going on ?

So my question is, are insert operations also supposed to work with schema ?

As an alternative i was also looking into using a NamingStrategy Bean,
unfortunately this always yields to a CachingNamingStrategy that only resolves the Schema once ...

thx in advance

-- cut examples ---

@table(name = "insurance", schema = "#{@tenantIdBean.getPrefix()}")

@component
public class TenantIdBean {
public String getPrefix() {
return "tenant_" + HttpInterceptor.getTenantId();
}
}

@schauder
Copy link
Contributor

Yes, statements do get cached. Please create a new ticket for this.

@goafabric
Copy link

Yes, statements do get cached. Please create a new ticket for this.

Yes, statements do get cached. Please create a new ticket for this.

thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add SpEL support for table and column names
5 participants