Skip to content

Example value of an empty OpenApiArray disappears after serialization #1643

@CarlosZaldivar2

Description

@CarlosZaldivar2

The Value property of OpenApiExample is treated as optional and an optional empty IEnumerable is ignored when writing both JSON and YAML. I don't think it's correct, since an empty array or an empty object could be a real example of a response body and we may want to show it to the API's user.

Minimal OpenAPI document with an empty array:

{
    "openapi": "3.0.1",
    "info": {
        "title": "Example API",
        "version": "1.0"
    },
    "paths": {
        "/products": {
            "get": {
                "responses": {
                    "200": {
                        "description": "Success",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "array",
                                    "items": {
                                        "type": "object",
                                        "properties": {
                                            "id": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                },
                                "examples": {
                                    "Success response - no results": {
                                        "value": []
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

The same document after it has been read and written back by OpenAPI.NET:

{
    "openapi": "3.0.1",
    "info": {
        "title": "Example API",
        "version": "1.0"
    },
    "paths": {
        "/products": {
            "get": {
                "responses": {
                    "200": {
                        "description": "Success",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "array",
                                    "items": {
                                        "type": "object",
                                        "properties": {
                                            "id": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                },
                                "examples": {
                                    "Success response - no results": {}
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions