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

SARIF OM changes for nullable int batch 1 #2650

Merged

Conversation

shaopeng-gh
Copy link
Collaborator

@shaopeng-gh shaopeng-gh commented Mar 28, 2023

  • NEW: Update Microsoft.Json.Schema.* packages from 2.1.0.0 to 2.3.0.0. #2650
  • BRK: Update Artifact.Offset, Invocation.ExitCode, Invocation.ExitSignalNumber, Invocation.ProcessId, Result.OccurrenceCount, EdgeTraversal.StepOverEdgeCount, Notification.ThreadId, StackFrame.ThreadId, ThreadFlowLocation.NestingLevel, and WebResponse.StatusCode properties from int to int? type. #2650

The reason of this change is, the upgrade of the Microsoft.Json.Schema.* packages exposed an issue that a few int properties should be nullable but it is current not nullable in our C# code.

Due to impact of the 4 line/columns numbers for Region, proposed for a separated PR and will need a confirmation for the details before start.

@shaopeng-gh shaopeng-gh marked this pull request as ready for review March 28, 2023 06:39
@@ -106,7 +106,7 @@ public static Action<WriteContext> GetWriter(string columnName)
writers["Kind"] = (c) => { c.Writer.Write(c.Result.Kind.ToString()); };
writers["Level"] = (c) => { c.Writer.Write(c.Result.Level.ToString()); };
writers["Message.Text"] = (c) => { c.Writer.Write(c.Result.Message?.Text ?? ""); };
writers["OccurrenceCount"] = (c) => { c.Writer.Write(c.Result.OccurrenceCount); };
writers["OccurrenceCount"] = (c) => { c.Writer.Write(c.Result.OccurrenceCount?.ToString() ?? ""); };
Copy link
Collaborator Author

@shaopeng-gh shaopeng-gh Mar 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

writers["OccurrenceCount"] = (c) => { c.Writer.Write(c.Result.OccurrenceCount?.ToString() ?? ""); };

note, this file is from sample solution. #Closed

@@ -1740,6 +1744,7 @@
"charLength": {
Copy link
Collaborator Author

@shaopeng-gh shaopeng-gh Mar 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

charLength

for charLength and byteLength, in the spec they indeed have the default value 0. I think we missed it. #Closed

@@ -1709,24 +1709,28 @@
"startLine": {
"description": "The line number of the first character in the region.",
"type": "integer",
"default": 0,
Copy link
Collaborator Author

@shaopeng-gh shaopeng-gh Mar 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"default": 0,

for the 4 number properties, we will do in a separated PR.
This change is temporary just for skip them so they will not be generated as nullable, and the PR is against the dev branch, we will get to it before to main, so this change will not show up in main at all. #Closed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so what's odd about this is we have a default of -1 for other things, why not this?

how can a default be lower than the minimum value? seems odd.

@@ -39,24 +39,32 @@ public virtual SarifNodeKind SarifNodeKind
/// The line number of the first character in the region.
/// </summary>
[DataMember(Name = "startLine", IsRequired = false, EmitDefaultValue = false)]
[DefaultValue(0)]
Copy link
Collaborator Author

@shaopeng-gh shaopeng-gh Mar 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[DefaultValue(0)]

this file is generated from the RTM6 json schema, please see my comment on that file in this PR. #WontFix

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, so we're making the same mistake though! '0' is a default value that effectively means it isn't there, so we should have a nullable type here, again.

This is more painful from an API use standpoint, maybe too painful! But everything would be consistent.

I do see from the spec that the region class in particular has complex/unusual default value semantics...

@@ -221,6 +221,16 @@
}
}
],
"ExternalProperties.Schema": [
Copy link
Member

@michaelcfanning michaelcfanning Mar 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Schema

how did we miss this? #Closed

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replied in the other comment.

@@ -1075,6 +1095,26 @@
}
}
],
"TranslationMetadata.DownloadUri": [
Copy link
Member

@michaelcfanning michaelcfanning Mar 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DownloadUri

Good changes, but hm. how'd you find them? #Resolved

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure how we missed them.
I found them by test not eye.

@michaelcfanning
Copy link
Member

michaelcfanning commented Mar 30, 2023

    [DefaultValue(-1)]

why is this still here? :) when will it go away?


In reply to: 1491078338


Refers to: src/Sarif/Autogenerated/Address.cs:43 in fefed9a. [](commit_id = fefed9a, deletion_comment = False)

@@ -38,6 +38,7 @@ public virtual SarifNodeKind SarifNodeKind
/// The URI of the JSON schema corresponding to the version of the external property file format.
/// </summary>
[DataMember(Name = "schema", IsRequired = false, EmitDefaultValue = false)]
[JsonConverter(typeof(Microsoft.CodeAnalysis.Sarif.Readers.UriConverter))]
public virtual Uri Schema { get; set; }
Copy link
Member

@michaelcfanning michaelcfanning Mar 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uri

