aries-cloudagent-python icon indicating copy to clipboard operation
aries-cloudagent-python copied to clipboard

Custom Marshmallow types do not autogenerate correctly in swagger spec

Open ff137 opened this issue 2 years ago • 1 comments

Some request schemas are defined with custom Marshmallow types, such as in the CredentialSchema:

class CredentialSchema(BaseModelSchema):
...
    context = fields.List(
        UriOrDictField(
            required=True,
        ),
        data_key="@context",
        required=True,
        description="The JSON-LD context of the credential",
        **CREDENTIAL_CONTEXT,
    )
...
    issuer = StrOrDictField(
        required=True,
        description=(
            "The JSON-LD Verifiable Credential Issuer."
            " Either string of object with id field."
        ),
        example=DIDKey.EXAMPLE,
    )
...
    credential_subject = DictOrDictListField(
        required=True,
        data_key="credentialSubject",
        **CREDENTIAL_SUBJECT,
    )

These custom fields (UriOrDictField, StrOrDictField, DictOrDictListField) are configured for validation purposes, but the type requirements don't reflect in the autogenerated swagger spec.

For an overview of schemas that are affected by this, you can look at the changes from this PR in cloudcontroller.

My proposal would be to define custom schemas for these cases, instead of just custom marshmallow fields. I'll see if I can post a PR in the next week or so.

ff137 avatar Aug 04 '23 12:08 ff137

I've spent some time investigating this in the past; based on my findings (which was some time ago), Marshmallow doesn't have a good way to express polymorphic fields. I'd be interested to see if you can come up with a better way; if not, I think we can also add some annotations to the field to influence the generation of the swagger.

dbluhm avatar Aug 07 '23 15:08 dbluhm