From 3c6b160894826fe2bfbe6d07eac770afd4100020 Mon Sep 17 00:00:00 2001 From: David Walluck Date: Thu, 3 Dec 2026 09:57:23 -0500 Subject: [PATCH] Fix null handling for values --- .../rwx/test/koji/KojiMultiCallTest.java | 12 +++ .../xml/kojiMulticallGetRPMResponse.xml | 86 +++++++++++++++++++ .../org/commonjava/rwx/vocab/ValueType.java | 25 +++++- .../rwx/vocab/ValueTypeNullTest.java | 54 ++++++++++++ 4 files changed, 175 insertions(+), 2 deletions(-) create mode 100644 rwx-test/src/test/resources/xml/kojiMulticallGetRPMResponse.xml create mode 100644 rwx/src/test/java/org/commonjava/rwx/vocab/ValueTypeNullTest.java diff --git a/rwx-test/src/test/java/org/commonjava/rwx/test/koji/KojiMultiCallTest.java b/rwx-test/src/test/java/org/commonjava/rwx/test/koji/KojiMultiCallTest.java index 5ef5c30..8475a57 100644 --- a/rwx-test/src/test/java/org/commonjava/rwx/test/koji/KojiMultiCallTest.java +++ b/rwx-test/src/test/java/org/commonjava/rwx/test/koji/KojiMultiCallTest.java @@ -27,6 +27,7 @@ import java.util.Map; import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertNull; import static junit.framework.TestCase.assertTrue; /** @@ -191,4 +192,15 @@ private void assertMultiCallResponse( MultiCallResponse response ) assertEquals( 8829, data2_1Map.get( "id" ) ); } + @Test + public void parseMultiCallGetRPMResponseWithNilMembers() throws Exception + { + String source = getXMLString( "kojiMulticallGetRPMResponse" ); + MultiCallResponse parsed = + new RWXMapper().parse( new ByteArrayInputStream( source.getBytes( StandardCharsets.UTF_8 ) ), MultiCallResponse.class ); + + List valueObjs = parsed.getValueObjs(); + assertEquals( 1, valueObjs.size() ); + } + } diff --git a/rwx-test/src/test/resources/xml/kojiMulticallGetRPMResponse.xml b/rwx-test/src/test/resources/xml/kojiMulticallGetRPMResponse.xml new file mode 100644 index 0000000..e0c7ff7 --- /dev/null +++ b/rwx-test/src/test/resources/xml/kojiMulticallGetRPMResponse.xml @@ -0,0 +1,86 @@ + + + + + + + + +arch +x86_64 + + +build_id + + +buildroot_id + + +buildtime +1645136189 + + +draft + + +epoch + + +external_repo_id +123 + + +external_repo_name +rhel-8.10.0-z-build-repo + + +extra + + +id +12345678 + + +metadata_only +0 + + +name +cyrus-sasl-lib + + +payloadhash +93bf5b96d9dfd5201b016927d7a4b61c + + +release +6.el8_5 + + +sha1header +598501096d3a56cb706f63cc921cc41258a267f4 + + +sha256header +2a26ba019b2593773efb2f100c7c173fa906c61ba5031ced85f3f0aa39e7aa37 + + +sha3_256header + + +sigmd5 +93bf5b96d9dfd5201b016927d7a4b61c + + +size + + +version +2.1.27 + + + + + + + diff --git a/rwx/src/main/java/org/commonjava/rwx/vocab/ValueType.java b/rwx/src/main/java/org/commonjava/rwx/vocab/ValueType.java index 489922f..3e5a4eb 100644 --- a/rwx/src/main/java/org/commonjava/rwx/vocab/ValueType.java +++ b/rwx/src/main/java/org/commonjava/rwx/vocab/ValueType.java @@ -39,12 +39,20 @@ public enum ValueType @Override public String toString( final Object value ) throws CoercionException { + if ( value == null ) + { + return null; + } throw new CoercionException( "Cannot coerce STRUCT types." ); } @Override public Object fromString( final String value ) throws CoercionException { + if ( value == null ) + { + return null; + } throw new CoercionException( "Cannot coerce STRUCT types." ); } }, Map.class, XmlRpcConstants.STRUCT ), @@ -54,12 +62,20 @@ public Object fromString( final String value ) throws CoercionException @Override public String toString( final Object value ) throws CoercionException { + if ( value == null ) + { + return null; + } throw new CoercionException( "Cannot coerce ARRAY types." ); } @Override public Object fromString( final String value ) throws CoercionException { + if ( value == null ) + { + return null; + } throw new CoercionException( "Cannot coerce ARRAY types." ); } }, List.class, XmlRpcConstants.ARRAY ), @@ -72,6 +88,11 @@ public Object fromString( final String value ) return Nil.NIL_VALUE; } + @Override + public String toString( final Object value ) + { + return null; + } }, Nil.class, "nil" ), INT( new ValueCoercion( "INT-to-String" ) @@ -108,12 +129,12 @@ public Object fromString( final String value ) throws CoercionException } /** - * If the value is of type Integer, convert it to Long + * If the value is of type Integer, convert it to Long, or null if nil. */ @Override public Object upgradeCast( Object value ) { - if ( value instanceof Long ) + if ( value == null || value instanceof Long ) { return value; } diff --git a/rwx/src/test/java/org/commonjava/rwx/vocab/ValueTypeNullTest.java b/rwx/src/test/java/org/commonjava/rwx/vocab/ValueTypeNullTest.java new file mode 100644 index 0000000..82bea1b --- /dev/null +++ b/rwx/src/test/java/org/commonjava/rwx/vocab/ValueTypeNullTest.java @@ -0,0 +1,54 @@ +/* + * Copyright © 2010 Red Hat, Inc. (http://github.com/Commonjava/commonjava) + * + * 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 + * + * http://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 org.commonjava.rwx.vocab; + +import org.commonjava.rwx.error.CoercionException; +import org.commonjava.rwx.error.XmlRpcException; +import org.commonjava.rwx.util.ParseUtils; +import org.commonjava.rwx.util.RenderUtils; +import org.junit.Test; + +import java.util.Collections; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; + +public class ValueTypeNullTest +{ + @Test + public void testNull() throws CoercionException, XmlRpcException + { + for ( final ValueType type : ValueType.values() ) + { + if ( type == ValueType.NIL ) + { + assertSame( Nil.NIL_VALUE, type.coercion().fromString( null ) ); + } + else + { + assertNull( type.coercion().fromString( null ) ); + } + + assertNull( type.coercion().toString( null ) ); + assertNull( type.coercion().upgradeCast( null ) ); + } + + assertNull( ParseUtils.upgradeCast( Long.class, null ) ); + assertNull( ValueType.NIL.coercion().toString( Nil.NIL_VALUE ) ); + assertEquals( RenderUtils.toXMLString( Collections.singletonList( null ) ), RenderUtils.toXMLString( Collections.singletonList( Nil.NIL_VALUE ) ) ); + } +}