Skip to content

Commit

Permalink
feat(op): add support for checkParameters method...
Browse files Browse the repository at this point in the history
...that searches for unknown/unrecognized parameters
  • Loading branch information
cmark committed Oct 14, 2024
1 parent 0b81744 commit 410b16c
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
*/
package com.b2international.fhir.r4.operations;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.*;
import java.util.function.Function;

import org.hl7.fhir.exceptions.FHIRFormatError;
import org.hl7.fhir.r4.model.Parameters;
import org.hl7.fhir.r4.model.PrimitiveType;
import org.hl7.fhir.r4.model.Type;

import com.google.common.collect.ImmutableSortedSet;

/**
* @since 0.1
*/
Expand Down Expand Up @@ -83,4 +84,32 @@ public boolean equals(Object obj) {
return this.parameters.equalsDeep(other.getParameters());
}

/**
* @throws FHIRFormatError - if there are unknown/unrecognized parameters specified
*/
public final void checkParameters() {
Set<String> acceptedParameterNames = getAcceptedParameterNames();

if (acceptedParameterNames == null || acceptedParameterNames.isEmpty()) {
return;
}

var unsupportedParameters = this.parameters.getParameter().stream()
.map(Parameters.ParametersParameterComponent::getName)
.filter(parameterName -> !acceptedParameterNames.contains(parameterName))
.collect(ImmutableSortedSet.toImmutableSortedSet(String::compareTo));

if (!unsupportedParameters.isEmpty()) {
throw new FHIRFormatError(String.format("Unknown/Unsupported parameters found in the request '%s'. Accepted parameters are: %s.", unsupportedParameters, acceptedParameterNames));
}
}

