-
Notifications
You must be signed in to change notification settings - Fork 10
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
Generate required properties as non-nullable #160
Generate required properties as non-nullable #160
Conversation
@@ -350,7 +350,12 @@ private string GetPropertyType(string className, string name, PropertyDefinition | |||
throw new InvalidOperationException("Base Type is null or empty."); | |||
} | |||
|
|||
return $"{baseType}?"; | |||
if (ownerClass?.Definition?.Required.Contains(jsonName!) == true && baseType != "DateTime") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left DateTime?
as always nullable, since:
- Zero makes semantically less sense, compared to for example integers
- It would've caused various compilation errors (particularly around
createdAt ?? DateTime.UtcNow
)
@@ -318,7 +318,7 @@ private string GetUnknownObjectType(PropertyDefinition property) | |||
return "ATObject"; | |||
} | |||
|
|||
private string GetPropertyType(string className, string name, PropertyDefinition property) | |||
private string GetPropertyType(string className, string name, PropertyDefinition property, ClassGeneration? ownerClass = null, string? jsonName = null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are 2 other call sites against this, I left the old behavior for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The two other call sites are respectively for List<>
, where ?
is already always removed, and implementation statements that don't depend on nullness, so there's nothing to actually change.
@@ -2252,7 +2252,8 @@ private void GenerateHeader(StringBuilder sb) | |||
sb.AppendLine(); | |||
|
|||
// Add #nullable | |||
sb.AppendLine("#nullable enable"); | |||
sb.AppendLine("#nullable enable annotations"); | |||
sb.AppendLine("#nullable disable warnings"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was considering whether to use required Example example { get; set; }
, but that would break existing users that set the fields one by one instead of using an initialization block.
The warnings for generated files are disabled, since the (CBORObject)
constructor only sets the fields that are actually in the CBOR.
// These three fields are all required, according to the lexicon. | ||
if (urlEmbed == null || title == null || description == null) | ||
{ | ||
return null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it fails to find any one of these properties, it should throw an Exception. Otherwise you'll end up with a null object with no idea why it failed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GenerateEmbedExternal
already returns null
to represent missing or incorrect OpenGraph tags, and bskycli
prints an error message in such case.
Can you rebase on Main and run Considering |
4dc73a0
to
248b458
Compare
@alnkesq What OS did you run the I run it on my Mac. I think there's an issue with how the files get generated through the order in operation of what gets generated first. For bskycli, the Also |
@alnkesq Okay, I think I figured it out: FishyFlip/tools/FFSourceGen/Program.cs Line 113 in 45e1c99
If you update this to private async Task GenerateClasses(string lexiconPath)
{
var files = Directory.EnumerateFiles(lexiconPath, "*.json", SearchOption.AllDirectories).OrderByDescending(n => n).ToList();
... Then I think it should normalize the list and be consistent. In your branch if I set it to |
For the tests failing, A bunch of |
Yes I had noticed that problem (still solving other issues) if (defJsonPath.Replace("\\", "/").EndsWith("/blue/zio/atfile/lock.json"))
{
// There's a typo in the original file.
// The generation output depends on the file system enumeration order
// since there's a second blue.zio.atfile.upload
// https://github.com/ziodotsh/lexicons/pull/2
schemaDocument.Id = "blue.zio.atfile.lock";
} |
I'm running it on WSL, with NTFS enumeration order (lexicographical) |
I'm not a bash expert, but if [ "$target_dir" == "whtwnd-whitewind-blog" ]; then
- REPO_DIRS+=("$(PWD)/../fflexicons/$target_dir/lexicons/com/whtwnd")
+ REPO_DIRS+=("$PWD/../fflexicons/$target_dir/lexicons/com/whtwnd") and so on. Otherwise it looks for a binary called PWD |
https://github.com/drasticactions/FishyFlip/blob/develop/tools/FFSourceGen/Program.cs#L2070-L2123 This is why the JSONConverters are missing, it was me being really lazy and just checking raw strings, including the nullable If you mod that to be inclusive for both nullable and non-nullable types it should work, I would just hack it to be a if (property.RawType.Contains("FishyFlip.Models.ATUri")) then it should be fine. This is what I get for "stream of consciousness" programming. e. Actually: FishyFlip/tools/FFSourceGen/Program.cs Lines 2070 to 2097 in 45e1c99
This |
Changed it on my Mac and it worked fine. You can commit it. |
248b458
to
73295f4
Compare
This reverts commit beeba28.
The lexicon fix ziodotsh/lexicons#2 has already been merged, so I removed the workaround. |
Ran the Authed Tests and they came back green. Once this builds I'll merge it. Since it's a breaking change I'm not going to push this to stable right away. I'll test it against some other libraries and apps to see what the damage will be. Since those values were required anyway for the APIs to work It shouldn't be bad, but just in case. |
Warnings aside, the only breaks I had in my own code were old If it's too bad we can change this PR to only work on reference types. |
Turns out that at some point in tagging https://www.nuget.org/packages/FishyFlip/3.4.0-alpha.0 I ended up tagging |
When you navigate through the properties of an ATProto response, all of them will be set to nullable, even if the lexicon claims they're required.
This causes either many warnings, or desensitization by using
!
everywhere (even in cases where the property is legitimately optional)Clients and PDSes generally reject records with missing required fields, so there shouldn't be many broken records in the wild (and in those rare cases,
NullReferenceExceptions
are probably fine, since our input is literally bad data).I re-ran the code generation tool (at least for the atproto lexicon) and everything compiles as expected (I didn't include it here since it would've been very noisy for an initial draft)
This is what inspired me to write this PR: alnkesq/AppViewLite@8debd3e