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

Cannot read the ReadOnly Property from SwaggerSchemaAttribute #1702

Closed
hany-g opened this issue Jun 12, 2020 · 8 comments
Closed

Cannot read the ReadOnly Property from SwaggerSchemaAttribute #1702

hany-g opened this issue Jun 12, 2020 · 8 comments

Comments

@hany-g
Copy link

hany-g commented Jun 12, 2020

The getter of the ReadOnly property on SwaggerSchemaAttribute throws an exception and doesn't allow external code to use it. There is an internal only property (ReadOnlyFlag) that is used by the library. We need to read the ReadOnly property to do some verification on the input objects. Such as allow readonly properties to be null in an update request. Thanks!

@domaindrivendev
Copy link
Owner

The current access restrictions are by design because the attribute is only supposed to be used in conjunction with the AnnotationsSchemaFilter that's in the same library. It sounds like you're trying to use the same attribute for a different/unintended purpose.

Can you elaborate a little more on what you're using it for - code samples would help? Thanks

@hany-g
Copy link
Author

hany-g commented Jun 17, 2020

Thanks for your response. We have some logic in our controller where we want clients to send updates on the schema objects without specifying changes to read-only properties (since they are not supposed to change from the schema specs point of view). On the service, we check for the readonly attribute and assume that this field was not changed. If it is changed, we return a bad request. This is why we need to read this attribute.

@domaindrivendev
Copy link
Owner

domaindrivendev commented Jun 19, 2020

Still not enough info to understand what you're doing - please provide code samples that explain what you're doing in more detail. As I said, you're looking to piggy back on some implementation detail that's internal to Swashbuckle and I don't want to open that up for use-case I don't fully understand.

@hany-g
Copy link
Author

hany-g commented Jul 1, 2020

Sorry for the delayed response. Here is what we do:

  1. Given two objects (an Existing resource and an incoming resource from the client), we compare their properties using reflections:

PropertyInfo[] oldPropertyInfo = oldObject.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.CanRead));
PropertyInfo[] newPropertyInfo = newObject.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.CanRead));

  1. Then we iterate on these properties to see if anything changed at all. If any of the properties changed, we try to see if the changed property was a readonly property or not since we allow readonly properties to be sent as null from the client and we assume that this is not a change. So we do the following:

SwaggerSchemaAttribute resourceModelSchemaAttribute = propertyInfo.GetCustomAttribute();
if (resourceModelSchemaAttribute == null)
{
return false;
}

PropertyInfo internalReadOnlyProperty = resourceModelSchemaAttribute.GetType().GetProperty("ReadOnlyFlag", BindingFlags.NonPublic | BindingFlags.Instance);
if (internalReadOnlyProperty == null)
{
return false;
}

object readOnlyValue = internalReadOnlyProperty.GetValue(resourceModelSchemaAttribute);
bool? readOnlyBoolean = readOnlyValue as bool?;
return readOnlyBoolean.HasValue && readOnlyBoolean.Value == true; // This means this is a swagger readonly attribute set to true.

We hope we can use the ReadOnly property directly to know whether the changed property is a swagger readonly attribute or not.

@hany-g
Copy link
Author

hany-g commented Jul 13, 2020

Gentle reminder about this.

@hany-g
Copy link
Author

hany-g commented Jul 30, 2020

Gentle reminder. Thank you!

@domaindrivendev
Copy link
Owner

domaindrivendev commented Jan 18, 2021

As mentioned previously, the SwaggerSchemaAttribute is intended to be inspected by the Swashbuckle Annotations library exclusively. It seems you want to inspect it within your own custom code and this is a usecase that I'm not willing to support at this time. I recommend you define your own custom attribute for this purpose or even use the existing System.ComponentModel.ReadOnlyAttribute. The latter isn't currently supported by Swashbuckle but that's on the roadmap (see #1954) and in lieue of that, you could wire up support for it with a very simple schema filter (see readme).

@Aeroverra
Copy link

Aeroverra commented Aug 9, 2022

smh... I will need to double attribute my models to add my own checks during reflection mapping so future me doesn't cause a vulnerability.

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

3 participants