-
-
Notifications
You must be signed in to change notification settings - Fork 42
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
Remove unreferenced schema, add --trim-unused-schema
& --keep-schema
#199
Remove unreferenced schema, add --trim-unused-schema
& --keep-schema
#199
Conversation
1aa2c11
to
56d697e
Compare
Codecov Report
@@ Coverage Diff @@
## main #199 +/- ##
==========================================
- Coverage 98.64% 98.30% -0.35%
==========================================
Files 52 55 +3
Lines 1842 2002 +160
==========================================
+ Hits 1817 1968 +151
- Misses 9 17 +8
- Partials 16 17 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
|
@kirides Thanks for this! So far it looks good to me, but I'll go through it in detail later and maybe hunt for a couple of edge cases and test with a couple of other OpenAPI specs that I can find |
@kirides I'm curious why this is just a draft. Aside from missing docs and a naming convention where members are prefixed with |
type: string | ||
nullable: true | ||
additionalProperties: false | ||
UserComponent: |
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.
@kirides What happens if there is a component called AnotherUserComponent
that references UserComponent
?
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's not accessible from any request schema (request, header, parameters, response) it will be trimmed away.
OpenApi doesn't have "inheritance", so any Schema that might be returned, should be in an AnyOf response type, thus won't be removed.
application/json: | ||
schema: | ||
$ref: '#/components/schemas/Warehouse' | ||
'400': |
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'm guilty of defining a ProblemDetails
component but never specifying it as a 400
response content 😅
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.
at work we return ProblemDetails for anything non success, thus any 400 & 500, don't ask me why.
src/Refitter.Core/SchemaCleaner.cs
Outdated
|
||
public class SchemaCleaner | ||
{ | ||
private readonly OpenApiDocument _doc; |
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.
@kirides this is just me nit-picking, but can we rename _doc
to document
?
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.
done
{ | ||
_doc = doc; | ||
} | ||
public void RemoveUnreferencedSchema() |
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.
@kirides can you please add an empty line between the constructor and the RemoveUnreferencedSchema()
method declaration
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.
done
I had some gripes with the way schema references work with NSwag, as they work a bit different than Microsoft's OpenApi libs. I'll have to check with some more elaborate documents (headers, ref of ref, ...) before I'm done. As for why I did open this PR while still in the draft, just to show that I do actually implement the feature according to the issue and maybe get some eye on it before it's "complete" |
1181570
to
bd91fce
Compare
--trim-unused-schema
& --keep-schema
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
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.
@kirides Great stuff as usual! Thanks again your contributions
fixes #170
This DRAFT implements the aforementioned feature request.
it adds a new commandline switch (
--trim-unused-schema
) aswell as configuration option (trimUnusedSchema: true
) which works well in conjunction with--tag Xyz
filters or similar.The new option removes any Schema found in
#/components/...
which is not referenced in any way by the operations of the OpenApi document.This results in much less code being generated for large documents which are filtered to generate only a few endpoints.