Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit f94d3ab

Browse files
authored
Merge pull request #732 from graphql-java-kickstart/fix/cors-enabled-682
fix: support both camel and kebab case cors enabled
2 parents d88752e + 3b22cf3 commit f94d3ab

File tree

4 files changed

+49
-18
lines changed

4 files changed

+49
-18
lines changed

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
# BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1515
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
1616
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17-
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1818
#
19-
version=12.0.0-SNAPSHOT
19+
version=12.0.1-SNAPSHOT
2020
### Project Metadata
2121
group=com.graphql-java-kickstart
2222
PROJECT_NAME=graphql-spring-boot
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package graphql.kickstart.autoconfigure.web.servlet;
2+
3+
import org.springframework.context.annotation.Condition;
4+
import org.springframework.context.annotation.ConditionContext;
5+
import org.springframework.core.env.PropertyResolver;
6+
import org.springframework.core.type.AnnotatedTypeMetadata;
7+
8+
public class CorsEnabledCondition implements Condition {
9+
10+
@Override
11+
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
12+
PropertyResolver resolver = context.getEnvironment();
13+
Boolean kebabCorsEnabled = resolver.getProperty("graphql.servlet.cors-enabled", Boolean.class);
14+
if (kebabCorsEnabled != null) {
15+
return kebabCorsEnabled;
16+
}
17+
Boolean camelCorsEnabled = resolver.getProperty("graphql.servlet.corsEnabled", Boolean.class);
18+
if (camelCorsEnabled != null) {
19+
return camelCorsEnabled;
20+
}
21+
return true;
22+
}
23+
}

graphql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/autoconfigure/web/servlet/GraphQLServletProperties.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,24 @@
3030
@ConfigurationProperties(prefix = "graphql.servlet")
3131
public class GraphQLServletProperties {
3232

33-
public final static Duration DEFAULT_SUBSCRIPTION_TIMEOUT = Duration.ZERO;
33+
public static final Duration DEFAULT_SUBSCRIPTION_TIMEOUT = Duration.ZERO;
3434

3535
private boolean enabled = true;
3636
private boolean corsEnabled = true;
3737
private String mapping = "/graphql";
3838
private boolean exceptionHandlersEnabled = false;
39-
/**
40-
* Subscription timeout. If a duration suffix is not specified, millisecond will be used.
41-
*/
39+
/** Subscription timeout. If a duration suffix is not specified, millisecond will be used. */
4240
@DurationUnit(ChronoUnit.MILLIS)
4341
private Duration subscriptionTimeout = DEFAULT_SUBSCRIPTION_TIMEOUT;
42+
4443
private ContextSetting contextSetting = ContextSetting.PER_QUERY_WITH_INSTRUMENTATION;
45-
/** Asynchronous execution timeout. If a duration suffix is not specified, millisecond will be used. @deprecated Use <tt>graphql.servlet.async.timeout</tt> instead */
46-
@Deprecated @DurationUnit(ChronoUnit.MILLIS) private Duration asyncTimeout;
44+
/**
45+
* Asynchronous execution timeout. If a duration suffix is not specified, millisecond will be
46+
* used. @deprecated Use <tt>graphql.servlet.async.timeout</tt> instead
47+
*/
48+
@Deprecated
49+
@DurationUnit(ChronoUnit.MILLIS)
50+
private Duration asyncTimeout;
4751
/** @deprecated Use <tt>graphql.servlet.async.enabled</tt> instead */
4852
@Deprecated private Boolean asyncModeEnabled;
4953

graphql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/autoconfigure/web/servlet/GraphQLWebAutoConfiguration.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import org.springframework.beans.factory.annotation.Qualifier;
7474
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
7575
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
76+
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
7677
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
7778
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
7879
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
@@ -128,17 +129,15 @@ public GraphQLErrorStartupListener graphQLErrorStartupListener(
128129

129130
@Bean
130131
@ConditionalOnClass(CorsFilter.class)
132+
@Conditional(CorsEnabledCondition.class)
131133
@ConfigurationProperties("graphql.servlet.cors")
132134
public CorsConfiguration corsConfiguration() {
133135
return new CorsConfiguration();
134136
}
135137

136138
@Bean
137139
@ConditionalOnClass(CorsFilter.class)
138-
@ConditionalOnProperty(
139-
value = "graphql.servlet.corsEnabled",
140-
havingValue = "true",
141-
matchIfMissing = true)
140+
@Conditional(CorsEnabledCondition.class)
142141
public CorsFilter corsConfigurer(CorsConfiguration corsConfiguration) {
143142
Map<String, CorsConfiguration> corsConfigurations = new LinkedHashMap<>(1);
144143
if (corsConfiguration.getAllowedMethods() == null) {
@@ -303,12 +302,17 @@ public GraphQLConfiguration graphQLServletConfiguration(
303302
@Autowired(required = false) GraphQLResponseCacheManager responseCacheManager,
304303
@Autowired(required = false) AsyncTaskDecorator asyncTaskDecorator,
305304
@Autowired(required = false) @Qualifier("graphqlAsyncTaskExecutor") Executor asyncExecutor) {
306-
Duration asyncTimeout = Optional.ofNullable(asyncServletProperties.getTimeout()) //
307-
.orElse(AsyncServletProperties.DEFAULT_TIMEOUT);
308-
long asyncTimeoutMilliseconds = Optional.ofNullable(graphQLServletProperties.getAsyncTimeout()) //
309-
.orElse(asyncTimeout).toMillis();
310-
long subscriptionTimeoutMilliseconds = Optional.ofNullable(graphQLServletProperties.getSubscriptionTimeout()) //
311-
.orElse(GraphQLServletProperties.DEFAULT_SUBSCRIPTION_TIMEOUT).toMillis();
305+
Duration asyncTimeout =
306+
Optional.ofNullable(asyncServletProperties.getTimeout()) //
307+
.orElse(AsyncServletProperties.DEFAULT_TIMEOUT);
308+
long asyncTimeoutMilliseconds =
309+
Optional.ofNullable(graphQLServletProperties.getAsyncTimeout()) //
310+
.orElse(asyncTimeout)
311+
.toMillis();
312+
long subscriptionTimeoutMilliseconds =
313+
Optional.ofNullable(graphQLServletProperties.getSubscriptionTimeout()) //
314+
.orElse(GraphQLServletProperties.DEFAULT_SUBSCRIPTION_TIMEOUT)
315+
.toMillis();
312316
return GraphQLConfiguration.with(invocationInputFactory)
313317
.with(graphQLInvoker)
314318
.with(graphQLObjectMapper)

0 commit comments

Comments
 (0)