You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@felix.apache.org by Cristiano <cv...@gmail.com> on 2018/12/19 20:51:33 UTC
[Converter] How to workaround this ConversionException for an
optional value ?
Hello all,
I'm using Converter to convert a JsonObject (a Map) to an Interface.
I've created this test for a scenario where any bundle is informed:
> IProvisioningConfiguration config = CONVERTER.convert(json)
> .to(IProvisioningConfiguration.class);
>
> assertThat(config).isNotNull();
> assertThat(config.delay()).isEqualTo(5);
> assertThat(config.bundles()).isEmpty();
My problem is that I'm receiving a ConversionException when trying to
access a method from the interface which value was not supplied by the
source object map.
> org.osgi.util.converter.ConversionException: No value for property:
> bundles
> at
> org.osgi.util.converter.ConvertingImpl$4.invoke(ConvertingImpl.java:802)
> at com.sun.proxy.$Proxy8.bundles(Unknown Source)
> at
> br.com.c8tech.vxosgi.config.JsonConvertionUnitTest.testProvisioningConfigurationJsonConvertionToInterface(JsonConvertionUnitTest.java:102)
Is there any way to work with optional values in the target interface
without get this exception ?
many thanks,
Cristiano
Re: [Converter] How to workaround this ConversionException for an
optional value ?
Posted by Cristiano <cv...@gmail.com>.
Hello again,
I found where my problem is...
In Converter's specification converting from Map to target DTO or
JavaBeans there is no need to the source to provide all the keys. the
converter just ignore missing keys. But unfortunately, the same was not
allowed with Interfaces. I can't think one reason for that... :-
From spec:
> Interfaces can provide methods for default values by providing a
> single-argument method override
> in addition to the no-parameter method matching the key name. If the
> type of the default does not
> match the target type it is converted first.
> Default values are used when the key is not present in the map for the
> method. If a key is present
> with a null value, then null is taken as the value and converted to
> the target type.
> If no default is specified and a requested value is not present in the
> map, a ConversionException is
> thrown.
With such approach to use overridden method to set the default values it
is almost impossible to use an interfaces to deal with complex json with
optional attributes.
We are using interfaces to generate compile-time immutable structures
(classes and their builders) and using them everywhere. So reusing those
interfaces appeared to be the right choice in order to receive data from
Configurator's JSON complex structure and then pass the proxy object
created by Converter to our immutable class builder.
I'll need to think of another solution now, any ideas ?
best regards,
Cristiano
Re: [Converter] How to workaround this ConversionException for an
optional value ?
Posted by Cristiano <cv...@gmail.com>.
Hi David,
Would you please move the felix-parent pom to version 6 ?
With the current version 4 is not possible to build it with java 9 or 11.
> [INFO]
> ------------------------------------------------------------------------
> [ERROR] Failed to execute goal
> org.apache.maven.plugins:maven-antrun-plugin:1.8:run
> (set-bundle-required-execution-environment) on project
> org.apache.felix.converter: An Ant BuildException has occured: Unable
> to create javax script engine for javascript
> [ERROR] around Ant part ...<script language="javascript">var System =
> java.lang.System;... @ 4:33 in
> /home/cvgaviao/Development/Development_Java/OSS/felix/converter/converter/target/antrun/build-main.xml
> [ERROR] -> [Help 1]
I had this same problem with maven-ant when I worked on BundleRepository
in order to move it to Repository 1.1
(https://issues.apache.org/jira/browse/FELIX-5988).
best regards,
Cristiano
On 20/12/2018 13:41, David Bosschaert wrote:
> Hi Cristiano,
>
> I fixed FELIX-6006. It would be great if you could validate it.
>
> Best regards,
>
> David
>
> On Thu, 20 Dec 2018 at 13:56, David Bosschaert <da...@gmail.com>
> wrote:
>
>> I have created https://issues.apache.org/jira/browse/FELIX-6006 for it.
>>
>> Best regards,
>>
>> David
>>
>> On Thu, 20 Dec 2018 at 13:10, David Bosschaert <da...@gmail.com>
>> wrote:
>>
>>> Hi Cristiano,
>>>
>>> Yes, you have identified a bug - please open an issue for it. I'll work
>>> on a fix.
>>>
>>> Best regards,
>>>
>>> David
>>>
>>> On Thu, 20 Dec 2018 at 12:55, Cristiano <cv...@gmail.com> wrote:
>>>
>>>> Hi David, many thanks for the insight. It is a very interesting
>>>> approach, ideed.
>>>>
>>>> I just tried with errorHandler() now. I've took the tests from Felix
>>>> Converter and use my own converter instance and it worked for DTO,
>>>> javabeans and other common cases.
>>>>
>>>> But unfortunately, the current implementation of Felix Converter is not
>>>> calling this method when dealing with the Interface's created Proxy.
>>>>
>>>> The code bellow was taken from the invoke(Object proxy, Method method,
>>>> Object[] args) method:
>>>>
>>>>> // If no value is available take the default if
>>>> specified
>>>>> if (val == null) {
>>>>> if (cls.isAnnotation()) {
>>>>> val = method.getDefaultValue();
>>>>> }
>>>>>
>>>>> if (val == null) {
>>>>> if (args != null && args.length == 1) {
>>>>> val = args[0];
>>>>> } else {
>>>>> throw new ConversionException(
>>>>> "No value for property: " +
>>>> propName);
>>>>> }
>>>>> }
>>>>> }
>>>> As you can see, the ConversionException is being thrown and it is not
>>>> captured by the CustomConverterImpl class as it does in other situations.
>>>>
>>>>
>>>> Should I open an issue for that ?
>>>>
>>>> best regards
>>>>
>>>>
>>>> On 20/12/2018 08:35, David Bosschaert wrote:
>>>>> Hi Cristiano,
>>>>>
>>>>> You should be able to do this with a custom errorHandler() [1].
>>>>> For that you'd create a custom converter and supply it with an
>>>> errorHandler
>>>>> function. See also [2] and [3].
>>>>>
>>>>> Hope this helps,
>>>>>
>>>>> David
>>>>>
>>>>> [1]
>>>>>
>>>> https://osgi.org/javadoc/osgi.cmpn/7.0.0/org/osgi/util/converter/ConverterBuilder.html#errorHandler-org.osgi.util.converter.ConverterFunction-
>>>>> [2]
>>>>>
>>>> https://osgi.org/specification/osgi.cmpn/7.0.0/util.converter.html#util.converter-customizing.converters
>>>>> [3]
>>>>>
>>>> https://osgi.org/specification/osgi.cmpn/7.0.0/util.converter.html#d0e147674
>>>>> On Wed, 19 Dec 2018 at 20:51, Cristiano <cv...@gmail.com> wrote:
>>>>>
>>>>>> Hello all,
>>>>>>
>>>>>> I'm using Converter to convert a JsonObject (a Map) to an Interface.
>>>>>>
>>>>>> I've created this test for a scenario where any bundle is informed:
>>>>>>
>>>>>>> IProvisioningConfiguration config = CONVERTER.convert(json)
>>>>>>> .to(IProvisioningConfiguration.class);
>>>>>>>
>>>>>>> assertThat(config).isNotNull();
>>>>>>> assertThat(config.delay()).isEqualTo(5);
>>>>>>> assertThat(config.bundles()).isEmpty();
>>>>>> My problem is that I'm receiving a ConversionException when trying to
>>>>>> access a method from the interface which value was not supplied by the
>>>>>> source object map.
>>>>>>
>>>>>>> org.osgi.util.converter.ConversionException: No value for property:
>>>>>>> bundles
>>>>>>> at
>>>>>>>
>>>> org.osgi.util.converter.ConvertingImpl$4.invoke(ConvertingImpl.java:802)
>>>>>>> at com.sun.proxy.$Proxy8.bundles(Unknown Source)
>>>>>>> at
>>>>>>>
>>>> br.com.c8tech.vxosgi.config.JsonConvertionUnitTest.testProvisioningConfigurationJsonConvertionToInterface(JsonConvertionUnitTest.java:102)
>>>>>> Is there any way to work with optional values in the target interface
>>>>>> without get this exception ?
>>>>>>
>>>>>>
>>>>>> many thanks,
>>>>>>
>>>>>> Cristiano
>>>>>>
>>>>>>
Re: [Converter] How to workaround this ConversionException for an
optional value ?
Posted by Cristiano <cv...@gmail.com>.
Hi David, great !
Ok, I'll validate it this evening.
best regards,
Cristiano
On 20/12/2018 13:41, David Bosschaert wrote:
> Hi Cristiano,
>
> I fixed FELIX-6006. It would be great if you could validate it.
>
> Best regards,
>
> David
>
> On Thu, 20 Dec 2018 at 13:56, David Bosschaert <da...@gmail.com>
> wrote:
>
>> I have created https://issues.apache.org/jira/browse/FELIX-6006 for it.
>>
>> Best regards,
>>
>> David
>>
>> On Thu, 20 Dec 2018 at 13:10, David Bosschaert <da...@gmail.com>
>> wrote:
>>
>>> Hi Cristiano,
>>>
>>> Yes, you have identified a bug - please open an issue for it. I'll work
>>> on a fix.
>>>
>>> Best regards,
>>>
>>> David
>>>
>>> On Thu, 20 Dec 2018 at 12:55, Cristiano <cv...@gmail.com> wrote:
>>>
>>>> Hi David, many thanks for the insight. It is a very interesting
>>>> approach, ideed.
>>>>
>>>> I just tried with errorHandler() now. I've took the tests from Felix
>>>> Converter and use my own converter instance and it worked for DTO,
>>>> javabeans and other common cases.
>>>>
>>>> But unfortunately, the current implementation of Felix Converter is not
>>>> calling this method when dealing with the Interface's created Proxy.
>>>>
>>>> The code bellow was taken from the invoke(Object proxy, Method method,
>>>> Object[] args) method:
>>>>
>>>>> // If no value is available take the default if
>>>> specified
>>>>> if (val == null) {
>>>>> if (cls.isAnnotation()) {
>>>>> val = method.getDefaultValue();
>>>>> }
>>>>>
>>>>> if (val == null) {
>>>>> if (args != null && args.length == 1) {
>>>>> val = args[0];
>>>>> } else {
>>>>> throw new ConversionException(
>>>>> "No value for property: " +
>>>> propName);
>>>>> }
>>>>> }
>>>>> }
>>>> As you can see, the ConversionException is being thrown and it is not
>>>> captured by the CustomConverterImpl class as it does in other situations.
>>>>
>>>>
>>>> Should I open an issue for that ?
>>>>
>>>> best regards
>>>>
>>>>
>>>> On 20/12/2018 08:35, David Bosschaert wrote:
>>>>> Hi Cristiano,
>>>>>
>>>>> You should be able to do this with a custom errorHandler() [1].
>>>>> For that you'd create a custom converter and supply it with an
>>>> errorHandler
>>>>> function. See also [2] and [3].
>>>>>
>>>>> Hope this helps,
>>>>>
>>>>> David
>>>>>
>>>>> [1]
>>>>>
>>>> https://osgi.org/javadoc/osgi.cmpn/7.0.0/org/osgi/util/converter/ConverterBuilder.html#errorHandler-org.osgi.util.converter.ConverterFunction-
>>>>> [2]
>>>>>
>>>> https://osgi.org/specification/osgi.cmpn/7.0.0/util.converter.html#util.converter-customizing.converters
>>>>> [3]
>>>>>
>>>> https://osgi.org/specification/osgi.cmpn/7.0.0/util.converter.html#d0e147674
>>>>> On Wed, 19 Dec 2018 at 20:51, Cristiano <cv...@gmail.com> wrote:
>>>>>
>>>>>> Hello all,
>>>>>>
>>>>>> I'm using Converter to convert a JsonObject (a Map) to an Interface.
>>>>>>
>>>>>> I've created this test for a scenario where any bundle is informed:
>>>>>>
>>>>>>> IProvisioningConfiguration config = CONVERTER.convert(json)
>>>>>>> .to(IProvisioningConfiguration.class);
>>>>>>>
>>>>>>> assertThat(config).isNotNull();
>>>>>>> assertThat(config.delay()).isEqualTo(5);
>>>>>>> assertThat(config.bundles()).isEmpty();
>>>>>> My problem is that I'm receiving a ConversionException when trying to
>>>>>> access a method from the interface which value was not supplied by the
>>>>>> source object map.
>>>>>>
>>>>>>> org.osgi.util.converter.ConversionException: No value for property:
>>>>>>> bundles
>>>>>>> at
>>>>>>>
>>>> org.osgi.util.converter.ConvertingImpl$4.invoke(ConvertingImpl.java:802)
>>>>>>> at com.sun.proxy.$Proxy8.bundles(Unknown Source)
>>>>>>> at
>>>>>>>
>>>> br.com.c8tech.vxosgi.config.JsonConvertionUnitTest.testProvisioningConfigurationJsonConvertionToInterface(JsonConvertionUnitTest.java:102)
>>>>>> Is there any way to work with optional values in the target interface
>>>>>> without get this exception ?
>>>>>>
>>>>>>
>>>>>> many thanks,
>>>>>>
>>>>>> Cristiano
>>>>>>
>>>>>>
Re: [Converter] How to workaround this ConversionException for an
optional value ?
Posted by David Bosschaert <da...@gmail.com>.
Hi Cristiano,
I fixed FELIX-6006. It would be great if you could validate it.
Best regards,
David
On Thu, 20 Dec 2018 at 13:56, David Bosschaert <da...@gmail.com>
wrote:
> I have created https://issues.apache.org/jira/browse/FELIX-6006 for it.
>
> Best regards,
>
> David
>
> On Thu, 20 Dec 2018 at 13:10, David Bosschaert <da...@gmail.com>
> wrote:
>
>> Hi Cristiano,
>>
>> Yes, you have identified a bug - please open an issue for it. I'll work
>> on a fix.
>>
>> Best regards,
>>
>> David
>>
>> On Thu, 20 Dec 2018 at 12:55, Cristiano <cv...@gmail.com> wrote:
>>
>>> Hi David, many thanks for the insight. It is a very interesting
>>> approach, ideed.
>>>
>>> I just tried with errorHandler() now. I've took the tests from Felix
>>> Converter and use my own converter instance and it worked for DTO,
>>> javabeans and other common cases.
>>>
>>> But unfortunately, the current implementation of Felix Converter is not
>>> calling this method when dealing with the Interface's created Proxy.
>>>
>>> The code bellow was taken from the invoke(Object proxy, Method method,
>>> Object[] args) method:
>>>
>>> > // If no value is available take the default if
>>> specified
>>> > if (val == null) {
>>> > if (cls.isAnnotation()) {
>>> > val = method.getDefaultValue();
>>> > }
>>> >
>>> > if (val == null) {
>>> > if (args != null && args.length == 1) {
>>> > val = args[0];
>>> > } else {
>>> > throw new ConversionException(
>>> > "No value for property: " +
>>> propName);
>>> > }
>>> > }
>>> > }
>>>
>>> As you can see, the ConversionException is being thrown and it is not
>>> captured by the CustomConverterImpl class as it does in other situations.
>>>
>>>
>>> Should I open an issue for that ?
>>>
>>> best regards
>>>
>>>
>>> On 20/12/2018 08:35, David Bosschaert wrote:
>>> > Hi Cristiano,
>>> >
>>> > You should be able to do this with a custom errorHandler() [1].
>>> > For that you'd create a custom converter and supply it with an
>>> errorHandler
>>> > function. See also [2] and [3].
>>> >
>>> > Hope this helps,
>>> >
>>> > David
>>> >
>>> > [1]
>>> >
>>> https://osgi.org/javadoc/osgi.cmpn/7.0.0/org/osgi/util/converter/ConverterBuilder.html#errorHandler-org.osgi.util.converter.ConverterFunction-
>>> > [2]
>>> >
>>> https://osgi.org/specification/osgi.cmpn/7.0.0/util.converter.html#util.converter-customizing.converters
>>> > [3]
>>> >
>>> https://osgi.org/specification/osgi.cmpn/7.0.0/util.converter.html#d0e147674
>>> >
>>> > On Wed, 19 Dec 2018 at 20:51, Cristiano <cv...@gmail.com> wrote:
>>> >
>>> >> Hello all,
>>> >>
>>> >> I'm using Converter to convert a JsonObject (a Map) to an Interface.
>>> >>
>>> >> I've created this test for a scenario where any bundle is informed:
>>> >>
>>> >>> IProvisioningConfiguration config = CONVERTER.convert(json)
>>> >>> .to(IProvisioningConfiguration.class);
>>> >>>
>>> >>> assertThat(config).isNotNull();
>>> >>> assertThat(config.delay()).isEqualTo(5);
>>> >>> assertThat(config.bundles()).isEmpty();
>>> >> My problem is that I'm receiving a ConversionException when trying to
>>> >> access a method from the interface which value was not supplied by the
>>> >> source object map.
>>> >>
>>> >>> org.osgi.util.converter.ConversionException: No value for property:
>>> >>> bundles
>>> >>> at
>>> >>>
>>> org.osgi.util.converter.ConvertingImpl$4.invoke(ConvertingImpl.java:802)
>>> >>> at com.sun.proxy.$Proxy8.bundles(Unknown Source)
>>> >>> at
>>> >>>
>>> >>
>>> br.com.c8tech.vxosgi.config.JsonConvertionUnitTest.testProvisioningConfigurationJsonConvertionToInterface(JsonConvertionUnitTest.java:102)
>>> >>
>>> >> Is there any way to work with optional values in the target interface
>>> >> without get this exception ?
>>> >>
>>> >>
>>> >> many thanks,
>>> >>
>>> >> Cristiano
>>> >>
>>> >>
>>>
>>
Re: [Converter] How to workaround this ConversionException for an
optional value ?
Posted by David Bosschaert <da...@gmail.com>.
I have created https://issues.apache.org/jira/browse/FELIX-6006 for it.
Best regards,
David
On Thu, 20 Dec 2018 at 13:10, David Bosschaert <da...@gmail.com>
wrote:
> Hi Cristiano,
>
> Yes, you have identified a bug - please open an issue for it. I'll work on
> a fix.
>
> Best regards,
>
> David
>
> On Thu, 20 Dec 2018 at 12:55, Cristiano <cv...@gmail.com> wrote:
>
>> Hi David, many thanks for the insight. It is a very interesting
>> approach, ideed.
>>
>> I just tried with errorHandler() now. I've took the tests from Felix
>> Converter and use my own converter instance and it worked for DTO,
>> javabeans and other common cases.
>>
>> But unfortunately, the current implementation of Felix Converter is not
>> calling this method when dealing with the Interface's created Proxy.
>>
>> The code bellow was taken from the invoke(Object proxy, Method method,
>> Object[] args) method:
>>
>> > // If no value is available take the default if
>> specified
>> > if (val == null) {
>> > if (cls.isAnnotation()) {
>> > val = method.getDefaultValue();
>> > }
>> >
>> > if (val == null) {
>> > if (args != null && args.length == 1) {
>> > val = args[0];
>> > } else {
>> > throw new ConversionException(
>> > "No value for property: " +
>> propName);
>> > }
>> > }
>> > }
>>
>> As you can see, the ConversionException is being thrown and it is not
>> captured by the CustomConverterImpl class as it does in other situations.
>>
>>
>> Should I open an issue for that ?
>>
>> best regards
>>
>>
>> On 20/12/2018 08:35, David Bosschaert wrote:
>> > Hi Cristiano,
>> >
>> > You should be able to do this with a custom errorHandler() [1].
>> > For that you'd create a custom converter and supply it with an
>> errorHandler
>> > function. See also [2] and [3].
>> >
>> > Hope this helps,
>> >
>> > David
>> >
>> > [1]
>> >
>> https://osgi.org/javadoc/osgi.cmpn/7.0.0/org/osgi/util/converter/ConverterBuilder.html#errorHandler-org.osgi.util.converter.ConverterFunction-
>> > [2]
>> >
>> https://osgi.org/specification/osgi.cmpn/7.0.0/util.converter.html#util.converter-customizing.converters
>> > [3]
>> >
>> https://osgi.org/specification/osgi.cmpn/7.0.0/util.converter.html#d0e147674
>> >
>> > On Wed, 19 Dec 2018 at 20:51, Cristiano <cv...@gmail.com> wrote:
>> >
>> >> Hello all,
>> >>
>> >> I'm using Converter to convert a JsonObject (a Map) to an Interface.
>> >>
>> >> I've created this test for a scenario where any bundle is informed:
>> >>
>> >>> IProvisioningConfiguration config = CONVERTER.convert(json)
>> >>> .to(IProvisioningConfiguration.class);
>> >>>
>> >>> assertThat(config).isNotNull();
>> >>> assertThat(config.delay()).isEqualTo(5);
>> >>> assertThat(config.bundles()).isEmpty();
>> >> My problem is that I'm receiving a ConversionException when trying to
>> >> access a method from the interface which value was not supplied by the
>> >> source object map.
>> >>
>> >>> org.osgi.util.converter.ConversionException: No value for property:
>> >>> bundles
>> >>> at
>> >>>
>> org.osgi.util.converter.ConvertingImpl$4.invoke(ConvertingImpl.java:802)
>> >>> at com.sun.proxy.$Proxy8.bundles(Unknown Source)
>> >>> at
>> >>>
>> >>
>> br.com.c8tech.vxosgi.config.JsonConvertionUnitTest.testProvisioningConfigurationJsonConvertionToInterface(JsonConvertionUnitTest.java:102)
>> >>
>> >> Is there any way to work with optional values in the target interface
>> >> without get this exception ?
>> >>
>> >>
>> >> many thanks,
>> >>
>> >> Cristiano
>> >>
>> >>
>>
>
Re: [Converter] How to workaround this ConversionException for an
optional value ?
Posted by David Bosschaert <da...@gmail.com>.
Hi Cristiano,
Yes, you have identified a bug - please open an issue for it. I'll work on
a fix.
Best regards,
David
On Thu, 20 Dec 2018 at 12:55, Cristiano <cv...@gmail.com> wrote:
> Hi David, many thanks for the insight. It is a very interesting
> approach, ideed.
>
> I just tried with errorHandler() now. I've took the tests from Felix
> Converter and use my own converter instance and it worked for DTO,
> javabeans and other common cases.
>
> But unfortunately, the current implementation of Felix Converter is not
> calling this method when dealing with the Interface's created Proxy.
>
> The code bellow was taken from the invoke(Object proxy, Method method,
> Object[] args) method:
>
> > // If no value is available take the default if specified
> > if (val == null) {
> > if (cls.isAnnotation()) {
> > val = method.getDefaultValue();
> > }
> >
> > if (val == null) {
> > if (args != null && args.length == 1) {
> > val = args[0];
> > } else {
> > throw new ConversionException(
> > "No value for property: " +
> propName);
> > }
> > }
> > }
>
> As you can see, the ConversionException is being thrown and it is not
> captured by the CustomConverterImpl class as it does in other situations.
>
>
> Should I open an issue for that ?
>
> best regards
>
>
> On 20/12/2018 08:35, David Bosschaert wrote:
> > Hi Cristiano,
> >
> > You should be able to do this with a custom errorHandler() [1].
> > For that you'd create a custom converter and supply it with an
> errorHandler
> > function. See also [2] and [3].
> >
> > Hope this helps,
> >
> > David
> >
> > [1]
> >
> https://osgi.org/javadoc/osgi.cmpn/7.0.0/org/osgi/util/converter/ConverterBuilder.html#errorHandler-org.osgi.util.converter.ConverterFunction-
> > [2]
> >
> https://osgi.org/specification/osgi.cmpn/7.0.0/util.converter.html#util.converter-customizing.converters
> > [3]
> >
> https://osgi.org/specification/osgi.cmpn/7.0.0/util.converter.html#d0e147674
> >
> > On Wed, 19 Dec 2018 at 20:51, Cristiano <cv...@gmail.com> wrote:
> >
> >> Hello all,
> >>
> >> I'm using Converter to convert a JsonObject (a Map) to an Interface.
> >>
> >> I've created this test for a scenario where any bundle is informed:
> >>
> >>> IProvisioningConfiguration config = CONVERTER.convert(json)
> >>> .to(IProvisioningConfiguration.class);
> >>>
> >>> assertThat(config).isNotNull();
> >>> assertThat(config.delay()).isEqualTo(5);
> >>> assertThat(config.bundles()).isEmpty();
> >> My problem is that I'm receiving a ConversionException when trying to
> >> access a method from the interface which value was not supplied by the
> >> source object map.
> >>
> >>> org.osgi.util.converter.ConversionException: No value for property:
> >>> bundles
> >>> at
> >>>
> org.osgi.util.converter.ConvertingImpl$4.invoke(ConvertingImpl.java:802)
> >>> at com.sun.proxy.$Proxy8.bundles(Unknown Source)
> >>> at
> >>>
> >>
> br.com.c8tech.vxosgi.config.JsonConvertionUnitTest.testProvisioningConfigurationJsonConvertionToInterface(JsonConvertionUnitTest.java:102)
> >>
> >> Is there any way to work with optional values in the target interface
> >> without get this exception ?
> >>
> >>
> >> many thanks,
> >>
> >> Cristiano
> >>
> >>
>
Re: [Converter] How to workaround this ConversionException for an
optional value ?
Posted by Cristiano <cv...@gmail.com>.
Hi David, many thanks for the insight. It is a very interesting
approach, ideed.
I just tried with errorHandler() now. I've took the tests from Felix
Converter and use my own converter instance and it worked for DTO,
javabeans and other common cases.
But unfortunately, the current implementation of Felix Converter is not
calling this method when dealing with the Interface's created Proxy.
The code bellow was taken from the invoke(Object proxy, Method method,
Object[] args) method:
> // If no value is available take the default if specified
> if (val == null) {
> if (cls.isAnnotation()) {
> val = method.getDefaultValue();
> }
>
> if (val == null) {
> if (args != null && args.length == 1) {
> val = args[0];
> } else {
> throw new ConversionException(
> "No value for property: " + propName);
> }
> }
> }
As you can see, the ConversionException is being thrown and it is not
captured by the CustomConverterImpl class as it does in other situations.
Should I open an issue for that ?
best regards
On 20/12/2018 08:35, David Bosschaert wrote:
> Hi Cristiano,
>
> You should be able to do this with a custom errorHandler() [1].
> For that you'd create a custom converter and supply it with an errorHandler
> function. See also [2] and [3].
>
> Hope this helps,
>
> David
>
> [1]
> https://osgi.org/javadoc/osgi.cmpn/7.0.0/org/osgi/util/converter/ConverterBuilder.html#errorHandler-org.osgi.util.converter.ConverterFunction-
> [2]
> https://osgi.org/specification/osgi.cmpn/7.0.0/util.converter.html#util.converter-customizing.converters
> [3]
> https://osgi.org/specification/osgi.cmpn/7.0.0/util.converter.html#d0e147674
>
> On Wed, 19 Dec 2018 at 20:51, Cristiano <cv...@gmail.com> wrote:
>
>> Hello all,
>>
>> I'm using Converter to convert a JsonObject (a Map) to an Interface.
>>
>> I've created this test for a scenario where any bundle is informed:
>>
>>> IProvisioningConfiguration config = CONVERTER.convert(json)
>>> .to(IProvisioningConfiguration.class);
>>>
>>> assertThat(config).isNotNull();
>>> assertThat(config.delay()).isEqualTo(5);
>>> assertThat(config.bundles()).isEmpty();
>> My problem is that I'm receiving a ConversionException when trying to
>> access a method from the interface which value was not supplied by the
>> source object map.
>>
>>> org.osgi.util.converter.ConversionException: No value for property:
>>> bundles
>>> at
>>> org.osgi.util.converter.ConvertingImpl$4.invoke(ConvertingImpl.java:802)
>>> at com.sun.proxy.$Proxy8.bundles(Unknown Source)
>>> at
>>>
>> br.com.c8tech.vxosgi.config.JsonConvertionUnitTest.testProvisioningConfigurationJsonConvertionToInterface(JsonConvertionUnitTest.java:102)
>>
>> Is there any way to work with optional values in the target interface
>> without get this exception ?
>>
>>
>> many thanks,
>>
>> Cristiano
>>
>>
Re: [Converter] How to workaround this ConversionException for an
optional value ?
Posted by David Bosschaert <da...@gmail.com>.
Hi Cristiano,
You should be able to do this with a custom errorHandler() [1].
For that you'd create a custom converter and supply it with an errorHandler
function. See also [2] and [3].
Hope this helps,
David
[1]
https://osgi.org/javadoc/osgi.cmpn/7.0.0/org/osgi/util/converter/ConverterBuilder.html#errorHandler-org.osgi.util.converter.ConverterFunction-
[2]
https://osgi.org/specification/osgi.cmpn/7.0.0/util.converter.html#util.converter-customizing.converters
[3]
https://osgi.org/specification/osgi.cmpn/7.0.0/util.converter.html#d0e147674
On Wed, 19 Dec 2018 at 20:51, Cristiano <cv...@gmail.com> wrote:
> Hello all,
>
> I'm using Converter to convert a JsonObject (a Map) to an Interface.
>
> I've created this test for a scenario where any bundle is informed:
>
> > IProvisioningConfiguration config = CONVERTER.convert(json)
> > .to(IProvisioningConfiguration.class);
> >
> > assertThat(config).isNotNull();
> > assertThat(config.delay()).isEqualTo(5);
> > assertThat(config.bundles()).isEmpty();
>
> My problem is that I'm receiving a ConversionException when trying to
> access a method from the interface which value was not supplied by the
> source object map.
>
> > org.osgi.util.converter.ConversionException: No value for property:
> > bundles
> > at
> > org.osgi.util.converter.ConvertingImpl$4.invoke(ConvertingImpl.java:802)
> > at com.sun.proxy.$Proxy8.bundles(Unknown Source)
> > at
> >
> br.com.c8tech.vxosgi.config.JsonConvertionUnitTest.testProvisioningConfigurationJsonConvertionToInterface(JsonConvertionUnitTest.java:102)
>
> Is there any way to work with optional values in the target interface
> without get this exception ?
>
>
> many thanks,
>
> Cristiano
>
>