You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
I'm having an API, where the field is regex based (URLField in this case), and that field can be blank. However, the schema that is generated does not take that into account and does not document the possibility for blank values.
Opposed to a CharField, a blank value is not valid per strict value (in this case the URL), so additional magic is required to make that possible.
To Reproduce
class Service(models.Model):
service_hyperlink = models.URLField(help_text="Hyperlink for the service. Can be used to reference a service.",
blank=True)
class ServiceSerializer(serializers.ModelSerializer):
class Meta:
model = Service
fields = ['service_hyperlink']
extra_kwargs = {
"service_hyperlink": {"allow_blank": True}
}
Expected behavior
A schema is generated where a blank value is possible. Something along the lines of the following, using oneOf:
Additional reference
With a custom field, this workaround should already be possible by putting the according oneOf annotation to that field. I think that having this fixed in the library would be the nicer solution though.
The text was updated successfully, but these errors were encountered:
Update: The workaround I proposed does not really work greatly with drf-spectacular. Trying to use a custom field as follows:
@extend_schema_field({"oneOf": [build_basic_type(OpenApiTypes.URI), {"enum": [""], "type": "string"}]})
class BlankUrlField(URLField):
pass
This generates the same documentation as when using an URLField.
However, using
@extend_schema_field({"oneOf": [build_basic_type(OpenApiTypes.URI), {"enum": [""], "type": "string"}]})
class BlankUrlField(Field):
pass
the documentation is generated as expected (though the behavior is obviously not the way that I want). Probably something about precedence, that custom annotations are not evaluated when inbuilt fields are detected
Describe the bug
I'm having an API, where the field is regex based (URLField in this case), and that field can be blank. However, the schema that is generated does not take that into account and does not document the possibility for blank values.
Opposed to a
CharField
, a blank value is not valid per strict value (in this case the URL), so additional magic is required to make that possible.To Reproduce
Expected behavior
A schema is generated where a blank value is possible. Something along the lines of the following, using
oneOf
:Additional reference
With a custom field, this workaround should already be possible by putting the according
oneOf
annotation to that field. I think that having this fixed in the library would be the nicer solution though.The text was updated successfully, but these errors were encountered: