Skip to content
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public void VerifyMultiplicityAndNullability()
{
Assert.That(() => ((IOpenApiSchema)null).QueryIsCollection(), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => ((IOpenApiSchema)null).QueryIsNullable(true), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => ((IOpenApiSchema)null).QueryAdmitsNull(), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => ((IOpenApiSchema)null).QueryIsValueType(), Throws.TypeOf<ArgumentNullException>());

using (Assert.EnterMultipleScope())
Expand All @@ -147,6 +148,10 @@ public void VerifyMultiplicityAndNullability()
Assert.That(this.schemas["Branch"].Properties["head"].QueryIsNullable(true), Is.True);
Assert.That(this.schemas["ProjectRequest"].Properties["defaultBranch"].QueryIsNullable(false), Is.True);

Assert.That(this.schemas["Branch"].Properties["head"].QueryAdmitsNull(), Is.True);
Assert.That(this.schemas["ProjectRequest"].Properties["defaultBranch"].QueryAdmitsNull(), Is.False);
Assert.That(this.schemas["ExternalDataRequest"].Properties["resourceIdentifier"].QueryAdmitsNull(), Is.False);

Assert.That(this.schemas["Branch"].Properties["created"].QueryIsValueType(), Is.True);
Assert.That(this.schemas["Branch"].Properties["owningProject"].QueryIsValueType(), Is.True);
Assert.That(this.schemas["PrimitiveConstraint"].Properties["inverse"].QueryIsValueType(), Is.True);
Expand Down
15 changes: 11 additions & 4 deletions SysML2.NET.CodeGenerator/Extensions/OpenApiSchemaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,17 @@ public static bool QueryIsNullable(this IOpenApiSchema schema, bool isRequired)
{
ArgumentNullException.ThrowIfNull(schema);

if (!isRequired)
{
return true;
}
return !isRequired || schema.QueryAdmitsNull();
}

/// <summary>
/// Queries whether the schema accepts an explicit JSON <c>null</c> value
/// </summary>
/// <param name="schema">The subject <see cref="IOpenApiSchema"/>.</param>
/// <returns>True when the schema has a <c>oneOf</c> branch of type <c>null</c>.</returns>
public static bool QueryAdmitsNull(this IOpenApiSchema schema)
{
ArgumentNullException.ThrowIfNull(schema);

return schema is not OpenApiSchemaReference
&& schema.OneOf?.Any(branch => branch is not OpenApiSchemaReference && branch.Type == JsonSchemaType.Null) == true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ public static void RegisterJsonSerializerHelper(this IHandlebars handlebars, Fun

return propertySchema.QueryIsValueType() && propertySchema.QueryIsNullable(arguments.QueryIsRequired());
});

handlebars.RegisterHelper("Property.QueryAdmitsNull", (_, arguments) =>
arguments.QueryPropertySchema().QueryAdmitsNull());

handlebars.RegisterHelper("Property.QueryIsOmittedWhenUnset", (_, arguments) =>
!arguments.QueryIsRequired() && !arguments.QueryPropertySchema().QueryAdmitsNull());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,12 @@ namespace SysML2.NET.Serializer.Json.PSM
writer.WriteString("@id"u8, {{#Property.WriteAccessor propertyName ../this.Key ../this.Value}}.Value);
writer.WriteEndObject();
}
{{#if (Property.QueryAdmitsNull propertyName ../this.Key ../this.Value)}}
else
{
writer.WriteNull("{{propertyName}}"u8);
}
{{/if}}
{{else}}
writer.WriteStartObject("{{propertyName}}"u8);
writer.WriteString("@id"u8, {{#Property.WriteAccessor propertyName ../this.Key ../this.Value}});
Expand All @@ -133,20 +135,24 @@ namespace SysML2.NET.Serializer.Json.PSM
writer.WritePropertyName("{{propertyName}}"u8);
PsmSerializationDispatcher.Provide({{#Property.WriteAccessor propertyName ../this.Key ../this.Value}}.GetType())({{#Property.WriteAccessor propertyName ../this.Key ../this.Value}}, writer, serializationModeKind, includeDerivedProperties);
}
{{#if (Property.QueryAdmitsNull propertyName ../this.Key ../this.Value)}}
else
{
writer.WriteNull("{{propertyName}}"u8);
}
{{/if}}
{{else if (Property.QueryIsComposite propertyName ../this.Key ../this.Value)}}
if ({{#Property.WriteAccessor propertyName ../this.Key ../this.Value}} != null)
{
writer.WritePropertyName("{{propertyName}}"u8);
{{#Property.WriteReferencedSerializer propertyName ../this.Key ../this.Value}}.Serialize({{#Property.WriteAccessor propertyName ../this.Key ../this.Value}}, writer, serializationModeKind, includeDerivedProperties);
}
{{#if (Property.QueryAdmitsNull propertyName ../this.Key ../this.Value)}}
else
{
writer.WriteNull("{{propertyName}}"u8);
}
{{/if}}
{{else if (Property.QueryIsNullableValue propertyName ../this.Key ../this.Value)}}
if ({{#Property.WriteAccessor propertyName ../this.Key ../this.Value}}.HasValue)
{
Expand All @@ -160,16 +166,27 @@ namespace SysML2.NET.Serializer.Json.PSM
writer.WriteString("{{propertyName}}"u8, {{#Property.WriteAccessor propertyName ../this.Key ../this.Value}}.Value);
{{/if}}
}
{{#if (Property.QueryAdmitsNull propertyName ../this.Key ../this.Value)}}
else
{
writer.WriteNull("{{propertyName}}"u8);
}
{{/if}}
{{else if (Property.QueryIsEnumeration propertyName ../this.Key ../this.Value)}}
writer.WriteString("{{propertyName}}"u8, {{#Property.WriteEnumerationProvider propertyName ../this.Key ../this.Value}}.ToUtf8Bytes({{#Property.WriteAccessor propertyName ../this.Key ../this.Value}}));
{{else if (Property.QueryIsBoolean propertyName ../this.Key ../this.Value)}}
writer.WriteBoolean("{{propertyName}}"u8, {{#Property.WriteAccessor propertyName ../this.Key ../this.Value}});
{{else if (Property.QueryIsNumeric propertyName ../this.Key ../this.Value)}}
writer.WriteNumber("{{propertyName}}"u8, {{#Property.WriteAccessor propertyName ../this.Key ../this.Value}});
{{else if (Property.QueryIsOmittedWhenUnset propertyName ../this.Key ../this.Value)}}
if ({{#Property.WriteAccessor propertyName ../this.Key ../this.Value}} != null)
{
{{#if (Property.QueryIsUri propertyName ../this.Key ../this.Value)}}
writer.WriteString("{{propertyName}}"u8, {{#Property.WriteAccessor propertyName ../this.Key ../this.Value}}.ToString());
{{else}}
writer.WriteString("{{propertyName}}"u8, {{#Property.WriteAccessor propertyName ../this.Key ../this.Value}});
{{/if}}
}
{{else if (Property.QueryIsUri propertyName ../this.Key ../this.Value)}}
writer.WriteString("{{propertyName}}"u8, {{#Property.WriteAccessor propertyName ../this.Key ../this.Value}}?.ToString());
{{else}}
Expand Down
16 changes: 13 additions & 3 deletions SysML2.NET.Serializer.Json.Tests/PsmSerializerTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,19 @@ public void VerifySerializeRequest()

var withoutBranch = new ProjectRequest { Name = "no branch" };

var nullBranchJson = Serialize(stream => this.serializer.SerializeRequest(withoutBranch, SerializationModeKind.JSON, false, stream, this.jsonWriterOptions));
var withoutBranchJson = Serialize(stream => this.serializer.SerializeRequest(withoutBranch, SerializationModeKind.JSON, false, stream, this.jsonWriterOptions));

Assert.That(nullBranchJson, Does.Contain("\"defaultBranch\":null"));
Assert.That(withoutBranchJson, Does.Not.Contain("defaultBranch"));

var externalDataRequest = new ExternalDataRequest { ResourceIdentifier = new Uri("http://www.stariongroup.eu/external") };

var externalDataJson = Serialize(stream => this.serializer.SerializeRequest(externalDataRequest, SerializationModeKind.JSON, false, stream, this.jsonWriterOptions));

Assert.That(externalDataJson, Does.Contain("\"resourceIdentifier\":\"http://www.stariongroup.eu/external\""));

var withoutResourceIdentifierJson = Serialize(stream => this.serializer.SerializeRequest(new ExternalDataRequest(), SerializationModeKind.JSON, false, stream, this.jsonWriterOptions));

Assert.That(withoutResourceIdentifierJson, Does.Not.Contain("resourceIdentifier"));
}

[Test]
Expand Down Expand Up @@ -246,7 +256,7 @@ public void VerifySerializeDataVersion()
using (Assert.EnterMultipleScope())
{
Assert.That(nullPayloadDocument.RootElement.GetProperty("payload").ValueKind, Is.EqualTo(JsonValueKind.Null));
Assert.That(nullPayloadDocument.RootElement.GetProperty("identity").ValueKind, Is.EqualTo(JsonValueKind.Null));
Assert.That(nullPayloadDocument.RootElement.TryGetProperty("identity", out _), Is.False);
}

var commitRequest = new CommitRequest
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading