Prevent profile generation issue with multiple resource definition

classes that share an extension
This commit is contained in:
James Agnew 2014-12-10 10:40:29 -05:00
parent 84ecec2835
commit 8ac479b7bd
2 changed files with 74 additions and 59 deletions

View File

@ -33,6 +33,7 @@ import java.util.Map;
import ca.uhn.fhir.context.*;
import ca.uhn.fhir.model.api.ICompositeDatatype;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.WordUtils;
@ -57,7 +58,7 @@ import ca.uhn.fhir.rest.server.provider.ServerProfileProvider;
public class FhirDstu1 implements IFhirVersion {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirDstu1.class);
private Map<RuntimeChildDeclaredExtensionDefinition, String> myExtensionDefToCode = new HashMap<RuntimeChildDeclaredExtensionDefinition, String>();
// private Map<RuntimeChildDeclaredExtensionDefinition, String> myExtensionDefToCode = new HashMap<RuntimeChildDeclaredExtensionDefinition, String>();
private String myId;
@Override
@ -158,7 +159,7 @@ public class FhirDstu1 implements IFhirVersion {
String expectedPath = StringUtils.join(path, '.');
ourLog.info("Filling profile for: {} - Path: {}", expectedPath);
ourLog.debug("Filling profile for: {} - Path: {}", expectedPath);
String name = def.getName();
if (!expectedPath.equals(name)) {
path.pollLast();
@ -267,11 +268,12 @@ public class FhirDstu1 implements IFhirVersion {
return retVal;
}
private void scanForExtensions(Profile theProfile, BaseRuntimeElementDefinition<?> def) {
private Map<RuntimeChildDeclaredExtensionDefinition, String> scanForExtensions(Profile theProfile, BaseRuntimeElementDefinition<?> def) {
BaseRuntimeElementCompositeDefinition<?> cdef = ((BaseRuntimeElementCompositeDefinition<?>) def);
Map<RuntimeChildDeclaredExtensionDefinition, String> extensionDefToCode = new HashMap<RuntimeChildDeclaredExtensionDefinition, String>();
for (RuntimeChildDeclaredExtensionDefinition nextChild : cdef.getExtensions()) {
if (myExtensionDefToCode.containsKey(nextChild)) {
if (extensionDefToCode.containsKey(nextChild)) {
continue;
}
@ -288,10 +290,10 @@ public class FhirDstu1 implements IFhirVersion {
}
defn.setCode(code);
if (myExtensionDefToCode.values().contains(code)) {
if (extensionDefToCode.values().contains(code)) {
throw new IllegalStateException("Duplicate extension code: " + code);
}
myExtensionDefToCode.put(nextChild, code);
extensionDefToCode.put(nextChild, code);
if (nextChild.getChildType() != null && IPrimitiveDatatype.class.isAssignableFrom(nextChild.getChildType())) {
RuntimePrimitiveDatatypeDefinition pdef = (RuntimePrimitiveDatatypeDefinition) nextChild.getSingleChildOrThrow();
@ -306,11 +308,13 @@ public class FhirDstu1 implements IFhirVersion {
for (RuntimeChildDeclaredExtensionDefinition nextChildExt : pdef.getExtensions()) {
StructureElementDefinitionType type = defn.getDefinition().addType();
type.setCode(DataTypeEnum.EXTENSION);
type.setProfile("#" + myExtensionDefToCode.get(nextChildExt));
type.setProfile("#" + extensionDefToCode.get(nextChildExt));
}
}
}
return extensionDefToCode;
}
@Override

View File

@ -1,71 +1,82 @@
package ca.uhn.fhir.context;
import ca.uhn.fhir.model.api.annotation.*;
import junit.framework.TestCase;
import org.junit.Test;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.api.annotation.Child;
import ca.uhn.fhir.model.api.annotation.Description;
import ca.uhn.fhir.model.api.annotation.Extension;
import ca.uhn.fhir.model.api.annotation.ProvidesResources;
import ca.uhn.fhir.model.api.annotation.ResourceDef;
import ca.uhn.fhir.model.dstu.resource.Observation;
import ca.uhn.fhir.model.dstu.resource.Patient;
import ca.uhn.fhir.model.api.annotation.Extension;
import ca.uhn.fhir.model.dstu.resource.Profile;
import ca.uhn.fhir.model.primitive.StringDt;
import junit.framework.TestCase;
import org.junit.Ignore;
import org.junit.Test;
/**
* Created by Bill de Beaubien on 12/10/2014.
*/
public class DuplicateExtensionTest extends TestCase {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(DuplicateExtensionTest.class);
@Test
public void testScannerShouldAddProvidedResources() {
// FhirContext ctx = new FhirContext();
// RuntimeResourceDefinition patientDef = ctx.getResourceDefinition(CustomPatient.class);
// patientDef.toProfile();
//
// RuntimeResourceDefinition observationDef = ctx.getResourceDefinition(CustomObservation.class);
// observationDef.toProfile();
FhirContext ctx = new FhirContext();
RuntimeResourceDefinition patientDef = ctx.getResourceDefinition(CustomPatient.class);
Profile profile = (Profile) patientDef.toProfile();
String res = ctx.newJsonParser().setPrettyPrint(true).encodeResourceToString(profile);
ourLog.info(res);
RuntimeResourceDefinition observationDef = ctx.getResourceDefinition(CustomObservation.class);
profile = (Profile) observationDef.toProfile();
}
@ResourceDef(name = "Observation", id = "CustomObservation")
class CustomObservation extends Observation {
@Child(name = "idExt", order = 0)
@Extension(url = "http://foo.org#id", definedLocally = true, isModifier = false)
@Description(shortDefinition = "Contains the id of the resource")
private StringDt myIdExt;
public StringDt getIdExt() {
if (myIdExt == null) {
myIdExt = new StringDt();
}
return myIdExt;
}
public void setIdExt(StringDt theIdExt) {
myIdExt = theIdExt;
}
}
@ProvidesResources(resources = CustomObservation.class)
class CustomObservationProvider {
}
@ResourceDef(name = "Patient", id = "CustomPatient")
class CustomPatient extends Patient {
@Child(name = "idExt", order = 0)
@Extension(url = "http://foo.org#id", definedLocally = true, isModifier = false)
@Description(shortDefinition = "Contains the id of the resource")
private StringDt myIdExt;
public StringDt getIdExt() {
if (myIdExt == null) {
myIdExt = new StringDt();
}
return myIdExt;
}
public void setIdExt(StringDt theIdExt) {
myIdExt = theIdExt;
}
}
@ProvidesResources(resources = CustomPatient.class)
class CustomPatientProvider {
}
@ResourceDef(name = "Patient", id = "CustomPatient")
class CustomPatient extends Patient {
@Child(name="idExt", order = 0)
@Extension(url= "http://foo.org#id", definedLocally=true, isModifier=false)
@Description(shortDefinition = "Contains the id of the resource")
private StringDt myIdExt;
public StringDt getIdExt() {
if (myIdExt == null) {
myIdExt = new StringDt();
}
return myIdExt;
}
public void setIdExt(StringDt theIdExt) {
myIdExt = theIdExt;
}
}
@ResourceDef(name = "Observation", id = "CustomObservation")
class CustomObservation extends Observation {
@Child(name="idExt", order = 0)
@Extension(url= "http://foo.org#id", definedLocally=true, isModifier=false)
@Description(shortDefinition = "Contains the id of the resource")
private StringDt myIdExt;
public StringDt getIdExt() {
if (myIdExt == null) {
myIdExt = new StringDt();
}
return myIdExt;
}
public void setIdExt(StringDt theIdExt) {
myIdExt = theIdExt;
}
}
}