Skip to content

Change a schema that is not used in any endpoint

In some situations, you may want to use versioning not only for your OpenAPI schemas and endpoints but also within your code, for example to send versioned webhooks to your clients.

Suppose you want to change the type of an id field from integer to string:

from cadwyn import (
    ResponseInfo,
    VersionChange,
    convert_response_to_previous_version_for,
    schema,
)
from pydantic import BaseModel


# User from latest version
class User(BaseModel):
    id: str


class ChangeUserIdFromIntegerToString(VersionChange):
    """'User.id' is now a string so the API can support identifiers that are not numeric."""

    instructions_to_migrate_to_previous_version = [
        schema(User).field("id").had(type=int),
    ]

    @convert_response_to_previous_version_for(User)
    def change_id_to_int(response: ResponseInfo): ...

Unless there is an endpoint that has User as its response_model, this code will end up causing an error when you run the Cadwyn app. This is because Cadwyn tries to make sure that all of your converters apply to at least one endpoint. Otherwise, it would be too easy for you to make a mistake when writing converters for the wrong schemas.

To avoid it, set check_usage=False:

from cadwyn import (
    ResponseInfo,
    VersionChange,
    convert_response_to_previous_version_for,
    schema,
)
from pydantic import BaseModel


# User from latest version
class User(BaseModel):
    id: str


class ChangeUserIdFromIntegerToString(VersionChange):
    """'User.id' is now a string so the API can support identifiers that are not numeric."""

    instructions_to_migrate_to_previous_version = [
        schema(User).field("id").had(type=int),
    ]

    @convert_response_to_previous_version_for(User, check_usage=False)
    def change_id_to_int(response: ResponseInfo): ...