Skip to content

Commit

Permalink
Merge branch 'openmrs:master' into RESTWS-910
Browse files Browse the repository at this point in the history
  • Loading branch information
mherman22 authored Jun 21, 2023
2 parents e4e2ec0 + a15b055 commit 04a6e17
Show file tree
Hide file tree
Showing 19 changed files with 90 additions and 59 deletions.
2 changes: 1 addition & 1 deletion integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest</artifactId>
<version>2.40.0-SNAPSHOT</version>
<version>2.41.0-SNAPSHOT</version>
</parent>
<artifactId>webservices.rest-integration-tests</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion omod-1.10/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest</artifactId>
<version>2.40.0-SNAPSHOT</version>
<version>2.41.0-SNAPSHOT</version>
</parent>
<artifactId>webservices.rest-omod-1.10</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion omod-1.11/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest</artifactId>
<version>2.40.0-SNAPSHOT</version>
<version>2.41.0-SNAPSHOT</version>
</parent>
<artifactId>webservices.rest-omod-1.11</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion omod-1.12/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest</artifactId>
<version>2.40.0-SNAPSHOT</version>
<version>2.41.0-SNAPSHOT</version>
</parent>
<artifactId>webservices.rest-omod-1.12</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion omod-1.8/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest</artifactId>
<version>2.40.0-SNAPSHOT</version>
<version>2.41.0-SNAPSHOT</version>
</parent>
<artifactId>webservices.rest-omod-1.8</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion omod-1.9/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest</artifactId>
<version>2.40.0-SNAPSHOT</version>
<version>2.41.0-SNAPSHOT</version>
</parent>
<artifactId>webservices.rest-omod-1.9</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion omod-2.0/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest</artifactId>
<version>2.40.0-SNAPSHOT</version>
<version>2.41.0-SNAPSHOT</version>
</parent>
<artifactId>webservices.rest-omod-2.0</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion omod-2.1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest</artifactId>
<version>2.40.0-SNAPSHOT</version>
<version>2.41.0-SNAPSHOT</version>
</parent>
<artifactId>webservices.rest-omod-2.1</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion omod-2.2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest</artifactId>
<version>2.40.0-SNAPSHOT</version>
<version>2.41.0-SNAPSHOT</version>
</parent>
<artifactId>webservices.rest-omod-2.2</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion omod-2.3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest</artifactId>
<version>2.40.0-SNAPSHOT</version>
<version>2.41.0-SNAPSHOT</version>
</parent>
<artifactId>webservices.rest-omod-2.3</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion omod-2.4/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>webservices.rest</artifactId>
<groupId>org.openmrs.module</groupId>
<version>2.40.0-SNAPSHOT</version>
<version>2.41.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion omod-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest</artifactId>
<version>2.40.0-SNAPSHOT</version>
<version>2.41.0-SNAPSHOT</version>
</parent>
<artifactId>webservices.rest-omod-common</artifactId>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public class ConversionUtil {

// This would better be a Map<Pair<Class, String>, Type> but adding the dependency for
// org.apache.commons.lang3.tuple.Pair (through omrs-api) messed up other tests
private static Map<String, Type> typeVariableMap = new ConcurrentHashMap<String, Type>();
private static final Map<String, Type> typeVariableMap = new ConcurrentHashMap<String, Type>();

private static ConcurrentMap<Class<?>, Converter> converterCache;

private static Converter nullConverter;
private static final Converter nullConverter;

static {
converterCache = new ConcurrentHashMap<Class<?>, Converter>();
Expand Down Expand Up @@ -170,8 +170,9 @@ public static Object convert(Object object, Type toType, Object instance) throws
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Object convert(Object object, Type toType) throws ConversionException {
if (object == null)
return object;
if (object == null) {
return null;
}

Class<?> toClass = toType instanceof Class ? ((Class<?>) toType) : (Class<?>) (((ParameterizedType) toType)
.getRawType());
Expand All @@ -188,7 +189,7 @@ public static Object convert(Object object, Type toType) throws ConversionExcept
Object ret = Array.newInstance(targetElementType, input.size());

int i = 0;
for (Object element : (Collection) object) {
for (Object element : input) {
Array.set(ret, i, convert(element, targetElementType));
++i;
}
Expand All @@ -210,8 +211,9 @@ public static Object convert(Object object, Type toType) throws ConversionExcept
// if we have generic type information for the target collection, we can use it to do conversion
ParameterizedType toParameterizedType = (ParameterizedType) toType;
Type targetElementType = toParameterizedType.getActualTypeArguments()[0];
for (Object element : (Collection) object)
for (Object element : (Collection) object) {
ret.add(convert(element, targetElementType));
}
} else {
// otherwise we must just add all items in a non-type-safe manner
ret.addAll((Collection) object);
Expand All @@ -221,8 +223,9 @@ public static Object convert(Object object, Type toType) throws ConversionExcept

// otherwise we're converting _to_ a non-collection type

if (toClass.isAssignableFrom(object.getClass()))
if (toClass.isAssignableFrom(object.getClass())) {
return object;
}

// Numbers with a decimal are always assumed to be Double, so convert to Float, if necessary
if (toClass.isAssignableFrom(Float.class) && object instanceof Double) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import java.util.Arrays;
import java.util.List;

import org.openmrs.api.APIException;
import org.openmrs.api.context.Context;
import org.openmrs.api.context.ServiceContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
Expand All @@ -35,17 +37,17 @@ public class DynamicBeanConfiguration {
* otherwise it falls back to the old one.
*/
@Bean(name = "jsonHttpMessageConverter")
public HttpMessageConverter getMappingJacksonHttpMessageConverter() throws Exception {
public HttpMessageConverter<?> getMappingJacksonHttpMessageConverter() throws Exception {

Class<?> clazz;
try {
clazz = Context.loadClass("org.springframework.http.converter.json.MappingJacksonHttpMessageConverter");
clazz = Context.loadClass("org.springframework.http.converter.json.MappingJackson2HttpMessageConverter");
}
catch (ClassNotFoundException e) {
clazz = Context.loadClass("org.springframework.http.converter.json.MappingJackson2HttpMessageConverter");
clazz = Context.loadClass("org.springframework.http.converter.json.MappingJacksonHttpMessageConverter");
}

return (HttpMessageConverter) clazz.newInstance();
return (HttpMessageConverter<?>) clazz.newInstance();
}

/**
Expand Down Expand Up @@ -75,7 +77,7 @@ public AbstractHandlerExceptionResolver getHandlerExceptionResolver() throws Exc

bean = (AbstractHandlerExceptionResolver) clazz.newInstance();

Method method = bean.getClass().getMethod("setMessageConverters", new Class[] { HttpMessageConverter[].class });
Method method = bean.getClass().getMethod("setMessageConverters", HttpMessageConverter[].class);
method.invoke(bean, new Object[] { new HttpMessageConverter[] { stringHttpMessageConverter,
jsonHttpMessageConverter, xmlMarshallingHttpMessageConverter } });
}
Expand All @@ -85,15 +87,12 @@ public AbstractHandlerExceptionResolver getHandlerExceptionResolver() throws Exc

bean = (AbstractHandlerExceptionResolver) clazz.newInstance();

Method method = bean.getClass().getMethod("setMessageConverters", new Class[] { List.class });
method.invoke(bean, new Object[] { Arrays.asList( stringHttpMessageConverter,
jsonHttpMessageConverter, xmlMarshallingHttpMessageConverter ) });
Method method = bean.getClass().getMethod("setMessageConverters", List.class);
method.invoke(bean, Arrays.asList(stringHttpMessageConverter,
jsonHttpMessageConverter, xmlMarshallingHttpMessageConverter));
}

if (bean != null) {
bean.setOrder(1);

}
bean.setOrder(1);

return bean;
}
Expand All @@ -110,10 +109,10 @@ public AbstractHandlerMapping getHandlerMapping() throws Exception {

Class<?> clazz;
try {
clazz = Context.loadClass("org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping");
clazz = Context.loadClass("org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping");
}
catch (ClassNotFoundException e) {
clazz = Context.loadClass("org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping");
clazz = Context.loadClass("org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping");
}

return (AbstractHandlerMapping) clazz.newInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,10 @@ private Resource newResource(Class<? extends Resource> resourceClass) {
* required parameters
* <strong>Should</strong> return null if given parameters are missing a parameter required by search handlers
* eligible for given resource name and parameters
* <strong>Should</strong> return default search handler if two search handlers match given resource and parameters and no search handler
* id is specified and one of the matching search handlers is the default
* <strong>Should</strong> fail if two search handlers match given resource and parameters and no search handler
* id is specified
* id is specified and neither matching search hander is the default
* <strong>Should</strong> return null if a non special request parameter in given parameters cannot be found in
* any search handler
* <strong>Should</strong> return null if no search handler is found for given resource name
Expand Down Expand Up @@ -568,15 +570,23 @@ public SearchHandler getSearchHandler(String resourceName, Map<String, String[]>
return null;
} else if (candidateSearchHandlers.size() == 1) {
return candidateSearchHandlers.iterator().next();
} else {
List<String> candidateSearchHandlerIds = new ArrayList<String>();
for (SearchHandler candidateSearchHandler : candidateSearchHandlers) {
candidateSearchHandlerIds.add(RestConstants.REQUEST_PROPERTY_FOR_SEARCH_ID + "="
+ candidateSearchHandler.getSearchConfig().getId());
}
// if multiple, return default
for (SearchHandler candidateSearchHandler : candidateSearchHandlers) {
if ("default".equals(candidateSearchHandler.getSearchConfig().getId())) {
return candidateSearchHandler;
}
throw new InvalidSearchException("The search is ambiguous. Please specify "
+ StringUtils.join(candidateSearchHandlerIds, " or "));
}

// multiple and no default, throw exception
List<String> candidateSearchHandlerIds = new ArrayList<String>();
for (SearchHandler candidateSearchHandler : candidateSearchHandlers) {
candidateSearchHandlerIds.add(RestConstants.REQUEST_PROPERTY_FOR_SEARCH_ID + "="
+ candidateSearchHandler.getSearchConfig().getId());
}
throw new InvalidSearchException("The search is ambiguous. Please specify "
+ StringUtils.join(candidateSearchHandlerIds, " or "));

}
}

Expand Down
20 changes: 6 additions & 14 deletions omod-common/src/main/resources/webModuleApplicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@

<bean id="restService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionManager" ref="transactionManager" />
<property name="target">
<bean
class="org.openmrs.module.webservices.rest.web.api.impl.RestServiceImpl">
<property name="restHelperService" ref="restHelperService"></property>
<property name="openmrsClassScanner" ref="openmrsClassScanner"></property>
<property name="restHelperService" ref="restHelperService" />
<property name="openmrsClassScanner" ref="openmrsClassScanner" />
</bean>
</property>
<property name="preInterceptors">
<ref bean="serviceInterceptors" />
</property>
<property name="preInterceptors" ref="serviceInterceptors" />
<property name="transactionAttributeSource">
<bean
class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource" />
Expand All @@ -47,17 +43,13 @@

<bean id="restHelperService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionManager" ref="transactionManager" />
<property name="target">
<bean class="org.openmrs.module.webservices.rest.web.api.impl.RestHelperServiceImpl">
<property name="sessionFactory" ref="dbSessionFactory" />
</bean>
</property>
<property name="preInterceptors">
<ref bean="serviceInterceptors" />
</property>
<property name="preInterceptors" ref="serviceInterceptors" />
<property name="transactionAttributeSource">
<bean
class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ public void getSearchHandler_shouldReturnNullIfGivenParametersAreMissingAParamet
* @see RestServiceImpl#getSearchHandler(String, Map)
*/
@Test
public void getSearchHandler_shouldFailIfTwoSearchHandlersMatchGivenResourceAndParametersAndNoSearchHandlerIdIsSpecified()
public void getSearchHandler_shouldReturnDefaultSearchHandlerIfDefaultAndCustomSearchHandlersMatchGivenResourceAndParametersAndNoSearchHandlerIdIsSpecified()
throws Exception {

SearchHandler searchHandler1 = mock(SearchHandler.class);
Expand All @@ -717,7 +717,34 @@ public void getSearchHandler_shouldFailIfTwoSearchHandlersMatchGivenResourceAndP
Map<String, String[]> parameters = new HashMap<String, String[]>();
parameters.put("source", new String[] { "some name" });
parameters.put("code", new String[] { "some code" });


assertThat(restService.getSearchHandler("v1/concept", parameters), is(searchHandler1));
}

@Test
public void getSearchHandler_shouldFailIfTwoSearchHandlersMatchGivenResourceAndParametersAndNoSearchHandlerIdIsSpecified()
throws Exception {

SearchHandler searchHandler1 = mock(SearchHandler.class);
SearchConfig searchConfig1 = new SearchConfig("conceptByName", "v1/concept", "1.8.*", new SearchQuery.Builder(
"description").withRequiredParameters("source").withOptionalParameters("code").build());
when(searchHandler1.getSearchConfig()).thenReturn(searchConfig1);

SearchHandler searchHandler2 = mock(SearchHandler.class);
SearchConfig searchConfig2 = new SearchConfig("conceptByMapping", "v1/concept", "1.8.*", new SearchQuery.Builder(
"description").withRequiredParameters("source").withOptionalParameters("code").build());
when(searchHandler2.getSearchConfig()).thenReturn(searchConfig2);

setCurrentOpenmrsVersion("1.8.10");

when(restHelperService.getRegisteredSearchHandlers()).thenReturn(asList(searchHandler1, searchHandler2));

RestUtil.disableContext(); //to avoid a Context call

Map<String, String[]> parameters = new HashMap<String, String[]>();
parameters.put("source", new String[] { "some name" });
parameters.put("code", new String[] { "some code" });

expectedException.expect(InvalidSearchException.class);
expectedException.expectMessage("The search is ambiguous. Please specify s=");
restService.getSearchHandler("v1/concept", parameters);
Expand Down
2 changes: 1 addition & 1 deletion omod/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest</artifactId>
<version>2.40.0-SNAPSHOT</version>
<version>2.41.0-SNAPSHOT</version>
</parent>
<artifactId>webservices.rest-omod</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest</artifactId>
<version>2.40.0-SNAPSHOT</version>
<version>2.41.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Rest Web Services</name>
<description>Parent project for the Rest Web Services Module</description>
Expand Down

0 comments on commit 04a6e17

Please sign in to comment.