-
-
Notifications
You must be signed in to change notification settings - Fork 158
Auto-generated JSON:API controllers using source generators #1117
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
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38f50e6
to
0613b3e
Compare
Codecov Report
@@ Coverage Diff @@
## master #1117 +/- ##
==========================================
+ Coverage 89.00% 89.41% +0.40%
==========================================
Files 249 245 -4
Lines 7123 7294 +171
==========================================
+ Hits 6340 6522 +182
+ Misses 783 772 -11
Continue to review full report at Codecov.
|
c25c019
to
ac84993
Compare
966ae83
to
e68b2a5
Compare
…work (see https://stackoverflow.com/questions/540749/can-a-c-sharp-class-inherit-attributes-from-its-interface). Fixed detection of attribute usage on base classes.
…the same resource type
… pulled in via NuGet
This lets each project opt-in for producing a NuGet package, instead of listing them globally
0042b94
to
b545d90
Compare
bart-degreed
pushed a commit
that referenced
this pull request
Dec 3, 2021
Removed Interface target from custom attributes, because it does not work (see https://stackoverflow.com/questions/540749/can-a-c-sharp-class-inherit-attributes-from-its-interface). Fixed detection of attribute usage on base classes. Auto-generation of JSON:API controllers (using source generators) Updated integration tests to use auto-generated controllers Fixed: throw at startup when multiple controllers are registered for the same resource type Addressed cleanupcode/inspectcode issues Add dependency from JsonApiDotNetCore to SourceGenerators, so it gets pulled in via NuGet Added unit tests for controller source generator Update ROADMAP.md Updated documentation Produce NuGet package in cibuild This lets each project opt-in for producing a NuGet package, instead of listing them globally Addressed review feedback Empty commit to republish docs from master branch
bart-degreed
pushed a commit
that referenced
this pull request
Dec 3, 2021
Removed Interface target from custom attributes, because it does not work (see https://stackoverflow.com/questions/540749/can-a-c-sharp-class-inherit-attributes-from-its-interface). Fixed detection of attribute usage on base classes. Auto-generation of JSON:API controllers (using source generators) Updated integration tests to use auto-generated controllers Fixed: throw at startup when multiple controllers are registered for the same resource type Addressed cleanupcode/inspectcode issues Add dependency from JsonApiDotNetCore to SourceGenerators, so it gets pulled in via NuGet Added unit tests for controller source generator Update ROADMAP.md Updated documentation Produce NuGet package in cibuild This lets each project opt-in for producing a NuGet package, instead of listing them globally Addressed review feedback
bart-degreed
pushed a commit
that referenced
this pull request
Dec 3, 2021
Removed Interface target from custom attributes, because it does not work (see https://stackoverflow.com/questions/540749/can-a-c-sharp-class-inherit-attributes-from-its-interface). Fixed detection of attribute usage on base classes. Auto-generation of JSON:API controllers (using source generators) Updated integration tests to use auto-generated controllers Fixed: throw at startup when multiple controllers are registered for the same resource type Addressed cleanupcode/inspectcode issues Add dependency from JsonApiDotNetCore to SourceGenerators, so it gets pulled in via NuGet Added unit tests for controller source generator Update ROADMAP.md Updated documentation Produce NuGet package in cibuild This lets each project opt-in for producing a NuGet package, instead of listing them globally Addressed review feedback
bart-degreed
pushed a commit
that referenced
this pull request
Dec 3, 2021
Removed Interface target from custom attributes, because it does not work (see https://stackoverflow.com/questions/540749/can-a-c-sharp-class-inherit-attributes-from-its-interface). Fixed detection of attribute usage on base classes. Auto-generation of JSON:API controllers (using source generators) Updated integration tests to use auto-generated controllers Fixed: throw at startup when multiple controllers are registered for the same resource type Addressed cleanupcode/inspectcode issues Add dependency from JsonApiDotNetCore to SourceGenerators, so it gets pulled in via NuGet Added unit tests for controller source generator Update ROADMAP.md Updated documentation Produce NuGet package in cibuild This lets each project opt-in for producing a NuGet package, instead of listing them globally Addressed review feedback
maurei
approved these changes
Dec 3, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Usage
To generate a controller, decorate your resource class with
[Resource]
. For example:This enables to choose per resource type if a controller should be generated or not.
A subset of exposed endpoints can be specified too:
Auto-generated controllers can easily be augmented because they are partial classes. For example:
In Visual Studio, Solution Explorer shows what code was generated:
Implementation
Alternatives
An alternative solution I considered is to dynamically register controllers to ASP.NET at runtime, basically #900.
Several reasons why I believe source generators are the better choice here:
Notes:
DisableRoutingConvention
,DisableQueryString
,JsonPropertyName
andResource
used on base classes did not work.Pluralize()
would take language rules into account, but this is not the case. The localized satellite assemblies just contain texts such as "one week ago", "yesterday" etc, which we don't use.ResourceAttribute.PublicName
is now optional. This means existing code[Resource("articles")]
needs to be changed to:[Resource(PublicName: "articles")]
.[ActivatorUtilitiesConstructor]
to force the IoC container to choose your own controller constructor with extra dependencies.Helpful links for future reference (when developing source generators):
Closes #732
Closes #365
QUALITY CHECKLIST