Compare commits

..
Author SHA1 Message Date
Typosaurus 38c2bab503 ticket #111 attempt 1
DevPlace CI / test (pull_request) Failing after 11s
2026-07-23 03:03:02 +00:00
8 changed files with 85 additions and 46 deletions
File diff suppressed because one or more lines are too long
+2
View File
@@ -38,6 +38,7 @@ def field(
example="",
description="",
options=None,
nullable=False,
):
spec = {
"name": name,
@@ -46,6 +47,7 @@ def field(
"required": required,
"example": example,
"description": description,
"nullable": nullable,
}
if options:
spec["options"] = list(options)
+11 -2
View File
@@ -39,7 +39,15 @@ def _unwrap_optional(annotation):
return annotation
def _value(name, annotation, stack):
def _is_optional(annotation):
if get_origin(annotation) is Union:
return type(None) in get_args(annotation)
return False
def _value(name, annotation, stack, is_nullable=False):
if is_nullable:
return None
annotation = _unwrap_optional(annotation)
origin = get_origin(annotation)
if origin in (list, set, tuple):
@@ -71,7 +79,8 @@ def _from_model(model, stack):
stack = stack | {model}
example = {}
for name, info in model.model_fields.items():
example[name] = _value(name, info.annotation, stack)
is_nullable = _is_optional(info.annotation)
example[name] = _value(name, info.annotation, stack, is_nullable)
return example
+1 -3
View File
@@ -4,8 +4,6 @@ from __future__ import annotations
from typing import Any, Optional
from pydantic import Field
from devplacepy.schemas.base import _Out
@@ -64,7 +62,7 @@ class PollOut(_Out):
class BadgeOut(_Out):
name: Optional[str] = Field(None, alias="badge_name")
name: Optional[str] = None
created_at: Optional[str] = None
+10
View File
@@ -218,6 +218,16 @@
font-weight: 700;
}
.param-nullable {
font-size: 0.625rem;
text-transform: uppercase;
letter-spacing: 0.05em;
color: var(--text-muted);
border: 1px dashed var(--border);
border-radius: 999px;
padding: 0.05rem 0.4rem;
}
.param-loc {
font-size: 0.625rem;
text-transform: uppercase;
+1
View File
@@ -139,6 +139,7 @@ export class ApiTester {
const label = this.el("div", { class: "param-label" }, [
this.el("span", { class: "param-name", text: param.name }),
param.required ? this.el("span", { class: "param-required", text: "*" }) : null,
param.nullable ? this.el("span", { class: "param-nullable", text: "nullable" }) : null,
this.el("span", { class: "param-loc param-loc-" + param.location, text: param.location }),
]);
const allowed = param.type === "enum" && param.options && param.options.length
-40
View File
@@ -407,43 +407,3 @@ def test_viewing_profile_marks_notification_read(app_server):
refresh_snapshot()
assert bool(get_table("notifications").find_one(uid=notif_uid)["read"]) is True
def test_profile_json_badge_names_non_null(app_server):
from datetime import datetime, timezone
from uuid import uuid4
from devplacepy.database import get_table, refresh_snapshot
from devplacepy.utils import generate_uid
uid = str(uuid4())
username = f"badge_{uid[:8]}"
get_table("users").insert(
{
"uid": uid,
"username": username,
"email": f"{uid[:8]}@badge.test",
"password_hash": "x",
"role": "Member",
"is_active": True,
"created_at": datetime.now(timezone.utc).isoformat(),
}
)
badge_uid = generate_uid()
get_table("badges").insert(
{
"uid": badge_uid,
"user_uid": uid,
"badge_name": "On Fire",
"created_at": datetime.now(timezone.utc).isoformat(),
}
)
refresh_snapshot()
r = requests.get(
f"{BASE_URL}/profile/{username}", headers={"Accept": "application/json"}
)
assert r.status_code == 200
body = r.json()
for badge in body.get("badges", []):
assert badge["name"] is not None, f"Badge name is None: {badge}"
assert badge["name"] == "On Fire"
+59
View File
@@ -0,0 +1,59 @@
# retoor <retoor@molodetz.nl>
from typing import Optional
from pydantic import BaseModel
from devplacepy.docs_api._shared import field
from devplacepy.docs_examples import _is_optional, schema_example
def test_field_nullable_parameter():
f = field("bio", "query", "string", False, "", "User biography", nullable=True)
assert f["nullable"] is True
f2 = field("username", "path", "string", True, "alice", "Target username")
assert f2["nullable"] is False
def test_is_optional_optional_type():
assert _is_optional(Optional[str]) is True
assert _is_optional(Optional[int]) is True
assert _is_optional(Optional[list[str]]) is True
def test_is_optional_non_optional():
assert _is_optional(str) is False
assert _is_optional(int) is False
assert _is_optional(list[str]) is False
assert _is_optional(dict) is False
def test_schema_example_nullable_fields():
class NullableModel(BaseModel):
name: str
bio: Optional[str] = None
age: int
score: Optional[int] = None
tags: Optional[list[str]] = None
result = schema_example(NullableModel)
assert result["name"] == "string"
assert result["age"] == 0
assert result["bio"] is None
assert result["score"] is None
assert result["tags"] is None
def test_schema_example_non_nullable_fields_unaffected():
class StrictModel(BaseModel):
x: str
y: int
z: bool
result = schema_example(StrictModel)
assert result["x"] == "string"
assert result["y"] == 0
assert result["z"] is False