/**
* Subclasses may optionally override this method to provide support for parameter validation via the {@link #checkParameters(boolean)} method.
* @return
*/
protected SortedSet<String> getAcceptedParameterNames() {
return ImmutableSortedSet.of();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;

import org.hl7.fhir.r4.model.*;
import org.hl7.fhir.r4.model.Parameters.ParametersParameterComponent;

import com.google.common.collect.ImmutableSortedSet;

/**
* @since 0.1
*/
Expand All @@ -38,6 +41,24 @@ public final class CodeSystemLookupParameters extends BaseParameters {
//how to represent LANG.X here, just lang or lang.*?
public static final Set<String> OFFICIAL_R5_PROPERTY_VALUES = Set.of(PROPERTY_SYSTEM, PROPERTY_NAME, PROPERTY_VERSION, PROPERTY_DISPLAY, PROPERTY_DESIGNATION, PROPERTY_PARENT, PROPERTY_CHILD);

private static final String PARAM_CODE = "code";
private static final String PARAM_SYSTEM = "system";
private static final String PARAM_VERSION = "version";
private static final String PARAM_CODING = "coding";
private static final String PARAM_DATE = "date";
private static final String PARAM_DISPLAY_LANGUAGE = "displayLanguage";
private static final String PARAM_PROPERTY = "property";

private static final SortedSet<String> ACCEPTED_PARAMETER_NAMES = ImmutableSortedSet.of(
PARAM_CODE,
PARAM_SYSTEM,
PARAM_VERSION,
PARAM_CODING,
PARAM_DATE,
PARAM_DISPLAY_LANGUAGE,
PARAM_PROPERTY
);

public CodeSystemLookupParameters() {
this(new Parameters());
}
Expand All @@ -47,39 +68,39 @@ public CodeSystemLookupParameters(Parameters parameters) {
}

public CodeType getCode() {
return (CodeType) getParameterValue("code", Parameters.ParametersParameterComponent::getValue);
return (CodeType) getParameterValue(PARAM_CODE, Parameters.ParametersParameterComponent::getValue);
}

public UriType getSystem() {
return (UriType) getParameterValue("system", Parameters.ParametersParameterComponent::getValue);
return (UriType) getParameterValue(PARAM_SYSTEM, Parameters.ParametersParameterComponent::getValue);
}

public StringType getVersion() {
return (StringType) getParameterValue("version", Parameters.ParametersParameterComponent::getValue);
return (StringType) getParameterValue(PARAM_VERSION, Parameters.ParametersParameterComponent::getValue);
}

public Coding getCoding() {
return (Coding) getParameterValue("coding", Parameters.ParametersParameterComponent::getValue);
return (Coding) getParameterValue(PARAM_CODING, Parameters.ParametersParameterComponent::getValue);
}

public DateTimeType getDate() {
return (DateTimeType) getParameterValue("date", Parameters.ParametersParameterComponent::getValue);
return (DateTimeType) getParameterValue(PARAM_DATE, Parameters.ParametersParameterComponent::getValue);
}

public CodeType getDisplayLanguage() {
return (CodeType) getParameterValue("displayLanguage", Parameters.ParametersParameterComponent::getValue);
return (CodeType) getParameterValue(PARAM_DISPLAY_LANGUAGE, Parameters.ParametersParameterComponent::getValue);
}

public List<CodeType> getProperty() {
return getParameters("property").stream().map(ParametersParameterComponent::getValue).map(t -> (CodeType) t).toList();
return getParameters(PARAM_PROPERTY).stream().map(ParametersParameterComponent::getValue).map(t -> (CodeType) t).toList();
}

public CodeSystemLookupParameters setCode(String code) {
return setCode(new CodeType(code));
}

public CodeSystemLookupParameters setCode(CodeType code) {
getParameters().addParameter("code", code);
getParameters().addParameter(PARAM_CODE, code);
return this;
}

Expand All @@ -88,7 +109,7 @@ public CodeSystemLookupParameters setSystem(String system) {
}

public CodeSystemLookupParameters setSystem(UriType system) {
getParameters().addParameter("system", system);
getParameters().addParameter(PARAM_SYSTEM, system);
return this;
}

Expand All @@ -97,12 +118,12 @@ public CodeSystemLookupParameters setVersion(String version) {
}

public CodeSystemLookupParameters setVersion(StringType version) {
getParameters().addParameter("version", version);
getParameters().addParameter(PARAM_VERSION, version);
return this;
}

public CodeSystemLookupParameters setCoding(Coding coding) {
getParameters().addParameter("coding", coding);
getParameters().addParameter(PARAM_CODING, coding);
return this;
}

Expand All @@ -115,7 +136,7 @@ public CodeSystemLookupParameters setDate(Date date) {
}

public CodeSystemLookupParameters setDate(DateTimeType date) {
getParameters().addParameter("date", date);
getParameters().addParameter(PARAM_DATE, date);
return this;
}

Expand All @@ -124,7 +145,7 @@ public CodeSystemLookupParameters setDisplayLanguage(String displayLanguage) {
}

public CodeSystemLookupParameters setDisplayLanguage(CodeType displayLanguage) {
getParameters().addParameter("displayLanguage", displayLanguage);
getParameters().addParameter(PARAM_DISPLAY_LANGUAGE, displayLanguage);
return this;
}

Expand All @@ -133,17 +154,17 @@ public CodeSystemLookupParameters setDisplayLanguage(CodeType displayLanguage) {
* @return the list of actual values instead of a list with wrapped {@link StringType} instances.
*/
public List<String> getPropertyValues() {
return getParameters("property").stream().map(ParametersParameterComponent::getValue).map(t -> ((StringType) t).getValueNotNull()).toList();
return getParameters(PARAM_PROPERTY).stream().map(ParametersParameterComponent::getValue).map(t -> ((StringType) t).getValueNotNull()).toList();
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public CodeSystemLookupParameters setProperty(List propertyValues) {
var list = propertyValues == null ? List.of() : propertyValues;
list.forEach(propertyValue -> {
if (propertyValue instanceof StringType) {
getParameters().addParameter("property", (StringType) propertyValue);
getParameters().addParameter(PARAM_PROPERTY, (StringType) propertyValue);
} else if (propertyValue instanceof String) {
getParameters().addParameter("property", new StringType((String) propertyValue));
getParameters().addParameter(PARAM_PROPERTY, new StringType((String) propertyValue));
} else {
throw new UnsupportedOperationException();
// throw new BadRequestException(String.format("Value type '%s' is not supported in property values. Need to be String or StringType.", propertyValue));
Expand All @@ -153,7 +174,12 @@ public CodeSystemLookupParameters setProperty(List propertyValues) {
}

public boolean isPropertyRequested(String propertyValue) {
return hasParameterWithValue("property", Parameters.ParametersParameterComponent::getValue, propertyValue);
return hasParameterWithValue(PARAM_PROPERTY, Parameters.ParametersParameterComponent::getValue, propertyValue);
}

@Override
protected SortedSet<String> getAcceptedParameterNames() {
return ACCEPTED_PARAMETER_NAMES;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
*/
package com.b2international.fhir.r4b.operations;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.*;
import java.util.function.Function;

import org.hl7.fhir.exceptions.FHIRFormatError;
import org.hl7.fhir.r4b.model.DataType;
import org.hl7.fhir.r4b.model.Parameters;
import org.hl7.fhir.r4b.model.PrimitiveType;

import com.google.common.collect.ImmutableSortedSet;

/**
* @since 0.1
*/
Expand Down Expand Up @@ -82,4 +83,32 @@ public boolean equals(Object obj) {
BaseParameters other = (BaseParameters) obj;
return this.parameters.equalsDeep(other.getParameters());
}

/**
* @throws FHIRFormatError - if there are unknown/unrecognized parameters specified
*/
public final void checkParameters() {
Set<String> acceptedParameterNames = getAcceptedParameterNames();

if (acceptedParameterNames == null || acceptedParameterNames.isEmpty()) {
return;
}

var unsupportedParameters = this.parameters.getParameter().stream()
.map(Parameters.ParametersParameterComponent::getName)
.filter(parameterName -> !acceptedParameterNames.contains(parameterName))
.collect(ImmutableSortedSet.toImmutableSortedSet(String::compareTo));

if (!unsupportedParameters.isEmpty()) {
throw new FHIRFormatError(String.format("Unknown/Unsupported parameters found in the request '%s'. Accepted parameters are: %s.", unsupportedParameters, acceptedParameterNames));
}
}

/**
* Subclasses may optionally override this method to provide support for parameter validation via the {@link #checkParameters(boolean)} method.
* @return
*/
protected SortedSet<String> getAcceptedParameterNames() {
return ImmutableSortedSet.of();
}
}
Loading

0 comments on commit 410b16c

Please sign in to comment.