Skip to content

Commit

Permalink
Merge pull request from GHSA-2457-2263-mm9f
Browse files Browse the repository at this point in the history
Co-authored-by: graemerocher <graeme.rocher@gmail.com>
  • Loading branch information
jameskleeh and graemerocher committed Jan 18, 2022
1 parent e5f03a5 commit b022323
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 12 deletions.
Expand Up @@ -44,32 +44,32 @@ public interface ConversionContext extends AnnotationMetadataProvider, TypeVaria
/**
* Constant for Boolean argument.
*/
ArgumentConversionContext<Boolean> BOOLEAN = ConversionContext.of(Argument.BOOLEAN);
ArgumentConversionContext<Boolean> BOOLEAN = ImmutableArgumentConversionContext.of(Argument.BOOLEAN);

/**
* Constant for Integer argument.
*/
ArgumentConversionContext<Integer> INT = ConversionContext.of(Argument.INT);
ArgumentConversionContext<Integer> INT = ImmutableArgumentConversionContext.of(Argument.INT);

/**
* Constant for Long argument.
*/
ArgumentConversionContext<Long> LONG = ConversionContext.of(Argument.LONG);
ArgumentConversionContext<Long> LONG = ImmutableArgumentConversionContext.of(Argument.LONG);

/**
* Constant for String argument.
*/
ArgumentConversionContext<String> STRING = ConversionContext.of(Argument.STRING);
ArgumentConversionContext<String> STRING = ImmutableArgumentConversionContext.of(Argument.STRING);

/**
* Constant for List<String> argument.
*/
ArgumentConversionContext<List<String>> LIST_OF_STRING = ConversionContext.of(Argument.LIST_OF_STRING);
ArgumentConversionContext<List<String>> LIST_OF_STRING = ImmutableArgumentConversionContext.of(Argument.LIST_OF_STRING);

/**
* Constant for List<String> argument.
*/
ArgumentConversionContext<Map> MAP = ConversionContext.of(Argument.of(Map.class));
ArgumentConversionContext<Map> MAP = ImmutableArgumentConversionContext.of(Argument.of(Map.class));

