Skip to content

Commit

Permalink
Merge pull request #324 from turkraft/3.x.x
Browse files Browse the repository at this point in the history
Left join singular attributes and example fixes
  • Loading branch information
torshid authored Sep 15, 2023
2 parents da5c1cd + f79a305 commit 65de41c
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ void test() {
return null;
});

FilterNode node = filterParser.parse("x : y : z", ctx);
FilterNode node = filterParser.parse("x : y ! z", ctx);

Assertions.assertEquals("x ! y ! z", filterStringConverter.convert(node));
Assertions.assertEquals("x ! y : z", filterStringConverter.convert(node));

}

Expand Down
8 changes: 7 additions & 1 deletion jpa-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
Expand Down Expand Up @@ -66,6 +66,11 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>

</dependencies>

<profiles>
Expand All @@ -87,6 +92,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.1.3</version>
</plugin>

<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public List<Company> getCompanies(@Parameter(hidden = true) FilterSpecification<

@Operation(parameters = @Parameter(name = "filter", in = ParameterIn.QUERY,
schema = @Schema(type = "string"),
example = "maritalStatus in ('divorced', 'separated') and (size(staff) > 2 or manager is not null)"))
example = "maritalStatus in ['divorced', 'separated'] and (size(staff) > 2 or manager is not null)"))
@GetMapping(value = "employee")
public List<Employee> getEmployees(
@Parameter(hidden = true) FilterSpecification<Employee> filter) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.turkraft.springfilter.example;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.stereotype.Component;

@Component
public class StringToEnumConverterFactory
implements ConverterFactory<String, Enum> {

private static class StringToEnumConverter<T extends Enum>
implements Converter<String, T> {

private final Class<T> enumType;

public StringToEnumConverter(Class<T> enumType) {
this.enumType = enumType;
}

public T convert(String source) {
for (T constant : enumType.getEnumConstants()) {
if (constant.toString().equalsIgnoreCase(source)) {
return constant;
}
}
return (T) Enum.valueOf(this.enumType, source.trim());
}

}

@Override
public <T extends Enum> Converter<String, T> getConverter(
Class<T> targetType) {
return new StringToEnumConverter(targetType);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.turkraft.springfilter.example;

import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfiguration implements WebMvcConfigurer {

@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverterFactory(new StringToEnumConverterFactory());
}

}
1 change: 1 addition & 0 deletions jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.1.3</version>
</plugin>

</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import jakarta.persistence.EntityManager;
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.From;
import jakarta.persistence.criteria.JoinType;
import jakarta.persistence.criteria.MapJoin;
import jakarta.persistence.criteria.Path;
import jakarta.persistence.criteria.Root;
Expand Down Expand Up @@ -93,7 +94,10 @@ public Path<?> getPath(RootContext rootContext, String fieldPath) {

if (shouldJoin) {
if (!rootContext.getPaths().containsKey(chain)) {
rootContext.getPaths().put(chain, ((From<?, ?>) path).join(field));
rootContext.getPaths().put(chain, ((From<?, ?>) path).join(field,
nextPath.getModel().getBindableType() == BindableType.SINGULAR_ATTRIBUTE
? JoinType.LEFT
: JoinType.INNER));
}
nextPath = rootContext.getPaths().get(chain);
}
Expand Down
2 changes: 2 additions & 0 deletions mongo-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.1.3</version>
</plugin>

</plugins>
Expand All @@ -102,6 +103,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.1.3</version>
</plugin>

<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public void run(String... args) {

@Operation(hidden = true)
@GetMapping("/")
public void index(HttpServletResponse response) throws IOException {
public void index(HttpServletResponse response)
throws IOException {
response.sendRedirect("swagger-ui.html");
}

Expand All @@ -120,7 +121,7 @@ public List<Company> getCompanies(

@Operation(parameters = @Parameter(name = "filter", in = ParameterIn.QUERY,
schema = @Schema(type = "string"),
example = "maritalStatus in ('divorced', 'separated') and (size(staff) > 2 or manager is not null)"))
example = "maritalStatus in ['DIVORCED', 'SEPARATED'] and (size(staff) > 2 or manager is not null)"))
@GetMapping(value = "employee")
public List<Employee> getEmployees(
@Parameter(hidden = true) @Filter(entityClass = Employee.class) Document filter) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.turkraft.springfilter.example;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.stereotype.Component;

@Component
public class StringToEnumConverterFactory
implements ConverterFactory<String, Enum> {

private static class StringToEnumConverter<T extends Enum>
implements Converter<String, T> {

private final Class<T> enumType;

public StringToEnumConverter(Class<T> enumType) {
this.enumType = enumType;
}

public T convert(String source) {
for (T constant : enumType.getEnumConstants()) {
if (constant.toString().equalsIgnoreCase(source)) {
return constant;
}
}
return (T) Enum.valueOf(this.enumType, source.trim());
}

}

@Override
public <T extends Enum> Converter<String, T> getConverter(
Class<T> targetType) {
return new StringToEnumConverter(targetType);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.turkraft.springfilter.example;

import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfiguration implements WebMvcConfigurer {

@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverterFactory(new StringToEnumConverterFactory());
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.turkraft.springfilter.example.model;

import com.fasterxml.jackson.annotation.JsonValue;
import java.util.Date;
import java.util.List;
import org.springframework.data.annotation.Id;
Expand Down Expand Up @@ -40,13 +39,16 @@ public void setId(String id) {

public enum MaritalStatus {

UNKNOWN, MARRIED, WIDOWED, DIVORCED, SINGLE, SEPARATED;
UNKNOWN, MARRIED, WIDOWED, DIVORCED, SINGLE, SEPARATED

/* need to add a custom enum->string mongo converter in order to open the following code
otherwise mongo uses name() when serializing and this creates inconsistencies with jackson
@JsonValue
@Override
public String toString() {
return name().toLowerCase();
}
*/

}

Expand Down
1 change: 1 addition & 0 deletions mongo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.1.3</version>
</plugin>

</plugins>
Expand Down

0 comments on commit 65de41c

Please sign in to comment.