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

Add flag to emit #nullable enable #344

Open
Pretasoc opened this issue Jul 28, 2022 · 2 comments
Open

Add flag to emit #nullable enable #344

Pretasoc opened this issue Jul 28, 2022 · 2 comments

Comments

@Pretasoc
Copy link
Contributor

When the --nullableReferenceAttributes is set some members are annotated with nullable attributes. At least in my usecases most of the members won't receive any attribute, but should be treates as non nullable. Because the compiler does not emit nullable attributes for tool generated source code. There is currently no easy way to get non oblivious nullability annotations. The only way to enable nullable annotions from the compiler is to explicitly enable #nullable in the generated source file.

The global nullable context does not apply for generated code files. Under either strategy, the nullable context is disabled for any source file marked as generated. This means any APIs in generated files are not annotated. There are four ways a file is marked as generated: [...]

Therefore i suggest to add a new flag, that will emit a #nullable at the start of the generated file.

From a pure "does it compile" standing point, it should be save the emit #nullable enable whenever --nullableReferenceAttributes and --netCore is set. But there might be xsd schemas, that would lead to property falsely annotated as non nullable.

@Pretasoc Pretasoc changed the title Add flat to emit #nullable enable Add flag to emit #nullable enable Jul 28, 2022
@mganss
Copy link
Owner

mganss commented Aug 9, 2022

Sorry this took so long 😳

From a pure "does it compile" standing point, it should be save the emit #nullable enable whenever --nullableReferenceAttributes and --netCore is set. But there might be xsd schemas, that would lead to property falsely annotated as non nullable.

Can you give an example?

Where should we put the #nullable enable? Before the leading comment or after?

@Pretasoc
Copy link
Contributor Author

Pretasoc commented Aug 9, 2022

No Problem.

Actually i don't have an example, where adding #nullable enable would result in wrong annotations. Its simply that i dont feel confident enough to say, there will never be a problem. I think a very common source of wrongly annotated classes could be bad designed schemas, but thats a scope we can't handle here. However i will do some investigations, whether its save or not to emit the #nullable enable in the cases mentioned above.

Where should we put the #nullable enable? Before the leading comment or after?

The microsoft documentation is not really specific about it. I tested sucessfully with putting it between the leading comment and the first namespace declaration

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

No branches or pull requests

2 participants