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

fix: support both camel and kebab case cors enabled #732

Merged
merged 1 commit into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
# BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
version=12.0.0-SNAPSHOT
version=12.0.1-SNAPSHOT
### Project Metadata
group=com.graphql-java-kickstart
PROJECT_NAME=graphql-spring-boot
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package graphql.kickstart.autoconfigure.web.servlet;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.PropertyResolver;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class CorsEnabledCondition implements Condition {

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
PropertyResolver resolver = context.getEnvironment();
Boolean kebabCorsEnabled = resolver.getProperty("graphql.servlet.cors-enabled", Boolean.class);
if (kebabCorsEnabled != null) {
return kebabCorsEnabled;
}
Boolean camelCorsEnabled = resolver.getProperty("graphql.servlet.corsEnabled", Boolean.class);
if (camelCorsEnabled != null) {
return camelCorsEnabled;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,24 @@
@ConfigurationProperties(prefix = "graphql.servlet")
public class GraphQLServletProperties {

public final static Duration DEFAULT_SUBSCRIPTION_TIMEOUT = Duration.ZERO;
public static final Duration DEFAULT_SUBSCRIPTION_TIMEOUT = Duration.ZERO;

private boolean enabled = true;
private boolean corsEnabled = true;
private String mapping = "/graphql";
private boolean exceptionHandlersEnabled = false;
/**
* Subscription timeout. If a duration suffix is not specified, millisecond will be used.
*/
/** Subscription timeout. If a duration suffix is not specified, millisecond will be used. */
@DurationUnit(ChronoUnit.MILLIS)
private Duration subscriptionTimeout = DEFAULT_SUBSCRIPTION_TIMEOUT;

private ContextSetting contextSetting = ContextSetting.PER_QUERY_WITH_INSTRUMENTATION;
/** Asynchronous execution timeout. If a duration suffix is not specified, millisecond will be used. @deprecated Use <tt>graphql.servlet.async.timeout</tt> instead */
@Deprecated @DurationUnit(ChronoUnit.MILLIS) private Duration asyncTimeout;
/**
* Asynchronous execution timeout. If a duration suffix is not specified, millisecond will be
* used. @deprecated Use <tt>graphql.servlet.async.timeout</tt> instead
*/
@Deprecated
@DurationUnit(ChronoUnit.MILLIS)
private Duration asyncTimeout;
/** @deprecated Use <tt>graphql.servlet.async.enabled</tt> instead */
@Deprecated private Boolean asyncModeEnabled;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
Expand Down Expand Up @@ -128,17 +129,15 @@ public GraphQLErrorStartupListener graphQLErrorStartupListener(

@Bean
@ConditionalOnClass(CorsFilter.class)
@Conditional(CorsEnabledCondition.class)
@ConfigurationProperties("graphql.servlet.cors")
public CorsConfiguration corsConfiguration() {
return new CorsConfiguration();
}

@Bean
@ConditionalOnClass(CorsFilter.class)
@ConditionalOnProperty(
value = "graphql.servlet.corsEnabled",
havingValue = "true",
matchIfMissing = true)
@Conditional(CorsEnabledCondition.class)
public CorsFilter corsConfigurer(CorsConfiguration corsConfiguration) {
Map<String, CorsConfiguration> corsConfigurations = new LinkedHashMap<>(1);
if (corsConfiguration.getAllowedMethods() == null) {
Expand Down Expand Up @@ -303,12 +302,17 @@ public GraphQLConfiguration graphQLServletConfiguration(
@Autowired(required = false) GraphQLResponseCacheManager responseCacheManager,
@Autowired(required = false) AsyncTaskDecorator asyncTaskDecorator,
@Autowired(required = false) @Qualifier("graphqlAsyncTaskExecutor") Executor asyncExecutor) {
Duration asyncTimeout = Optional.ofNullable(asyncServletProperties.getTimeout()) //
.orElse(AsyncServletProperties.DEFAULT_TIMEOUT);
long asyncTimeoutMilliseconds = Optional.ofNullable(graphQLServletProperties.getAsyncTimeout()) //
.orElse(asyncTimeout).toMillis();
long subscriptionTimeoutMilliseconds = Optional.ofNullable(graphQLServletProperties.getSubscriptionTimeout()) //
.orElse(GraphQLServletProperties.DEFAULT_SUBSCRIPTION_TIMEOUT).toMillis();
Duration asyncTimeout =
Optional.ofNullable(asyncServletProperties.getTimeout()) //
.orElse(AsyncServletProperties.DEFAULT_TIMEOUT);
long asyncTimeoutMilliseconds =
Optional.ofNullable(graphQLServletProperties.getAsyncTimeout()) //
.orElse(asyncTimeout)
.toMillis();
long subscriptionTimeoutMilliseconds =
Optional.ofNullable(graphQLServletProperties.getSubscriptionTimeout()) //
.orElse(GraphQLServletProperties.DEFAULT_SUBSCRIPTION_TIMEOUT)
.toMillis();
return GraphQLConfiguration.with(invocationInputFactory)
.with(graphQLInvoker)
.with(graphQLObjectMapper)
Expand Down