Skip to content
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

Avoid having TypeConverter defined as beans #10315

Merged
merged 7 commits into from
Jan 4, 2024
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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2017-2024 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.http.server;

import io.micronaut.core.annotation.Internal;
import io.micronaut.core.convert.MutableConversionService;
import io.micronaut.core.convert.TypeConverter;
import io.micronaut.core.convert.TypeConverterRegistrar;
import io.micronaut.http.server.cors.CorsOriginConfiguration;
import io.micronaut.http.server.cors.CorsOriginConverter;

import java.util.Map;

/**
* The HTTP server type converters registrar.
*
* @author Denis Stepanov
* @since 4.3.0
*/
@Internal
public final class HttpServerTypeConvertersRegistrar implements TypeConverterRegistrar {

@Override
public void register(MutableConversionService conversionService) {
CorsOriginConverter corsOriginConverter = new CorsOriginConverter();
conversionService.addConverter(Map.class, CorsOriginConfiguration.class, (TypeConverter) corsOriginConverter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.micronaut.http.server.cors;

import io.micronaut.core.annotation.Internal;
import io.micronaut.core.convert.ArgumentConversionContext;
import io.micronaut.core.convert.ConversionContext;
import io.micronaut.core.convert.ImmutableArgumentConversionContext;
Expand All @@ -23,7 +24,6 @@
import io.micronaut.core.convert.value.ConvertibleValuesMap;
import io.micronaut.core.type.Argument;
import io.micronaut.http.HttpMethod;
import jakarta.inject.Singleton;

import java.util.List;
import java.util.Map;
Expand All @@ -36,7 +36,7 @@
* @author Graeme Rocher
* @since 1.0
*/
@Singleton
@Internal
public class CorsOriginConverter implements TypeConverter<Map<String, Object>, CorsOriginConfiguration> {

private static final String ALLOWED_ORIGINS = "allowed-origins";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright 2017-2020 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.
#

Args = --initialize-at-build-time=io.micronaut.http.server.cors.CorsOriginConverter
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.micronaut.http.server.HttpServerTypeConvertersRegistrar
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@
import io.micronaut.core.convert.MutableConversionService;
import io.micronaut.core.convert.TypeConverterRegistrar;
import io.micronaut.core.util.StringUtils;
import io.micronaut.http.cookie.SameSite;
import io.micronaut.http.cookie.SameSiteConverter;

import java.util.Optional;

/**
* The media type converters registrar.
* The HTTP converters registrar.
*
* @author Denis Stepanov
* @since 3.6.0
*/
@Internal
public final class MediaTypeConvertersRegistrar implements TypeConverterRegistrar {
public final class HttpTypeConverterRegistrar implements TypeConverterRegistrar {

@Override
public void register(MutableConversionService conversionService) {
Expand All @@ -45,5 +47,6 @@ public void register(MutableConversionService conversionService) {
}
}
});
conversionService.addConverter(CharSequence.class, SameSite.class, new SameSiteConverter());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
*/
package io.micronaut.http.cookie;

import io.micronaut.core.annotation.Internal;
import io.micronaut.core.convert.ConversionContext;
import io.micronaut.core.convert.TypeConverter;
import io.micronaut.core.util.StringUtils;
import jakarta.inject.Singleton;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -29,7 +30,7 @@
* @author Sergio del Amo
* @since 3.0.1
*/
@Singleton
@Internal
public class SameSiteConverter implements TypeConverter<CharSequence, SameSite> {
private static final Map<CharSequence, SameSite> CONVERSIONS = new ConcurrentHashMap<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
io.micronaut.http.MediaTypeConvertersRegistrar
io.micronaut.http.HttpTypeConverterRegistrar
io.micronaut.http.converters.SharedHttpConvertersRegistrar
2 changes: 1 addition & 1 deletion src/main/docs/guide/config/customTypeConverter.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ snippet::io.micronaut.docs.config.converters.MapToLocalDateConverter[tags="impor
<3> The implementation delegates to the injected conversion service to convert the values from the Map used to create a `LocalDate`
<4> If an exception occurs during binding, call `reject(..)` which propagates additional information to the container

NOTE: Register new type converters by declaring a bean of type api:core.convert.TypeConverter[]. `ConversionService.SHARED` is deprecated, and it will be removed in a future version of the Micronaut Framework.
NOTE: It's possible to add a custom type converter into `ConversionService.SHARED` by registering it in a api:core.convert.TypeConverterRegistrar[] via the service loader.
2 changes: 1 addition & 1 deletion src/main/docs/guide/httpServer/binding.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ WARNING: Since Java does not retain argument names in bytecode, you must compile

Generally any type that can be converted from a String representation to a Java type via the link:{api}/io/micronaut/core/convert/ConversionService.html[ConversionService] API can be bound to.

This includes most common Java types, however additional link:{api}/io/micronaut/core/convert/TypeConverter.html[TypeConverter] instances can be registered via the service loader or by creating beans of type `TypeConverter`,
This includes most common Java types. However, you can simply add additional link:{api}/io/micronaut/core/convert/TypeConverter.html[TypeConverter] either by defining it as a bean or by registering it in a api:core.convert.TypeConverterRegistrar[] via the service loader.

The handling of nullability deserves special mention. Consider for example the following example:

Expand Down
Loading