-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from collerek/fastapi_docs
fix multiple pkonly models with same name in openapi schema
- Loading branch information
Showing
5 changed files
with
163 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from typing import Optional | ||
from uuid import UUID, uuid4 | ||
|
||
import databases | ||
import sqlalchemy | ||
from fastapi import FastAPI | ||
from starlette.testclient import TestClient | ||
|
||
import ormar | ||
|
||
app = FastAPI() | ||
DATABASE_URL = "sqlite:///db.sqlite" | ||
database = databases.Database(DATABASE_URL) | ||
metadata = sqlalchemy.MetaData() | ||
|
||
|
||
class BaseMeta(ormar.ModelMeta): | ||
metadata = metadata | ||
database = database | ||
|
||
|
||
class CA(ormar.Model): | ||
class Meta(BaseMeta): | ||
tablename = "cas" | ||
|
||
id: UUID = ormar.UUID(primary_key=True, default=uuid4) | ||
ca_name: str = ormar.Text(default="") | ||
|
||
|
||
class CB1(ormar.Model): | ||
class Meta(BaseMeta): | ||
tablename = "cb1s" | ||
|
||
id: UUID = ormar.UUID(primary_key=True, default=uuid4) | ||
cb1_name: str = ormar.Text(default="") | ||
ca1: Optional[CA] = ormar.ForeignKey(CA, nullable=True) | ||
|
||
|
||
class CB2(ormar.Model): | ||
class Meta(BaseMeta): | ||
tablename = "cb2s" | ||
|
||
id: UUID = ormar.UUID(primary_key=True, default=uuid4) | ||
cb2_name: str = ormar.Text(default="") | ||
ca2: Optional[CA] = ormar.ForeignKey(CA, nullable=True) | ||
|
||
|
||
@app.get("/ca", response_model=CA) | ||
async def get_ca(): # pragma: no cover | ||
return None | ||
|
||
|
||
@app.get("/cb1", response_model=CB1) | ||
async def get_cb1(): # pragma: no cover | ||
return None | ||
|
||
|
||
@app.get("/cb2", response_model=CB2) | ||
async def get_cb2(): # pragma: no cover | ||
return None | ||
|
||
|
||
def test_all_endpoints(): | ||
client = TestClient(app) | ||
with client as client: | ||
response = client.get("/openapi.json") | ||
assert response.status_code == 200, response.text | ||
schema = response.json() | ||
components = schema["components"]["schemas"] | ||
assert all(x in components for x in ["CA", "CB1", "CB2"]) | ||
pk_onlys = [x for x in list(components.keys()) if x.startswith("PkOnly")] | ||
assert len(pk_onlys) == 2 |