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

generators: support custom ReferenceResolver class #350

Merged
merged 1 commit into from
Jun 13, 2019

Conversation

pahaz
Copy link
Contributor

@pahaz pahaz commented Apr 16, 2019

I need to override the logic of the ReferenceResolver class.

My customized ReferenceResolver looks like:

class StandardizedReferenceResolver(ReferenceResolver):
    def set(self, name, obj, scope=None):
        scope = self._check_scope(scope)
        assert obj is not None, "referenced objects cannot be None/null"
        self._objects[scope][name] = obj

    def setdefault(self, name, maker, scope=None):
        scope = self._check_scope(scope)
        ret = self.getdefault(name, None, scope)
        if ret is None:
            ret = maker()
            value = self.getdefault(name, None, scope)
            if value is None:
                self.set(name, ret, scope)
            elif value != ret and len(ret.properties) > len(value.properties):
                self.set(name, ret, scope)
            elif value != ret:
                logger.debug("during setdefault, maker for %s inserted a "
                             "value and returned a different value", name)
                ret = value
        return ret

    def with_scope(self, scope):
        assert scope in self.scopes, "unknown scope %s" % scope
        ret = StandardizedReferenceResolver(force_init=True)
        ret._objects = self._objects
        ret._force_scope = scope
        return ret

With that fix I can use:

from drf_yasg.generators import OpenAPISchemaGenerator

class StandardizedSchemaGenerator(OpenAPISchemaGenerator):
    reference_resolver_class = StandardizedReferenceResolver

@pahaz
Copy link
Contributor Author

pahaz commented Apr 18, 2019

Is it going to be merged?

@axnsan12
Copy link
Owner

Yeah, seems okay.

@axnsan12 axnsan12 merged commit 2656696 into axnsan12:master Jun 13, 2019
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

Successfully merging this pull request may close these issues.

2 participants