-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Improve GraphQL Query Schema validation and logging #6536
Conversation
src/OrchardCore.Modules/OrchardCore.OpenId/Configuration/OpenIdClientConfiguration.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore.Modules/OrchardCore.OpenId/Configuration/OpenIdClientConfiguration.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore.Modules/OrchardCore.OpenId/Configuration/OpenIdClientConfiguration.cs
Show resolved
Hide resolved
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.
Logging uses a different string interpolation method to prevent allocations, so you should never use $
to format log messages.
I think I'm ok with this staying as LogWarning
src/OrchardCore.Modules/OrchardCore.OpenId/Configuration/OpenIdClientConfiguration.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore.Modules/OrchardCore.OpenId/Configuration/OpenIdClientConfiguration.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore.Modules/OrchardCore.OpenId/Configuration/OpenIdClientConfiguration.cs
Outdated
Show resolved
Hide resolved
…dClientConfiguration.cs Co-authored-by: Dean Marcussen <[email protected]>
var queryField = BuildContentTypeFieldType(schema, contentType, query); | ||
if (queryField != null) | ||
var querySchema = JObject.Parse(query.Schema); | ||
if (!querySchema.ContainsKey("type")) |
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.
We have to add a sample schema on the page ,but I don't know the right schema for here.
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 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.
Please leave that for a seperate pr
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.
@hyzx86 I have updated the logging statements with correct usage.
Please can you look at my comment on the controller error handling and see what the result is when adding a ModelState
error. It should definitely use ModelState
errors, it's possible you may have to have a notifier as well, but I would prefer it is all handled by ModelState
errors.
Probably sorting that out is the last thing to do for this pr :)
if (!string.IsNullOrWhiteSpace(query.Schema) && !CheckSchema(query.Schema)) | ||
{ | ||
_notifier.Error(H["Invalid schema JSON supplied."]); | ||
model.Editor = await _displayManager.BuildEditorAsync(query, updater: _updateModelAccessor.ModelUpdater, isNew: true); |
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.
This should add just add a ModelState
error, and see what the result is when it is handled later in the code
// If we got this far, something failed, redisplay form --> All errors should be handled by this section
model.Editor = editor;
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.
Hi @deanmarcussen , thanks for your help , ModelState
added
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
var queryField = BuildContentTypeFieldType(schema, contentType, query); | ||
if (queryField != null) | ||
var querySchema = JObject.Parse(query.Schema); | ||
if (!querySchema.ContainsKey("type")) |
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.
Please leave that for a seperate pr
|
||
if (!string.IsNullOrWhiteSpace(query.Schema) && !CheckSchema(query.Schema)) | ||
{ | ||
ModelState.AddModelError("Schema", S["Invalid schema JSON supplied."]); |
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.
Ok great, this works better.
Sorry, what I should have said though, is to implement IValidatableObject
on the QueriesCreateViewModel
.
Apply the test for valid JSON on that View Model by implementing IValidatableObject
.
Here is an example from where it is used elsewhere in the code https://github.com/OrchardCMS/OrchardCore/blob/dev/src/OrchardCore.Modules/OrchardCore.Users/ViewModels/LinkExternalLoginViewModel.cs
I think you will need to apply the same to the edit controller QueriesEditViewModel
View Model
Then in the Queries.Fields.Edit.cshtml
View you should be able to apply
<span asp-validation-for="Schema"></span>
(like is done for Name
in the same file) and you will get a validation error on the schema field itself
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.
But the value of Schema comes from query variable, not model, model.Schema is always empty
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 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.
Ok, I just added a string extension for testing json here #6614
But I have had a look at the Queries AdminController
and understand what is driving that viewmodel now.
I was wrong to suggest IValidateableObject
.
The query Schema
, is actually managed and updated by the QueryDisplayDriver.cs
It already performs some checks for validity so the test you are doing for valid Json should move into the QueryDisplayDriver.cs
UpdateAsync()
method, along with the other checks for valid Names etc.
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.
LGTM.
While this looks like a lot of formatting changes, it as actually because things are being wrapped in a try catch block to prevent errors being thrown when parsing json.
No description provided.