From 14d84f3eba7595c82d0c7f1da763c796cdec5400 Mon Sep 17 00:00:00 2001 From: Denis Stepanov Date: Mon, 4 Sep 2023 08:29:28 +0200 Subject: [PATCH] Convert configuration to beans (#561) --- config/accepted-api-changes.json | 195 ++++++++++++++++++ .../DefaultDeserializationConfiguration.java | 59 ++++++ .../config/DefaultSerdeConfiguration.java | 105 ++++++++++ .../DefaultSerializationConfiguration.java | 54 +++++ .../config/DeserializationConfiguration.java | 4 - .../serde/config/SerdeConfiguration.java | 4 - .../config/SerializationConfiguration.java | 4 - 7 files changed, 413 insertions(+), 12 deletions(-) create mode 100644 serde-api/src/main/java/io/micronaut/serde/config/DefaultDeserializationConfiguration.java create mode 100644 serde-api/src/main/java/io/micronaut/serde/config/DefaultSerdeConfiguration.java create mode 100644 serde-api/src/main/java/io/micronaut/serde/config/DefaultSerializationConfiguration.java diff --git a/config/accepted-api-changes.json b/config/accepted-api-changes.json index b3fc723cf..69aa8f98d 100644 --- a/config/accepted-api-changes.json +++ b/config/accepted-api-changes.json @@ -13,5 +13,200 @@ "type": "io.micronaut.serde.bson.BsonBinaryMapper", "member": "Constructor io.micronaut.serde.bson.BsonBinaryMapper(io.micronaut.serde.SerdeRegistry)", "reason": "Changed in Micronaut Framework 4" + }, + { + "type": "io.micronaut.serde.config.$DeserializationConfiguration$Intercepted$Definition$Reference", + "member": "Field $ANNOTATION_METADATA", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$DeserializationConfiguration$Intercepted$Definition$Reference", + "member": "Constructor io.micronaut.serde.config.$DeserializationConfiguration$Intercepted$Definition$Reference()", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$DeserializationConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.core.annotation.AnnotationSource", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$DeserializationConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.inject.BeanContextConditional", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$DeserializationConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.core.annotation.AnnotationMetadataProvider", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$DeserializationConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.core.beans.BeanInfo", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$DeserializationConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.core.annotation.AnnotationMetadataDelegate", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$DeserializationConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.inject.AdvisedBeanType", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$DeserializationConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.core.annotation.AnnotationMetadata", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$DeserializationConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.core.type.ArgumentCoercible", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$DeserializationConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.inject.QualifiedBeanType", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$DeserializationConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.inject.BeanType", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$DeserializationConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.inject.BeanDefinitionReference", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerdeConfiguration$Intercepted$Definition$Reference", + "member": "Field $ANNOTATION_METADATA", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerdeConfiguration$Intercepted$Definition$Reference", + "member": "Constructor io.micronaut.serde.config.$SerdeConfiguration$Intercepted$Definition$Reference()", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerdeConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.core.annotation.AnnotationSource", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerdeConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.inject.BeanContextConditional", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerdeConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.core.annotation.AnnotationMetadataProvider", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerdeConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.core.beans.BeanInfo", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerdeConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.core.annotation.AnnotationMetadataDelegate", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerdeConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.inject.AdvisedBeanType", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerdeConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.core.annotation.AnnotationMetadata", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerdeConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.core.type.ArgumentCoercible", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerdeConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.inject.QualifiedBeanType", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerdeConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.inject.BeanType", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerdeConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.inject.BeanDefinitionReference", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerializationConfiguration$Intercepted$Definition$Reference", + "member": "Field $ANNOTATION_METADATA", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerializationConfiguration$Intercepted$Definition$Reference", + "member": "Constructor io.micronaut.serde.config.$SerializationConfiguration$Intercepted$Definition$Reference()", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerializationConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.core.annotation.AnnotationSource", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerializationConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.inject.BeanContextConditional", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerializationConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.core.annotation.AnnotationMetadataProvider", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerializationConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.core.beans.BeanInfo", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerializationConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.core.annotation.AnnotationMetadataDelegate", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerializationConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.inject.AdvisedBeanType", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerializationConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.core.annotation.AnnotationMetadata", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerializationConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.core.type.ArgumentCoercible", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerializationConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.inject.QualifiedBeanType", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerializationConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.inject.BeanType", + "reason": "Internal" + }, + { + "type": "io.micronaut.serde.config.$SerializationConfiguration$Intercepted$Definition$Reference", + "member": "Implemented interface io.micronaut.inject.BeanDefinitionReference", + "reason": "Internal" } ] diff --git a/serde-api/src/main/java/io/micronaut/serde/config/DefaultDeserializationConfiguration.java b/serde-api/src/main/java/io/micronaut/serde/config/DefaultDeserializationConfiguration.java new file mode 100644 index 000000000..90e350ca2 --- /dev/null +++ b/serde-api/src/main/java/io/micronaut/serde/config/DefaultDeserializationConfiguration.java @@ -0,0 +1,59 @@ +/* + * Copyright 2017-2023 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.serde.config; + +import io.micronaut.context.annotation.BootstrapContextCompatible; +import io.micronaut.context.annotation.ConfigurationInject; +import io.micronaut.context.annotation.ConfigurationProperties; +import io.micronaut.core.bind.annotation.Bindable; +import io.micronaut.core.util.StringUtils; + +/** + * Default implementation of {@link DeserializationConfiguration}. + * + * @author Denis Stepanov + */ +@ConfigurationProperties(DeserializationConfiguration.PREFIX) +@BootstrapContextCompatible +final class DefaultDeserializationConfiguration implements DeserializationConfiguration { + private final boolean ignoreUnknown; + private final int arraySizeThreshold; + private final boolean strictNullable; + + @ConfigurationInject + DefaultDeserializationConfiguration(@Bindable(defaultValue = StringUtils.TRUE) boolean ignoreUnknown, + @Bindable(defaultValue = "100") int arraySizeThreshold, + @Bindable(defaultValue = StringUtils.FALSE) boolean strictNullable) { + this.ignoreUnknown = ignoreUnknown; + this.arraySizeThreshold = arraySizeThreshold; + this.strictNullable = strictNullable; + } + + @Override + public boolean isIgnoreUnknown() { + return ignoreUnknown; + } + + @Override + public int getArraySizeThreshold() { + return arraySizeThreshold; + } + + @Override + public boolean isStrictNullable() { + return strictNullable; + } +} diff --git a/serde-api/src/main/java/io/micronaut/serde/config/DefaultSerdeConfiguration.java b/serde-api/src/main/java/io/micronaut/serde/config/DefaultSerdeConfiguration.java new file mode 100644 index 000000000..fa9a31fca --- /dev/null +++ b/serde-api/src/main/java/io/micronaut/serde/config/DefaultSerdeConfiguration.java @@ -0,0 +1,105 @@ +/* + * Copyright 2017-2023 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.serde.config; + +import io.micronaut.context.annotation.BootstrapContextCompatible; +import io.micronaut.context.annotation.ConfigurationInject; +import io.micronaut.context.annotation.ConfigurationProperties; +import io.micronaut.core.bind.annotation.Bindable; +import io.micronaut.serde.LimitingStream; + +import java.util.List; +import java.util.Locale; +import java.util.Optional; +import java.util.TimeZone; + +/** + * The default implementation of SerdeConfiguration. + * + * @author Denis Stepanov + */ +@ConfigurationProperties(SerdeConfiguration.PREFIX) +@BootstrapContextCompatible +final class DefaultSerdeConfiguration implements SerdeConfiguration { + + private final Optional dateFormat; + private final TimeShape timeWriteShape; + private final NumericTimeUnit numericTimeUnit; + private final boolean writeBinaryAsArray; + private final Optional locale; + private final Optional timeZone; + private final List includedIntrospectionPackages; + private final int maximumNestingDepth; + + @ConfigurationInject + DefaultSerdeConfiguration(Optional dateFormat, + @Bindable(defaultValue = "STRING") TimeShape timeWriteShape, + @Bindable(defaultValue = "SECONDS") NumericTimeUnit numericTimeUnit, + @Bindable(defaultValue = "true") boolean writeBinaryAsArray, + Optional locale, + Optional timeZone, + @Bindable(defaultValue = "io.micronaut") List includedIntrospectionPackages, + @Bindable(defaultValue = LimitingStream.DEFAULT_MAXIMUM_DEPTH + "") int maximumNestingDepth) { + this.dateFormat = dateFormat; + this.timeWriteShape = timeWriteShape; + this.numericTimeUnit = numericTimeUnit; + this.writeBinaryAsArray = writeBinaryAsArray; + this.locale = locale; + this.timeZone = timeZone; + this.includedIntrospectionPackages = includedIntrospectionPackages; + this.maximumNestingDepth = maximumNestingDepth; + } + + @Override + public Optional getDateFormat() { + return dateFormat; + } + + @Override + public TimeShape getTimeWriteShape() { + return timeWriteShape; + } + + @Override + public NumericTimeUnit getNumericTimeUnit() { + return numericTimeUnit; + } + + @Override + public boolean isWriteBinaryAsArray() { + return writeBinaryAsArray; + } + + @Override + public Optional getLocale() { + return locale; + } + + @Override + public Optional getTimeZone() { + return timeZone; + } + + @Override + public List getIncludedIntrospectionPackages() { + return includedIntrospectionPackages; + } + + @Override + public int getMaximumNestingDepth() { + return maximumNestingDepth; + } +} diff --git a/serde-api/src/main/java/io/micronaut/serde/config/DefaultSerializationConfiguration.java b/serde-api/src/main/java/io/micronaut/serde/config/DefaultSerializationConfiguration.java new file mode 100644 index 000000000..4c762912a --- /dev/null +++ b/serde-api/src/main/java/io/micronaut/serde/config/DefaultSerializationConfiguration.java @@ -0,0 +1,54 @@ +/* + * Copyright 2017-2023 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.serde.config; + +import io.micronaut.context.annotation.BootstrapContextCompatible; +import io.micronaut.context.annotation.ConfigurationInject; +import io.micronaut.context.annotation.ConfigurationProperties; +import io.micronaut.core.bind.annotation.Bindable; +import io.micronaut.core.util.StringUtils; +import io.micronaut.serde.config.annotation.SerdeConfig; + +/** + * Default implementation of {@link SerializationConfiguration}. + * + * @author Denis Stepanov + */ +@ConfigurationProperties(SerializationConfiguration.PREFIX) +@BootstrapContextCompatible +final class DefaultSerializationConfiguration implements SerializationConfiguration { + + private final SerdeConfig.SerInclude inclusion; + private final boolean alwaysSerializeErrorsAsList; + + @ConfigurationInject + DefaultSerializationConfiguration(@Bindable(defaultValue = "NON_EMPTY") SerdeConfig.SerInclude inclusion, + @Bindable(defaultValue = StringUtils.TRUE) boolean alwaysSerializeErrorsAsList) { + this.inclusion = inclusion; + this.alwaysSerializeErrorsAsList = alwaysSerializeErrorsAsList; + } + + @Override + public SerdeConfig.SerInclude getInclusion() { + return inclusion; + } + + @Override + public boolean isAlwaysSerializeErrorsAsList() { + return alwaysSerializeErrorsAsList; + } + +} diff --git a/serde-api/src/main/java/io/micronaut/serde/config/DeserializationConfiguration.java b/serde-api/src/main/java/io/micronaut/serde/config/DeserializationConfiguration.java index 2abc8ccfc..2ba3cea11 100644 --- a/serde-api/src/main/java/io/micronaut/serde/config/DeserializationConfiguration.java +++ b/serde-api/src/main/java/io/micronaut/serde/config/DeserializationConfiguration.java @@ -15,16 +15,12 @@ */ package io.micronaut.serde.config; -import io.micronaut.context.annotation.BootstrapContextCompatible; -import io.micronaut.context.annotation.ConfigurationProperties; import io.micronaut.core.bind.annotation.Bindable; import io.micronaut.core.util.StringUtils; /** * Configuration for deserialization. */ -@ConfigurationProperties(DeserializationConfiguration.PREFIX) -@BootstrapContextCompatible public interface DeserializationConfiguration { String PREFIX = SerdeConfiguration.PREFIX + ".deserialization"; diff --git a/serde-api/src/main/java/io/micronaut/serde/config/SerdeConfiguration.java b/serde-api/src/main/java/io/micronaut/serde/config/SerdeConfiguration.java index dd88ce030..217922125 100644 --- a/serde-api/src/main/java/io/micronaut/serde/config/SerdeConfiguration.java +++ b/serde-api/src/main/java/io/micronaut/serde/config/SerdeConfiguration.java @@ -15,8 +15,6 @@ */ package io.micronaut.serde.config; -import io.micronaut.context.annotation.BootstrapContextCompatible; -import io.micronaut.context.annotation.ConfigurationProperties; import io.micronaut.core.bind.annotation.Bindable; import io.micronaut.serde.LimitingStream; @@ -29,8 +27,6 @@ * * @author gkrocher */ -@ConfigurationProperties(SerdeConfiguration.PREFIX) -@BootstrapContextCompatible public interface SerdeConfiguration { String PREFIX = "micronaut.serde"; diff --git a/serde-api/src/main/java/io/micronaut/serde/config/SerializationConfiguration.java b/serde-api/src/main/java/io/micronaut/serde/config/SerializationConfiguration.java index 5ad834a4a..0cd55b535 100644 --- a/serde-api/src/main/java/io/micronaut/serde/config/SerializationConfiguration.java +++ b/serde-api/src/main/java/io/micronaut/serde/config/SerializationConfiguration.java @@ -15,8 +15,6 @@ */ package io.micronaut.serde.config; -import io.micronaut.context.annotation.BootstrapContextCompatible; -import io.micronaut.context.annotation.ConfigurationProperties; import io.micronaut.core.annotation.NonNull; import io.micronaut.core.bind.annotation.Bindable; import io.micronaut.core.util.StringUtils; @@ -25,8 +23,6 @@ /** * Configuration for serialization. */ -@ConfigurationProperties(SerializationConfiguration.PREFIX) -@BootstrapContextCompatible public interface SerializationConfiguration { String PREFIX = SerdeConfiguration.PREFIX + ".serialization";