Coder Social home page Coder Social logo

Comments (5)

provinzkraut avatar provinzkraut commented on June 16, 2024

You need to set create_examples=True on the application itself; It is off by default.

from litestar import Litestar
from litestar.openapi import OpenAPIConfig

app = Litestar(
    openapi_config=OpenAPIConfig(
        create_examples=True,
        title="OpenAPI",
        version="1.0.0",
    ),
)

from litestar.

tuukkamustonen avatar tuukkamustonen commented on June 16, 2024

Thanks. Just couldn't find it, docs don't mention it (one more thing to add there...)

Though, I have an actual application (ie. not MCVE) that I'm converting, and it doesn't have create_examples=True set, yet it generates those examples. So, there must be bug/glitch in there somewhere.

I will debug more (probably tomorrow) and report back.

from litestar.

tuukkamustonen avatar tuukkamustonen commented on June 16, 2024

@provinzkraut Okay, traced it out. This one's actually quite funny. In my actual app, I had annotated the response type as dict[str, Any] and not as Response (temporarily, for laziness) as the return type of the endpoint, like:

from typing import Any

import uvicorn
from litestar import Litestar, post
from litestar.openapi import ResponseSpec
from pydantic import BaseModel


class Response(BaseModel):
    text: str
    num: int


@post("/", responses={201: ResponseSpec(Response)})
async def endpoint() -> dict[str, Any]:  # <---- HERE
    return {"text": "hello", "num": 1}


app = Litestar(
    route_handlers=[endpoint],
)

uvicorn.run(app)

This leads to Litestar _generating examples. Annotating the function return type with the Pydantic model, it does not (as create_examples=False).

My usage here is wrong (annotating a different type than in @post(responses=...)). However, I think the validation could be stricter, perhaps so that this would rather raise, then proceed with unexpected(?) behavior. What do you think?

from litestar.

provinzkraut avatar provinzkraut commented on June 16, 2024

Just to confirm, this only happens when you include a ResponseSpec?

from litestar.

tuukkamustonen avatar tuukkamustonen commented on June 16, 2024

Yes. It reproduces with dict[str, Any] as response type and with ResponseSpec defined.

With dict[str, Any] and without ResponseSpec the whole component is missing from the schema (probably as supposed):

{
  "info": {
    "title": "Litestar API",
    "version": "1.0.0"
  },
  "openapi": "3.1.0",
  "servers": [
    {
      "url": "/"
    }
  ],
  "paths": {
    "/": {
      "post": {
        "summary": "Endpoint",
        "operationId": "Endpoint",
        "responses": {
          "201": {
            "description": "Document created, URL follows",
            "headers": {},
            "content": {
              "application/json": {
                "schema": {
                  "additionalProperties": {},
                  "type": "object"
                }
              }
            }
          }
        },
        "deprecated": false
      }
    }
  },
  "components": {
    "schemas": {}
  }
}

from litestar.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.