There's a 1x1 mapping between this type, URI and the JsonConverter attribute. That could have been the way we handled this in the codegen hints + jschema (i.e., provide a way to emit a specific attribute for every instance of a property with a specific return type). Just an idea, can you confirm this feature does not already exist? #Resolved

Copy link
Collaborator Author

@shaopeng-gh shaopeng-gh Apr 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The UriConverter class is from SDK not JSchama so we can not directly always generate this attribute when Uri type in JSchema.

We could have support hint that base on type, not just name, then we can have all this declared once. I checked the hint is only based on name.

An other way is we could have still use base on name but support partial wild card e.g. *.*.Uri* then we can have it declared declared once, except this kind of special one Schema that does no have Uri in the name.
But I checked we only support * at the first part only: *.Uri.

private static string MakeHintDictionaryKey(string typeName, string propertyName)
        {
            return typeName + "." + propertyName.ToPascalCase();
        }

Let me know which direction, I can create a work item and work on it.

@@ -39,24 +39,32 @@ public virtual SarifNodeKind SarifNodeKind
/// The line number of the first character in the region.
/// </summary>
[DataMember(Name = "startLine", IsRequired = false, EmitDefaultValue = false)]
[DefaultValue(0)]
Copy link
Member

@michaelcfanning michaelcfanning Mar 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DefaultValue

have you proven this is required?

the value of 0 is a default for start line, so is the ignore and populate attribute sufficient here? i.e., no need to specify the zero value? #WontFix

@@ -116,8 +128,14 @@ public virtual SarifNodeKind SarifNodeKind
/// </summary>
public Region()
{
StartLine = 0;
Copy link
Member

@michaelcfanning michaelcfanning Mar 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StartLine

this is all unnecessary from the .NET standpoint, it's just wasted CPU cycles. #WontFix

Copy link
Collaborator Author

@shaopeng-gh shaopeng-gh Apr 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct, there are 2 thing here:

  1. the line column numbers will be gone after we work on it and regenerates, for this PR we can ignore all changes related to them for now,
  2. CharLength and ByteLength is a real change here. It is autogenerated.
    I have created an issue in JSchema:
    Improvement: remove unnecessary initialization of property default value in ToDotNet jschema#172

@@ -15,7 +15,7 @@ namespace Microsoft.CodeAnalysis.Sarif
/// A physical or virtual address, or a range of addresses, in an 'addressable region' (memory or a binary file).
/// </summary>
[DataContract]
[GeneratedCode("Microsoft.Json.Schema.ToDotNet", "2.1.0.0")]
[GeneratedCode("Microsoft.Json.Schema.ToDotNet", "2.3.0.0")]
public partial class Address : PropertyBagHolder, ISarifNode
Copy link
Member

@michaelcfanning michaelcfanning Mar 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

partial

so do you know for sure not that every file in notyetautogenerated contains other code changes that need to be preserved? or can we delete any of these? #WontFix

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I did have compared that every file in the notyetautogenerated folder have difference between the raw auto generated one, when work on these I believe some tests are needed, I think for the separate change.

@@ -29,7 +29,7 @@ public static IExpressionEvaluator<Result> ResultEvaluator(TermExpression term)
case "message.text":
return new StringEvaluator<Result>(r => r.Message?.Text, term, StringComparison.OrdinalIgnoreCase);
case "occurrencecount":
return new LongEvaluator<Result>(r => r.OccurrenceCount, term);
return new LongEvaluator<Result>(r => r.OccurrenceCount ?? 1, term);
Copy link
Member

@michaelcfanning michaelcfanning Mar 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OccurrenceCount

gee look at this, this hadn't occurred to me! occurrence count's default is 1!!

wonder if we should include this in a spec errata #Resolved

Copy link
Collaborator Author

@shaopeng-gh shaopeng-gh Apr 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wonder if we should include this in a spec errata ---- I agree with this and will need to talk to you if we consider "the spec is missing this default as 1 by mistake". If so we can keep the OccurrenceCount as not nullable and have default as 1.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All types with a default must be nullable!! We can discuss in stand-up today. :)

Copy link
Collaborator Author

@shaopeng-gh shaopeng-gh Apr 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All types with a default must be nullable!! ---- @michaelcfanning I have question about this because it is the opposite of what my understanding, my understanding and also my PR that lead to this was that was reviewed and checked in.
I created an email to describe details.
Also please review that change that was checked in: microsoft/jschema#167

And because integers without default we HAVE to make them nullable, if with a default you also want them to be nullable that would be basically everything should be nullable.

@michaelcfanning
Copy link
Member

michaelcfanning commented Mar 30, 2023

      "default": -1,

observe that the minimum value here matches the default value!


In reply to: 1491093798


In reply to: 1491093798


Refers to: src/Sarif/Schemata/sarif-2.1.0-rtm.6.json:1740 in fefed9a. [](commit_id = fefed9a, deletion_comment = False)

@@ -1709,24 +1709,28 @@
"startLine": {
"description": "The line number of the first character in the region.",
"type": "integer",
"default": 0,
Copy link
Member

@michaelcfanning michaelcfanning Mar 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default

something is wrong here. if this is the default, then minimum should be set as 0 as well. #WontFix

@@ -225,6 +225,11 @@ private void ProcessLine(string logFileLine, ref int nestingLevel, Result result
}
}

