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

Add additionalProperties to keys that do not discard the resolved serializer #299

Closed
sergei-maertens opened this issue Feb 15, 2021 · 2 comments
Labels
bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending

Comments

@sergei-maertens
Copy link
Contributor

Mostly creating this as a self-reminder for a PR to create, but, the following code:

or component.schema.get('properties', {})

triggers whether a resolved serializer component is kept in the schema or not. The logic has some support for the typical polymorphic properties (oneOf, anyOf, allOf) and also explicit schemas - i.e. the properties key is set and the schema actually has a pre-defined shape.

However, we're in a highly dynamic use case, see our polymorphism implementation.

  1. Schema is polymorphic, and the base serializer is composed of itself + an extra serializer, mapped via the discriminator field value.
  2. Our "empty" discriminator value maps to a dynamic serializer, meaning that we don't know the schema statically
  3. To build the runtime serializer, we fetch the form definition from an external system, and map the appropriate serializer fields. This works fine, also in the context of the parent polymorphic serializer.
  4. However, for the schema generation, we're essentially dealing with a hashmap here, for which additionalProperties: true is typically used in OpenAPI
  5. The serializer class does not have any field declarations, so resolve_serializer discards it. Because it's discarded, the empty string discriminator value is discarded as well, so the whole dynamic form serializer doesn't show up in the schema.

I've solved this by generating a ResolvedComponent in the polymorphic serializer extension, however this is a pattern that will be used outside of polymorphic serializers as well.

The clean solution would be to have a base serializer, e.g. class HashMapSerializer(serializers.Serializer): pass, and all the dynamic run-time behaviour can be in subclasses (in the __new__ class method for example). Then, we can write an OpenApiSerializerExtension targeting that class, which generates an object_type:

type: object
additionalProperties: true

resolve_serializer then also needs to keep the schema if additionalProperties is found in the schema, and everyone is happy :-)

@tfranzel
Copy link
Owner

hi @sergei-maertens, took me some time to get around this again.

this isssue is related to #395 and #351 and was fixed with f824bf1 (at least i'm pretty confident that it did)

@tfranzel tfranzel added bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending labels May 31, 2021
@tfranzel
Copy link
Owner

tfranzel commented Jun 1, 2021

closing this issue for now. feel free to comment if anything is missing or not working and we will follow-up.

@tfranzel tfranzel closed this as completed Jun 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending
Projects
None yet
Development

No branches or pull requests

2 participants