ghandic/jsf

Unable to generate from example.

Closed this issue · 4 comments

I have this:

schema = {
    "type": "object",
    "properties": {
        "author": {"type": "string", "example": "ASDF"}
    },
    "required": ["author"],
    "example": {"author": "Hic ipsum architecto"},
}


faker = JSF(schema)
fake_json = faker.generate(
    n=1, use_defaults=True, use_examples=True
)

but none of the examples values are used.

Hi,
I was able to solve your issue using "examples" array instead of "example"

schema = {
    "type": "object",
    "properties": {"author": {"type": "string", "example": "ASDF"}},
    "required": ["author"],
    "examples": [{"author": "Hic ipsum architecto"}],
}


faker = JSF(schema)
fake_json = faker.generate(n=1, use_defaults=True, use_examples=True)

as this code generates {'author': 'Hic ipsum architecto'}

Ok, perfect, but this is not an OpenAPI standard

https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-00#rfc.section.8.6

9.5. "examples"

The value of this keyword MUST be an array. There are no restrictions placed on the values within the array. When multiple occurrences of this keyword are applicable to a single sub-instance, implementations MUST provide a flat array of all values rather than an array of arrays.

This keyword can be used to provide sample JSON values associated with a particular schema, for the purpose of illustrating usage. It is RECOMMENDED that these values be valid against the associated schema.

Implementations MAY use the value(s) of "default", if present, as an additional example. If "examples" is absent, "default" MAY still be used in this manner.

Okay, I was looking at the website: https://swagger.io/docs/specification/basic-structure/
Thank you very much for the clarification.