Skip to content

Commit

Permalink
Merge commit 'cf1e39a0405acac1a1382982dd9bf954d2d17d93' into bugfix/2…
Browse files Browse the repository at this point in the history
…854-fix-validate-code
  • Loading branch information
mmsmits committed Nov 21, 2024
2 parents 23111a4 + cf1e39a commit 413a374
Show file tree
Hide file tree
Showing 19 changed files with 143 additions and 155 deletions.
2 changes: 1 addition & 1 deletion src/Hl7.Fhir.Base/CompatibilitySuppressions.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://learn.microsoft.com/en-us/dotnet/fundamentals/package-validation/diagnostic-ids -->
<!-- https://learn.microsoft.com/dotnet/fundamentals/package-validation/diagnostic-ids -->
<Suppressions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Suppression>
<DiagnosticId>CP0001</DiagnosticId>
Expand Down
2 changes: 1 addition & 1 deletion src/Hl7.Fhir.Base/Hl7.Fhir.Base.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Buffers" Version="4.5.1" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
<PackageReference Include="System.Text.Json" Version="9.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ public static void RemoveAllConstrainedByDiffExtensions<T>(this IEnumerable<T> e
}
}


internal static void RemoveAllNonInheritableExtensions(this Element element)
/// <summary>
/// This extension removes all non-inheritable extensions from the specified element definition and all it's child objects.
/// Non-inheritable extensions are extensions that should not be inherited by derived profiles.
/// </summary>
/// <param name="element"></param>
public static void RemoveAllNonInheritableExtensions(this Element element)
{
if (element == null) { throw Error.ArgumentNull(nameof(element)); }
element.RemoveNonInheritableExtensions();
Expand Down
46 changes: 46 additions & 0 deletions src/Hl7.Fhir.Conformance/CompatibilitySuppressions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://learn.microsoft.com/dotnet/fundamentals/package-validation/diagnostic-ids -->
<Suppressions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Suppression>
<DiagnosticId>CP0001</DiagnosticId>
<Target>T:Hl7.Fhir.Specification.Snapshot.RegenerationSettings</Target>
<Left>lib/net8.0/Hl7.Fhir.Conformance.dll</Left>
<Right>lib/net8.0/Hl7.Fhir.Conformance.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0001</DiagnosticId>
<Target>T:Hl7.Fhir.Specification.Snapshot.RegenerationSettings</Target>
<Left>lib/netstandard2.0/Hl7.Fhir.Conformance.dll</Left>
<Right>lib/netstandard2.0/Hl7.Fhir.Conformance.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Hl7.Fhir.Specification.Snapshot.SnapshotGeneratorSettings.get_RegenerationBehaviour</Target>
<Left>lib/net8.0/Hl7.Fhir.Conformance.dll</Left>
<Right>lib/net8.0/Hl7.Fhir.Conformance.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Hl7.Fhir.Specification.Snapshot.SnapshotGeneratorSettings.set_RegenerationBehaviour(Hl7.Fhir.Specification.Snapshot.RegenerationSettings)</Target>
<Left>lib/net8.0/Hl7.Fhir.Conformance.dll</Left>
<Right>lib/net8.0/Hl7.Fhir.Conformance.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Hl7.Fhir.Specification.Snapshot.SnapshotGeneratorSettings.get_RegenerationBehaviour</Target>
<Left>lib/netstandard2.0/Hl7.Fhir.Conformance.dll</Left>
<Right>lib/netstandard2.0/Hl7.Fhir.Conformance.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Hl7.Fhir.Specification.Snapshot.SnapshotGeneratorSettings.set_RegenerationBehaviour(Hl7.Fhir.Specification.Snapshot.RegenerationSettings)</Target>
<Left>lib/netstandard2.0/Hl7.Fhir.Conformance.dll</Left>
<Right>lib/netstandard2.0/Hl7.Fhir.Conformance.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
</Suppressions>
Original file line number Diff line number Diff line change
Expand Up @@ -2224,17 +2224,9 @@ private async Tasks.Task<bool> ensureSnapshot(StructureDefinition sd, string pro

try
{
var shouldGenerate = _settings.RegenerationBehaviour switch
{
RegenerationSettings.TRY_USE_EXISTING => !sd.HasSnapshot,
RegenerationSettings.REGENERATE_ONCE => !sd.HasSnapshot || !sd.Snapshot.IsCreatedBySnapshotGenerator(),
#pragma warning disable CS0618 // Type or member is obsolete
RegenerationSettings.FORCE_REGENERATE => true, // possible infinite recursion
#pragma warning restore CS0618 // Type or member is obsolete
_ => throw new InvalidOperationException($"Invalid RegenerationSettings value {_settings.RegenerationBehaviour}")
};

if (_settings.GenerateSnapshotForExternalProfiles && shouldGenerate)
if (_settings.GenerateSnapshotForExternalProfiles
&& (!sd.HasSnapshot || (_settings.ForceRegenerateSnapshots && !sd.Snapshot.IsCreatedBySnapshotGenerator()))
)
{
// Automatically expand external profiles on demand
// Debug.Print($"[{nameof(SnapshotGenerator)}.{nameof(ensureSnapshot)}] Recursively generate snapshot for type profile with url: '{sd.Url}' ...");
Expand Down Expand Up @@ -2316,18 +2308,9 @@ private async Tasks.Task<ElementDefinition> getSnapshotRootElement(StructureDefi
var cachedRoot = sd.GetSnapshotRootElementAnnotation();
if (cachedRoot != null) { return cachedRoot; }
#endif
var hasValidRoot = _settings.RegenerationBehaviour switch
{
RegenerationSettings.TRY_USE_EXISTING => sd.HasSnapshot,
RegenerationSettings.REGENERATE_ONCE => sd.HasSnapshot && sd.Snapshot.IsCreatedBySnapshotGenerator(),
#pragma warning disable CS0618 // Type or member is obsolete
RegenerationSettings.FORCE_REGENERATE => false,
#pragma warning restore CS0618 // Type or member is obsolete
_ => throw new InvalidOperationException($"Invalid RegenerationSettings value {_settings.RegenerationBehaviour}")
};


// 2. Return root element definition from existing (pre-generated) snapshot, if it exists
if (hasValidRoot)
if (sd.HasSnapshot && (sd.Snapshot.IsCreatedBySnapshotGenerator() || !_settings.ForceRegenerateSnapshots))
{
// Debug.Print($"[{nameof(SnapshotGenerator)}.{nameof(getSnapshotRootElement)}] {nameof(profileUri)} = '{profileUri}' - use existing root element definition from snapshot: #{sd.Snapshot.Element[0].GetHashCode()}");
// No need to save root ElemDef annotation, as the snapshot has already been fully expanded
Expand Down
46 changes: 46 additions & 0 deletions src/Hl7.Fhir.STU3/CompatibilitySuppressions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://learn.microsoft.com/dotnet/fundamentals/package-validation/diagnostic-ids -->
<Suppressions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Suppression>
<DiagnosticId>CP0001</DiagnosticId>
<Target>T:Hl7.Fhir.Specification.Snapshot.RegenerationSettings</Target>
<Left>lib/net8.0/Hl7.Fhir.STU3.dll</Left>
<Right>lib/net8.0/Hl7.Fhir.STU3.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0001</DiagnosticId>
<Target>T:Hl7.Fhir.Specification.Snapshot.RegenerationSettings</Target>
<Left>lib/netstandard2.0/Hl7.Fhir.STU3.dll</Left>
<Right>lib/netstandard2.0/Hl7.Fhir.STU3.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Hl7.Fhir.Specification.Snapshot.SnapshotGeneratorSettings.get_RegenerationBehaviour</Target>
<Left>lib/net8.0/Hl7.Fhir.STU3.dll</Left>
<Right>lib/net8.0/Hl7.Fhir.STU3.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Hl7.Fhir.Specification.Snapshot.SnapshotGeneratorSettings.set_RegenerationBehaviour(Hl7.Fhir.Specification.Snapshot.RegenerationSettings)</Target>
<Left>lib/net8.0/Hl7.Fhir.STU3.dll</Left>
<Right>lib/net8.0/Hl7.Fhir.STU3.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Hl7.Fhir.Specification.Snapshot.SnapshotGeneratorSettings.get_RegenerationBehaviour</Target>
<Left>lib/netstandard2.0/Hl7.Fhir.STU3.dll</Left>
<Right>lib/netstandard2.0/Hl7.Fhir.STU3.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Hl7.Fhir.Specification.Snapshot.SnapshotGeneratorSettings.set_RegenerationBehaviour(Hl7.Fhir.Specification.Snapshot.RegenerationSettings)</Target>
<Left>lib/netstandard2.0/Hl7.Fhir.STU3.dll</Left>
<Right>lib/netstandard2.0/Hl7.Fhir.STU3.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
</Suppressions>
27 changes: 5 additions & 22 deletions src/Hl7.Fhir.STU3/Specification/Snapshot/SnapshotGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1941,17 +1941,9 @@ private async Tasks.Task<bool> ensureSnapshot(StructureDefinition sd, string pro

try
{
var shouldGenerate = _settings.RegenerationBehaviour switch
{
RegenerationSettings.TRY_USE_EXISTING => !sd.HasSnapshot,
RegenerationSettings.REGENERATE_ONCE => !sd.HasSnapshot || !sd.Snapshot.IsCreatedBySnapshotGenerator(),
#pragma warning disable CS0618 // Type or member is obsolete
RegenerationSettings.FORCE_REGENERATE => true,
#pragma warning restore CS0618 // Type or member is obsolete
_ => throw new InvalidOperationException($"Invalid RegenerationSettings value {_settings.RegenerationBehaviour}")
};

if (_settings.GenerateSnapshotForExternalProfiles && shouldGenerate)
if (_settings.GenerateSnapshotForExternalProfiles
&& (!sd.HasSnapshot || (_settings.ForceRegenerateSnapshots && !sd.Snapshot.IsCreatedBySnapshotGenerator()))
)
{
// Automatically expand external profiles on demand
// Debug.Print($"[{nameof(SnapshotGenerator)}.{nameof(ensureSnapshot)}] Recursively generate snapshot for type profile with url: '{sd.Url}' ...");
Expand Down Expand Up @@ -2033,18 +2025,9 @@ private async Tasks.Task<ElementDefinition> getSnapshotRootElement(StructureDefi
var cachedRoot = sd.GetSnapshotRootElementAnnotation();
if (cachedRoot != null) { return cachedRoot; }
#endif
var hasValidRoot = _settings.RegenerationBehaviour switch
{
RegenerationSettings.TRY_USE_EXISTING => sd.HasSnapshot,
RegenerationSettings.REGENERATE_ONCE => sd.HasSnapshot && sd.Snapshot.IsCreatedBySnapshotGenerator(),
#pragma warning disable CS0618 // Type or member is obsolete
RegenerationSettings.FORCE_REGENERATE => false,
#pragma warning restore CS0618 // Type or member is obsolete
_ => throw new InvalidOperationException($"Invalid RegenerationSettings value {_settings.RegenerationBehaviour}")
};


// 2. Return root element definition from existing (pre-generated) snapshot, if it exists
if (hasValidRoot)
if (sd.HasSnapshot && (sd.Snapshot.IsCreatedBySnapshotGenerator() || !_settings.ForceRegenerateSnapshots))
{
// Debug.Print($"[{nameof(SnapshotGenerator)}.{nameof(getSnapshotRootElement)}] {nameof(profileUri)} = '{profileUri}' - use existing root element definition from snapshot: #{sd.Snapshot.Element[0].GetHashCode()}");
// No need to save root ElemDef annotation, as the snapshot has already been fully expanded
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright (c) 2017, Firely ([email protected]) and contributors
* See the file CONTRIBUTORS for details.
*
Expand All @@ -7,7 +7,6 @@
*/

using Hl7.Fhir.Utility;
using System;

namespace Hl7.Fhir.Specification.Snapshot
{
Expand Down Expand Up @@ -35,7 +34,7 @@ public void CopyTo(SnapshotGeneratorSettings other)
{
if (other == null) { throw Error.ArgumentNull(nameof(other)); }
other.GenerateSnapshotForExternalProfiles = GenerateSnapshotForExternalProfiles;
other.RegenerationBehaviour = RegenerationBehaviour;
other.ForceRegenerateSnapshots = ForceRegenerateSnapshots;
other.GenerateExtensionsOnConstraints = GenerateExtensionsOnConstraints;
other.GenerateAnnotationsOnConstraints = GenerateAnnotationsOnConstraints;
other.GenerateElementIds = GenerateElementIds;
Expand All @@ -56,18 +55,7 @@ public void CopyTo(SnapshotGeneratorSettings other)
/// Re-generated snapshots are annotated to prevent duplicate re-generation (assuming the provided resource resolver uses caching).
/// If disabled (default), then the snapshot generator relies on existing snapshot components, if they exist.
/// </summary>
[Obsolete(
"This setting does not work as intended. We will maintain the old behaviour for now, and we will consider removing it in a future major release. Use the new RegenerationBehaviour setting instead. See also https://github.com/FirelyTeam/firely-net-sdk/pull/2803")]
public bool ForceRegenerateSnapshots
{
get { return this.RegenerationBehaviour == RegenerationSettings.REGENERATE_ONCE; }
set { this.RegenerationBehaviour = value ? RegenerationSettings.REGENERATE_ONCE : RegenerationSettings.TRY_USE_EXISTING; }
} // ForceExpandAll

/// <summary>
/// Setting for the regeneration behaviour of the snapshot generator. see <see cref="RegenerationSettings"/>.
/// </summary>
public RegenerationSettings RegenerationBehaviour { get; set; }
public bool ForceRegenerateSnapshots { get; set; } = false; // ForceExpandAll

/// <summary>
/// Enable this setting to add a custom <see cref="SnapshotGeneratorExtensions.CONSTRAINED_BY_DIFF_EXT"/> extension
Expand Down Expand Up @@ -96,24 +84,4 @@ public bool ForceRegenerateSnapshots
// <remarks>See GForge #9791</remarks>
// public bool MergeTypeProfiles { get; set; }
}

/// <summary>
/// Settings for defining the behaviour of the snapshot generator with respect to regenerating snapshots.
/// </summary>
public enum RegenerationSettings
{
/// <summary>
/// Try to use an existing snapshot, if available.
/// </summary>
TRY_USE_EXISTING,
/// <summary>
/// Regenerate the snapshot once, to ensure it is up-to-date.
/// </summary>
REGENERATE_ONCE,
/// <summary>
/// Regenerate the snapshot every time. This is useful for debugging and testing purposes.
/// </summary>
[Obsolete("Watch out when using this setting! it could lead to infinite recursion and is mainly meant for debugging and testing purposes. If you previously had ForceRegenerateSnapshots set to true, consider using REGENERATE_ONCE instead.")]
FORCE_REGENERATE,
}
}
22 changes: 5 additions & 17 deletions src/Hl7.Fhir.Shims.Base/Specification/Source/SnapshotSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ public SnapshotSource(ISyncOrAsyncResourceResolver source, bool regenerate)
private static SnapshotGeneratorSettings createSettings(bool regenerate)
{
var settings = SnapshotGeneratorSettings.CreateDefault();
#pragma warning disable CS0618 // Type or member is obsolete
settings.ForceRegenerateSnapshots = regenerate;
#pragma warning restore CS0618 // Type or member is obsolete
return settings;
}

Expand All @@ -72,19 +70,19 @@ public SnapshotSource(ISyncOrAsyncResourceResolver source)

#region IResourceResolver

private IAsyncResourceResolver Resolver => Generator.AsyncResolver;
private IAsyncResourceResolver _resolver => Generator.AsyncResolver;

/// <summary>Find a resource based on its relative or absolute uri.</summary>
/// <remarks>The source ensures that resolved <see cref="StructureDefinition"/> instances have a snapshot component.</remarks>
public async Tasks.Task<Resource> ResolveByUriAsync(string uri) => await ensureSnapshot(await Resolver.ResolveByUriAsync(uri).ConfigureAwait(false)).ConfigureAwait(false);
public async Tasks.Task<Resource> ResolveByUriAsync(string uri) => await ensureSnapshot(await _resolver.ResolveByUriAsync(uri).ConfigureAwait(false)).ConfigureAwait(false);

/// <inheritdoc cref="ResolveByUriAsync(string)"/>
[Obsolete("SnapshotSource now works best with asynchronous resolvers. Use ResolveByUriAsync() instead.")]
public Resource ResolveByUri(string uri) => TaskHelper.Await(() => ResolveByUriAsync(uri));

/// <summary>Find a (conformance) resource based on its canonical uri.</summary>
/// <remarks>The source ensures that resolved <see cref="StructureDefinition"/> instances have a snapshot component.</remarks>
public async Tasks.Task<Resource> ResolveByCanonicalUriAsync(string uri) => await ensureSnapshot(await Resolver.ResolveByCanonicalUriAsync(uri).ConfigureAwait(false)).ConfigureAwait(false);
public async Tasks.Task<Resource> ResolveByCanonicalUriAsync(string uri) => await ensureSnapshot(await _resolver.ResolveByCanonicalUriAsync(uri).ConfigureAwait(false)).ConfigureAwait(false);

/// <inheritdoc cref="ResolveByCanonicalUriAsync(string)"/>
[Obsolete("SnapshotSource now works best with asynchronous resolvers. Use ResolveByCanonicalUriAsync() instead.")]
Expand All @@ -98,17 +96,7 @@ private async Tasks.Task<Resource> ensureSnapshot(Resource res)
{
if (res is StructureDefinition sd)
{
var shouldGenerate = Generator.Settings.RegenerationBehaviour switch
{
RegenerationSettings.TRY_USE_EXISTING => !sd.HasSnapshot,
RegenerationSettings.REGENERATE_ONCE => !sd.HasSnapshot || !sd.Snapshot.IsCreatedBySnapshotGenerator(),
#pragma warning disable CS0618 // Type or member is obsolete
RegenerationSettings.FORCE_REGENERATE => true,
#pragma warning restore CS0618 // Type or member is obsolete
_ => throw Error.NotSupported($"Unknown regeneration behaviour: {Generator.Settings.RegenerationBehaviour}")
};

if (shouldGenerate)
if (!sd.HasSnapshot || Generator.Settings.ForceRegenerateSnapshots || !sd.Snapshot.IsCreatedBySnapshotGenerator())
{
await Generator.UpdateAsync(sd).ConfigureAwait(false);
}
Expand All @@ -119,6 +107,6 @@ private async Tasks.Task<Resource> ensureSnapshot(Resource res)
// Allow derived classes to override
// http://blogs.msdn.com/b/jaredpar/archive/2011/03/18/debuggerdisplay-attribute-best-practices.aspx
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal protected virtual string DebuggerDisplay => $"{GetType().Name} for {Resolver.DebuggerDisplayString()}";
internal protected virtual string DebuggerDisplay => $"{GetType().Name} for {_resolver.DebuggerDisplayString()}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Threading.Tasks.Dataflow" Version="8.0.1" />
<PackageReference Include="System.Threading.Tasks.Dataflow" Version="9.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Threading.Tasks.Dataflow" Version="8.0.1" />
<PackageReference Include="System.Threading.Tasks.Dataflow" Version="9.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 413a374

Please sign in to comment.