Skip to content

Commit

Permalink
Merge pull request #2364 from FirelyTeam/feature/small-repairs
Browse files Browse the repository at this point in the history
Small corrections
  • Loading branch information
ewoutkramer authored Jan 19, 2023
2 parents 6340675 + 8b87439 commit dac7ba8
Show file tree
Hide file tree
Showing 30 changed files with 307 additions and 176 deletions.
2 changes: 1 addition & 1 deletion src/Benchmarks/Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.3" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.4" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Hl7.Fhir.Base/Introspection/DeclaredTypeAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Hl7.Fhir.Introspection
/// in the constructor to this attribute.
/// </summary>
[CLSCompliant(false)]
[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
public class DeclaredTypeAttribute : VersionedAttribute
{
public DeclaredTypeAttribute()
Expand Down
22 changes: 11 additions & 11 deletions src/Hl7.Fhir.Base/Introspection/ModelInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Hl7.Fhir.Introspection
/// <summary>
/// A cache of FHIR type mappings found on .NET classes.
/// </summary>
/// <remarks>POCO's in the "common" assemblies
/// <remarks>POCO's in the "base" assembly
/// can reflect the definition of multiple releases of FHIR using <see cref="IFhirVersionDependent"/>
/// attributes. A <see cref="ModelInspector"/> will always capture the metadata for one such
/// <see cref="Specification.FhirRelease" /> which is passed to it in the constructor.
Expand All @@ -41,8 +41,8 @@ public class ModelInspector : IStructureDefinitionSummaryProvider, IModelInfo
/// Calling this function repeatedly for the same type will return the same ClassMapping.
/// </summary>
/// <remarks>If the type given is FHIR Release specific, the returned mapping will contain
/// metadata for that release only. If the type is from the common assembly, it will contain
/// metadata for that type from the most recent release of the common assembly.</remarks>
/// metadata for that release only. If the type is from the base assembly, it will contain
/// metadata for that type from the most recent release of the base assembly.</remarks>
public static ClassMapping? GetClassMappingForType(Type t) =>
ForAssembly(t.GetTypeInfo().Assembly).FindOrImportClassMapping(t);

Expand All @@ -52,8 +52,8 @@ public class ModelInspector : IStructureDefinitionSummaryProvider, IModelInfo
/// the same assembly will return the same inspector.
/// </summary>
/// <remarks>If the assembly given is FHIR Release specific, the returned inspector will contain
/// metadata for that release only. If the assembly is the common assembly, it will contain
/// metadata for the most recent release for those common classes.</remarks>
/// metadata for that release only. If the assembly is the base assembly, it will contain
/// metadata for the most recent release for those base classes.</remarks>
public static ModelInspector ForAssembly(Assembly a)
{
return _inspectedAssemblies.GetOrAdd(a.FullName ?? throw Error.ArgumentNull(nameof(a.FullName)), _ => configureInspector(a));
Expand All @@ -67,10 +67,10 @@ static ModelInspector configureInspector(Assembly a)
var newInspector = new ModelInspector(modelAssemblyAttr.Since);
newInspector.Import(a);

// Make sure we always include the common types too.
var commonAssembly = typeof(Resource).GetTypeInfo().Assembly;
if (a.FullName != commonAssembly.FullName)
newInspector.Import(commonAssembly);
// Make sure we always include the types from the base assembly too.
var baseAssembly = typeof(Resource).GetTypeInfo().Assembly;
if (a.FullName != baseAssembly.FullName)
newInspector.Import(baseAssembly);

// And finally, the System/CQL primitive types
foreach (var cqlType in getCqlTypes())
Expand Down Expand Up @@ -104,9 +104,9 @@ static IEnumerable<Type> getCqlTypes()

/// <summary>
/// Returns a fully configured <see cref="ModelInspector"/> with the
/// FHIR metadata contents of the common assembly
/// FHIR metadata contents of the base assembly
/// </summary>
public static ModelInspector Common => ForType(typeof(ModelInspector));
public static ModelInspector Base => ForType(typeof(ModelInspector));

/// <summary>
/// Constructs a ModelInspector that will reflect the FHIR metadata for the given FHIR release
Expand Down
2 changes: 2 additions & 0 deletions src/Hl7.Fhir.Base/Model/Generated/Attachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ public string Url
/// Number of bytes of content (if url provided)
/// </summary>
[FhirElement("size", InSummary=true, Order=70)]
[DeclaredType(Type = typeof(UnsignedInt), Since = FhirRelease.STU3)]
[DeclaredType(Type = typeof(Integer64), Since = FhirRelease.R5)]
[DataMember]
public Hl7.Fhir.Model.Integer64 SizeElement
{
Expand Down
4 changes: 2 additions & 2 deletions src/Hl7.Fhir.Base/Model/Markdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public partial class Markdown
/// </summary>
public static bool IsValidValue(string value) => FhirString.IsValidValue(value);

public static implicit operator string(Markdown md) => md.Value;
public static implicit operator Markdown(string s) => new(s);
public static implicit operator string?(Markdown? md) => md?.Value;
public static implicit operator Markdown?(string? s) => s is not null ? new(s) : null;

}

Expand Down
2 changes: 1 addition & 1 deletion src/Hl7.Fhir.Base/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System;
using System.Runtime.CompilerServices;

[assembly: FhirModelAssembly]
[assembly: FhirModelAssembly(Since = Hl7.Fhir.Specification.FhirRelease.STU3)]

// The following GUID is for the ID of the typelib if this project is exposed to COM

Expand Down
2 changes: 1 addition & 1 deletion src/Hl7.Fhir.Base/Rest/TransactionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ private string paramValueToString(Parameters.ParameterComponent parameter)
case CodeableConcept codeableConcept:
return codeableConcept.ToToken();
default:
if (ModelInspector.Common.IsPrimitive(parameter.Value.GetType()))
if (ModelInspector.Base.IsPrimitive(parameter.Value.GetType()))
{
return parameter.Value.ToString();
}
Expand Down
27 changes: 7 additions & 20 deletions src/Hl7.Fhir.Base/Serialization/FhirJsonBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private JObject buildInternal(ITypedElement source)
object value = node.Definition != null ? node.Value : details?.OriginalValue ?? node.Value;
var objectInShadow = node.InstanceType != null ? primitiveTypes.Contains(node.InstanceType) : details?.UsesShadow ?? false;

JToken first = value != null ? buildValue(value) : null;
JToken first = value != null ? buildValue(value, node.InstanceType) : null;
JObject second = buildChildren(node);

// If this is a complex type with a value (should not occur)
Expand Down Expand Up @@ -200,25 +200,12 @@ private void addChildren(ITypedElement node, JObject parent)
}
}

private JValue buildValue(object value)
private JValue buildValue(object value, string requiredType = null) => value switch
{
switch (value)
{
case bool b:
case decimal d:
case Int32 i32:
case Int16 i16:
case ulong ul:
case double db:
case BigInteger bi:
case float f:
return new JValue(value);
case string s:
return new JValue(s.Trim());
case long l:
default:
return new JValue(PrimitiveTypeConverter.ConvertTo<string>(value));
}
}
bool or decimal or Int32 or Int16 or ulong or double or BigInteger or float => new JValue(value),
string s => new JValue(s.Trim()),
long l when requiredType is "integer" or "unsignedInt" or "positiveInt" => new JValue(l),
_ => new JValue(PrimitiveTypeConverter.ConvertTo<string>(value)),
};
}
}
7 changes: 6 additions & 1 deletion src/Hl7.Fhir.Base/Serialization/FhirJsonException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Hl7.Fhir.Utility;
using System;
using System.Globalization;
using System.Linq;
using System.Text.Json;