if (threadFlowLocation.NestingLevel == 0)
Copy link
Member

@michaelcfanning michaelcfanning Mar 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NestingLevel

why is this correct?

how do we distinguish between not present vs. an actual nesting level of 0? #Resolved

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are correct, this is code for converting StaticDriverVerifier result to SARIF, the NestingLevel here is converted so if it is 0 then it means 0, not unknown. I have reverted this change, and fixed the test expected outputs to have this property.

@@ -43,7 +43,7 @@
},
{
"location": {
"uri": "file:/c:/src/file.c",
"uri": "file:///c:/src/file.c",
Copy link
Member

@michaelcfanning michaelcfanning Mar 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file

i see! #Closed

@@ -680,7 +680,7 @@ public void SarifLogger_LogsStartAndEndTimesByDefault()
// Other properties should be empty.
invocation.CommandLine.Should().BeNull();
invocation.WorkingDirectory.Should().BeNull();
invocation.ProcessId.Should().Be(0);
invocation.ProcessId.Should().Be(null);
Copy link
Member

@michaelcfanning michaelcfanning Mar 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be(null)

Just use .BeNull() here? #Resolved

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct, batch fixed all existing ones.

@@ -1709,24 +1709,28 @@
"startLine": {
"description": "The line number of the first character in the region.",
"type": "integer",
"default": 0,
"minimum": 1
Copy link
Member

@michaelcfanning michaelcfanning Mar 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"minimum": 1

set all minimums to default and then let's please make every int value that has a default in the schema be a nullable item.

of course, we are skipping region for now. I wonder if we should build a special region deserializer! :) #WontFix

@shaopeng-gh shaopeng-gh marked this pull request as draft April 4, 2023 02:19
@github-advanced-security
Copy link

You have successfully added a new CodeQL configuration .github/workflows/codeql-analysis.yml:analyze. As part of the setup process, we have scanned this repository and found 6 existing alerts. Please check the repository Security tab to see all alerts.

CharOffset = -1;
CharLength = 0;

Check warning

Code scanning / CodeQL

Virtual call in constructor or destructor

Avoid virtual calls in a constructor or destructor.
ByteOffset = -1;
ByteLength = 0;

Check warning

Code scanning / CodeQL

Virtual call in constructor or destructor

Avoid virtual calls in a constructor or destructor.
@@ -116,8 +128,14 @@
/// </summary>
public Region()
{
StartLine = 0;

Check warning

Code scanning / CodeQL

Virtual call in constructor or destructor

Avoid virtual calls in a constructor or destructor.
@@ -116,8 +128,14 @@
/// </summary>
public Region()
{
StartLine = 0;
StartColumn = 0;

Check warning

Code scanning / CodeQL

Virtual call in constructor or destructor

Avoid virtual calls in a constructor or destructor.
@@ -116,8 +128,14 @@
/// </summary>
public Region()
{
StartLine = 0;
StartColumn = 0;
EndLine = 0;

Check warning

Code scanning / CodeQL

Virtual call in constructor or destructor

Avoid virtual calls in a constructor or destructor.
StartLine = 0;
StartColumn = 0;
EndLine = 0;
EndColumn = 0;

Check warning

Code scanning / CodeQL

Virtual call in constructor or destructor

Avoid virtual calls in a constructor or destructor.
@shaopeng-gh shaopeng-gh marked this pull request as ready for review April 4, 2023 06:26
@shaopeng-gh
Copy link
Collaborator Author

Thanks, I see your comments about the changes about the 4 line/column numbers.
You are correct that all changes related to them are wrong.
The reason is I want to skip these 4 fields and keep them not nullable now, and because they are auto generated, the way I skip them is to add a default value to them. Can be any integer but why I choose 0 which is less than the minimum 1? Because it is actually the effective default, if not most of the tests will fail.
Those temporary changes related to them are auto generated which will be auto generated again once we work on these 4 fields.
So we can ignore any changes related to them now.
Also this will go to DEV branch first so will not get merged to main before we get to these 4 fields.
I put the comment where and will close those ones, to avoid duplicated comments to look.

@shaopeng-gh
Copy link
Collaborator Author

    [DefaultValue(-1)]

Will need to fix this in the future changes, this change set is only those triggered by the new version of JSchema, must act or else will not build.


In reply to: 1491078338


Refers to: src/Sarif/Autogenerated/Address.cs:43 in fefed9a. [](commit_id = fefed9a, deletion_comment = False)

@shaopeng-gh shaopeng-gh changed the title SARIF OM changes for nullable int SARIF OM changes for nullable int batch 1 Apr 5, 2023
@michaelcfanning michaelcfanning merged commit 3ca7c4c into dev/SARIF_OM_Changes Apr 5, 2023
@michaelcfanning michaelcfanning deleted the users/shaopeng-gh/updatejschema_skip4 branch April 5, 2023 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants