From b7e268bf189c87f43282f25c7ebf2771086541ba Mon Sep 17 00:00:00 2001 From: Mattie Nickson Date: Fri, 6 Jun 2025 10:08:59 +0100 Subject: [PATCH] Fixed API docs rendering The use of `strip_whitespace` in `Field` for the query validation model caused the automatic generation of the API docs to fail with `ValueError: `schema_extra` declares key `strip_whitespace` which does not exist in `Schema` object`. By switching from `Field` to `Annotated` with `StringConstraints`, this avoids this issue allowing API docs to be correctly rendered. Signed-off-by: Mattie Nickson --- hyperglass/models/api/query.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/hyperglass/models/api/query.py b/hyperglass/models/api/query.py index 2f2a1c7..d199214 100644 --- a/hyperglass/models/api/query.py +++ b/hyperglass/models/api/query.py @@ -7,7 +7,8 @@ import secrets from datetime import datetime # Third Party -from pydantic import Field, BaseModel, ConfigDict, field_validator +from pydantic import BaseModel, ConfigDict, field_validator, StringConstraints +from typing_extensions import Annotated # Project from hyperglass.log import log @@ -21,6 +22,11 @@ from hyperglass.exceptions.private import InputValidationError from ..config.devices import Device +QueryLocation = Annotated[str, StringConstraints(strict=True, min_length=1, strip_whitespace=True)] +QueryTarget = Annotated[str, StringConstraints(min_length=1, strip_whitespace=True)] +QueryType = Annotated[str, StringConstraints(strict=True, min_length=1, strip_whitespace=True)] + + class SimpleQuery(BaseModel): """A simple representation of a post-validated query.""" @@ -39,12 +45,12 @@ class Query(BaseModel): model_config = ConfigDict(extra="allow", alias_generator=snake_to_camel, populate_by_name=True) # Device `name` field - query_location: str = Field(strict=True, min_length=1, strip_whitespace=True) + query_location: QueryLocation - query_target: t.Union[t.List[str], str] = Field(min_length=1, strip_whitespace=True) + query_target: t.Union[t.List[QueryTarget], QueryTarget] # Directive `id` field - query_type: str = Field(strict=True, min_length=1, strip_whitespace=True) + query_type: QueryType _kwargs: t.Dict[str, t.Any] def __init__(self, **data) -> None: