Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion docs/openapi_generator/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
from .pyopenapi.utility import Specification # noqa: E402


def str_presenter(dumper, data):
if data.startswith(f"/{LLAMA_STACK_API_VERSION}") or data.startswith(
"#/components/schemas/"
):
style = None
else:
style = ">" if "\n" in data or len(data) > 40 else None
return dumper.represent_scalar("tag:yaml.org,2002:str", data, style=style)


def main(output_dir: str):
output_dir = Path(output_dir)
if not output_dir.exists():
Expand Down Expand Up @@ -69,7 +79,8 @@ def main(output_dir: str):
y.sequence_dash_offset = 2
y.width = 80
y.allow_unicode = True
y.explicit_start = True
y.representer.add_representer(str, str_presenter)

y.dump(
spec.get_json(),
fp,
Expand Down
49 changes: 31 additions & 18 deletions docs/openapi_generator/pyopenapi/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
# This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree.

import collections
import hashlib
import ipaddress
import typing
from dataclasses import make_dataclass
from typing import Any, Dict, Set, Union

from ..strong_typing.core import JsonType
Expand Down Expand Up @@ -276,6 +276,20 @@ class StatusResponse:
examples: List[Any] = dataclasses.field(default_factory=list)


def create_docstring_for_request(
request_name: str, fields: List[Tuple[str, type, Any]], doc_params: Dict[str, str]
) -> str:
"""Creates a ReST-style docstring for a dynamically generated request dataclass."""
lines = ["\n"] # Short description

# Add parameter documentation in ReST format
for name, type_ in fields:
desc = doc_params.get(name, "")
lines.append(f":param {name}: {desc}")

return "\n".join(lines)


class ResponseBuilder:
content_builder: ContentBuilder

Expand Down Expand Up @@ -493,11 +507,24 @@ def _build_operation(self, op: EndpointOperation) -> Operation:
first = next(iter(op.request_params))
request_name, request_type = first

from dataclasses import make_dataclass

op_name = "".join(word.capitalize() for word in op.name.split("_"))
request_name = f"{op_name}Request"
request_type = make_dataclass(request_name, op.request_params)
fields = [
(
name,
type_,
)
for name, type_ in op.request_params
]
request_type = make_dataclass(
request_name,
fields,
namespace={
"__doc__": create_docstring_for_request(
request_name, fields, doc_params
)
},
)

requestBody = RequestBody(
content={
Expand Down Expand Up @@ -650,12 +677,6 @@ def generate(self) -> Document:
)
)

# types that are produced/consumed by operations
type_tags = [
self._build_type_tag(ref, schema)
for ref, schema in self.schema_builder.schemas.items()
]

# types that are emitted by events
event_tags: List[Tag] = []
events = get_endpoint_events(self.endpoint)
Expand All @@ -682,7 +703,6 @@ def generate(self) -> Document:
# list all operations and types
tags: List[Tag] = []
tags.extend(operation_tags)
tags.extend(type_tags)
tags.extend(event_tags)
for extra_tag_group in extra_tag_groups.values():
tags.extend(extra_tag_group)
Expand All @@ -697,13 +717,6 @@ def generate(self) -> Document:
tags=sorted(tag.name for tag in operation_tags),
)
)
if type_tags:
tag_groups.append(
TagGroup(
name=self.options.map("Types"),
tags=sorted(tag.name for tag in type_tags),
)
)
if event_tags:
tag_groups.append(
TagGroup(
Expand Down
1 change: 1 addition & 0 deletions docs/openapi_generator/strong_typing/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ def type_to_schema(self, data_type: TypeLike, force_expand: bool = False) -> Sch
# add property docstring if available
property_doc = property_docstrings.get(property_name)
if property_doc:
# print(output_name, property_doc)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I kept it on purpose as a reminder about where I had added this print. It took me a long time yesterday to figure out this doc stuff :)

property_def.pop("title", None)
property_def["description"] = property_doc

Expand Down
Loading