#nullable enable
Expand Down Expand Up @@ -46,6 +45,8 @@ public class FhirJsonException : CodedException
public const string RESOURCETYPE_UNEXPECTED_CODE = "JSON119";
public const string OBJECTS_CANNOT_BE_EMPTY_CODE = "JSON120";
public const string ARRAYS_CANNOT_BE_EMPTY_CODE = "JSON121";
public const string LONG_CANNOT_BE_PARSED_CODE = "JSON122";
public const string LONG_INCORRECT_FORMAT_CODE = "JSON123";

[Obsolete("According to the latest updates of the Json format, primitive arrays of different sizes are no longer considered an error.")]
public const string PRIMITIVE_ARRAYS_INCOMPAT_SIZE_CODE = "JSON122";
Expand Down Expand Up @@ -81,6 +82,10 @@ public class FhirJsonException : CodedException
internal static readonly FhirJsonException NUMBER_CANNOT_BE_PARSED = new(NUMBER_CANNOT_BE_PARSED_CODE, "Json number '{0}' cannot be parsed as a {1}.");
internal static readonly FhirJsonException UNEXPECTED_JSON_TOKEN = new(UNEXPECTED_JSON_TOKEN_CODE, "Expecting a {0}, but found a json {1} with value '{2}'.");

// In R5 Integer64 (long) are serialized as string. So we would expect a string during parsing.
internal static readonly FhirJsonException LONG_CANNOT_BE_PARSED = new(LONG_CANNOT_BE_PARSED_CODE, "Json string '{0}' cannot be parsed as a {1}.");
internal static readonly FhirJsonException LONG_INCORRECT_FORMAT = new(LONG_INCORRECT_FORMAT_CODE, "{0} '{1}' cannot be parsed as a {2}, because it should be a {3}.");

// The parser will turn a non-array value into an array with a single element, so no data is lost.
internal static readonly FhirJsonException EXPECTED_START_OF_ARRAY = new(EXPECTED_START_OF_ARRAY_CODE, "Expected start of array.");

Expand Down
Loading

0 comments on commit dac7ba8

Please sign in to comment.