/**
* In the case where the type to be converted contains generic type arguments this map will return
Expand Down Expand Up @@ -168,7 +168,10 @@ public Map<String, Argument<?>> getTypeVariables() {
}

/**
* Create a simple {@link ConversionContext} for the given generic type variables.
* Create a new simple {@link ConversionContext} for the given generic type variables.
*
* <p>NOTE: The instance returned by this method is NOT thread safe and should be shared
* via static state or between threads. Consider using {@link io.micronaut.core.convert.ImmutableArgumentConversionContext} for this case.</p>
*
* @param <T> type Generic
* @param argument The argument
Expand All @@ -181,6 +184,9 @@ static <T> ArgumentConversionContext<T> of(Argument<T> argument) {
/**
* Create a simple {@link ConversionContext} for the given generic type variables.
*
* <p>NOTE: The instance returned by this method is NOT thread safe and should be shared
* via static state or between threads. Consider using {@link io.micronaut.core.convert.ImmutableArgumentConversionContext} for this case.</p>
*
* @param <T> type Generic
* @param argument The argument
* @return The conversion context
Expand All @@ -193,6 +199,9 @@ static <T> ArgumentConversionContext<T> of(Class<T> argument) {
/**
* Create a simple {@link ConversionContext} for the given generic type variables.
*
* <p>NOTE: The instance returned by this method is NOT thread safe and should be shared
* via static state or between threads. Consider using {@link io.micronaut.core.convert.ImmutableArgumentConversionContext} for this case.</p>
*
* @param <T> type Generic
* @param argument The argument
* @param locale The locale
Expand All @@ -205,6 +214,9 @@ static <T> ArgumentConversionContext of(Argument<T> argument, @Nullable Locale l
/**
* Create a simple {@link ConversionContext} for the given generic type variables.
*
* <p>NOTE: The instance returned by this method is NOT thread safe and should be shared
* via static state or between threads. Consider using {@link io.micronaut.core.convert.ImmutableArgumentConversionContext} for this case.</p>
*
* @param <T> type Generic
* @param argument The argument
* @param locale The locale
Expand Down
Expand Up @@ -20,6 +20,7 @@

import java.nio.charset.Charset;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
* Default implementation of the {@link ConversionContext} interface.
Expand Down
@@ -0,0 +1,60 @@
/*
* Copyright 2017-2022 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.core.convert;

import io.micronaut.core.type.Argument;
import io.micronaut.core.util.ArgumentUtils;

/**
* Immutable variant of {@link io.micronaut.core.convert.ArgumentConversionContext} that can be used as a constant
* in cases where conversion error handling and rejection is not required.
*
* @param <T> The generic type
* @since 3.2.7
* @author graemerocher
*/
public interface ImmutableArgumentConversionContext<T> extends ArgumentConversionContext<T> {
/**
* Create a new simple {@link ConversionContext} for the given generic type variables.
*
* <p>NOTE: The instance returned by this method is NOT thread safe and should be shared
* via static state or between threads.</p>
*
* @param <T> type Generic
* @param argument The argument
* @return The conversion context
* @since 3.2.7
*/
static <T> ImmutableArgumentConversionContext<T> of(Argument<T> argument) {
ArgumentUtils.requireNonNull("argument", argument);
return () -> argument;
}

/**
* Create a simple {@link ConversionContext} for the given generic type variables.
*
* <p>NOTE: The instance returned by this method is NOT thread safe and should be shared
* via static state or between threads.</p>
*
* @param <T> type Generic
* @param type The argument
* @return The conversion context
* @since 3.2.7
*/
static <T> ImmutableArgumentConversionContext<T> of(Class<T> type) {
return of(Argument.of(type));
}
}
Expand Up @@ -195,7 +195,7 @@ public Object intercept(MethodInvocationContext<Object, Object> context) {
// Convert and put as path param
if (argument.getAnnotationMetadata().hasStereotype(Format.class)) {
ConversionService.SHARED.convert(value,
ConversionContext.of(String.class).with(argument.getAnnotationMetadata()))
ConversionContext.STRING.with(argument.getAnnotationMetadata()))
.ifPresent(v -> pathParams.put(name, v));
} else {
pathParams.put(name, value);
Expand Down
Expand Up @@ -18,6 +18,7 @@
import io.micronaut.core.async.publisher.Publishers;
import io.micronaut.core.convert.ArgumentConversionContext;
import io.micronaut.core.convert.ConversionContext;
import io.micronaut.core.convert.ImmutableArgumentConversionContext;
import io.micronaut.core.util.StringUtils;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.HttpMethod;
Expand Down Expand Up @@ -51,7 +52,7 @@
@Filter("/**")
public class CorsFilter implements HttpServerFilter {

private static final ArgumentConversionContext<HttpMethod> CONVERSION_CONTEXT_HTTP_METHOD = ConversionContext.of(HttpMethod.class);
private static final ArgumentConversionContext<HttpMethod> CONVERSION_CONTEXT_HTTP_METHOD = ImmutableArgumentConversionContext.of(HttpMethod.class);

protected final HttpServerConfiguration.CorsConfiguration corsConfiguration;

Expand Down
Expand Up @@ -17,6 +17,7 @@

import io.micronaut.core.convert.ArgumentConversionContext;
import io.micronaut.core.convert.ConversionContext;
import io.micronaut.core.convert.ImmutableArgumentConversionContext;
import io.micronaut.core.convert.TypeConverter;
import io.micronaut.core.convert.value.ConvertibleValues;
import io.micronaut.core.convert.value.ConvertibleValuesMap;
Expand Down Expand Up @@ -45,7 +46,7 @@ public class CorsOriginConverter implements TypeConverter<Map<String, Object>, C
private static final String ALLOW_CREDENTIALS = "allow-credentials";
private static final String MAX_AGE = "max-age";

private static final ArgumentConversionContext<List<HttpMethod>> CONVERSION_CONTEXT_LIST_OF_HTTP_METHOD = ConversionContext.of(Argument.listOf(HttpMethod.class));
private static final ArgumentConversionContext<List<HttpMethod>> CONVERSION_CONTEXT_LIST_OF_HTTP_METHOD = ImmutableArgumentConversionContext.of(Argument.listOf(HttpMethod.class));

@Override
public Optional<CorsOriginConfiguration> convert(Map<String, Object> object, Class<CorsOriginConfiguration> targetType, ConversionContext context) {
Expand Down
4 changes: 2 additions & 2 deletions http/src/main/java/io/micronaut/http/MediaType.java
Expand Up @@ -19,8 +19,8 @@
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.TypeHint;
import io.micronaut.core.convert.ArgumentConversionContext;
import io.micronaut.core.convert.ConversionContext;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.convert.ImmutableArgumentConversionContext;
import io.micronaut.core.naming.NameUtils;
import io.micronaut.core.type.Argument;
import io.micronaut.core.util.CollectionUtils;
Expand Down Expand Up @@ -374,7 +374,7 @@ public class MediaType implements CharSequence {
static final Argument<MediaType> ARGUMENT = Argument.of(MediaType.class);

@Internal
static final ArgumentConversionContext<MediaType> CONVERSION_CONTEXT = ConversionContext.of(ARGUMENT);
static final ArgumentConversionContext<MediaType> CONVERSION_CONTEXT = ImmutableArgumentConversionContext.of(ARGUMENT);

private static final char SEMICOLON = ';';

Expand Down

0 comments on commit b022323

Please sign in to comment.