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

In mocking, prefer example(s) over default value when it's null #2609

Open
mtvx opened this issue Sep 19, 2024 · 0 comments
Open

In mocking, prefer example(s) over default value when it's null #2609

mtvx opened this issue Sep 19, 2024 · 0 comments

Comments

@mtvx
Copy link

mtvx commented Sep 19, 2024

Context

The docs at https://docs.stoplight.io/docs/prism/83dbbd75532cf-http-mocking have this as the highest priority in value mocking:

  • If the property has a default value, then it will return the specified value.

This is highest on the list, so if default is found, it's used no matter what. This is problematic with nullable fields, which may or may not exist in a response:

{
    "type": "object",
    "properties": {
        "foo": {
            "type": ["string", "null"],
            "default": null,
            "examples": ["example-1"],
        },
    },
    "required": [],
}

Because the field is optional, it's not defined in "required". But then, it's common to explicitly show the field in a response, so it's declared as nullable, too ("type": ["string", "null"]). No problem so far.

The problem comes when the schema also contains "default": null value, which should be valid although maybe not absolutely required? (Should be interpret as null if it doesn't exist, at least when not talking about PATCH methods...)

At least Pydantic, a popular Python library (and probably some other tools as well?), output schema like this when the default value is None:

class Foobar(BaseModel):
    foo: str | None = None

Because it really defines a default value, it totally makes sense that the outputted JSON schema has that "default": null.

Current Behavior

Prism chooses null value in mocking, because it's declared as default.

Expected Behavior

There are much more interesting options available than null (although technically valid). It would be much nicer to use "example-1". Null just gives basically nothing, it's obscure and everything.

Have Prism use the first example (if one exists), instead of the null default.

Possible Solution(s)

Prioritize example over default, when it's null.

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

No branches or pull requests

1 participant