-
Notifications
You must be signed in to change notification settings - Fork 442
fix(server): improve Swagger /docs example annotations for A2A routes #1092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
6b0c999
387fbfe
f5fa47f
d5dce72
88da366
7c718fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| from typing import Any | ||
|
|
||
| from google.api import field_behavior_pb2 as _fb | ||
| from google.protobuf.descriptor import Descriptor, FieldDescriptor | ||
| from google.protobuf.message import Message | ||
|
|
||
|
|
@@ -33,6 +34,15 @@ | |
| FieldDescriptor.TYPE_SINT64: {'type': 'string'}, | ||
| } | ||
|
|
||
|
|
||
| def _is_required(field: FieldDescriptor) -> bool: | ||
| """Returns True if the field carries google.api.field_behavior = REQUIRED.""" | ||
| try: | ||
| return _fb.REQUIRED in field.GetOptions().Extensions[_fb.field_behavior] | ||
|
Check failure on line 41 in src/a2a/server/routes/_proto_schema.py
|
||
| except KeyError: | ||
| return False | ||
|
Comment on lines
+42
to
+43
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like this is redundant as Let's try to avoid mocks for such tests, if we don't have a certain structure across current protobufs I'd just skip testing this. We should just make sure we invoke building the entire schema in tests so that in case of changes we don't support it fails right there. |
||
|
|
||
|
|
||
| _WELL_KNOWN_SCHEMAS: dict[str, dict[str, Any]] = { | ||
| 'google.protobuf.Timestamp': {'type': 'string', 'format': 'date-time'}, | ||
| 'google.protobuf.Duration': {'type': 'string'}, | ||
|
|
@@ -57,16 +67,44 @@ | |
|
|
||
| if field.type == FieldDescriptor.TYPE_MESSAGE: | ||
| item = message_schema(field.message_type, components) | ||
| # Well-known types return an inline schema (no $ref); don't wrap them as | ||
| # nullable — they're already inlined as their JSON-Schema equivalent. | ||
| if not _is_required(field) and '$ref' in item: | ||
| return {'oneOf': [item, {'type': 'null'}], 'example': None} | ||
|
martimfasantos marked this conversation as resolved.
Outdated
|
||
| elif field.type == FieldDescriptor.TYPE_ENUM: | ||
| item = { | ||
| 'type': 'string', | ||
| 'enum': [v.name for v in field.enum_type.values], | ||
| } | ||
| values = [v.name for v in field.enum_type.values] | ||
| example = next( | ||
| ( | ||
| v | ||
| for v in values | ||
| if 'UNSPECIFIED' not in v and 'UNKNOWN' not in v | ||
| ), | ||
| values[0] if values else None, | ||
| ) | ||
| item: dict[str, Any] = {'type': 'string', 'enum': values} | ||
| if example: | ||
| item['example'] = example | ||
| else: | ||
| item = dict(_PROTO_SCALAR_SCHEMAS.get(field.type, {'type': 'string'})) | ||
| if field.type == FieldDescriptor.TYPE_STRING: | ||
| # REQUIRED fields must be non-empty; use the field name as a | ||
| # recognisable placeholder. All other strings default to "". | ||
| item['example'] = field.name if _is_required(field) else '' | ||
| elif field.type == FieldDescriptor.TYPE_BOOL: | ||
| item['example'] = False | ||
|
|
||
| if field.is_repeated: | ||
| return {'type': 'array', 'items': item} | ||
| array_schema: dict[str, Any] = {'type': 'array', 'items': item} | ||
| # Propagate the item example to the array so Swagger pre-fills one entry | ||
| # instead of generating one entry per oneOf branch. | ||
| item_example = ( | ||
| components.get(item['$ref'].split('/')[-1], {}).get('example') | ||
| if '$ref' in item | ||
| else item.get('example') | ||
| ) | ||
| if item_example is not None: | ||
| array_schema['example'] = [item_example] | ||
| return array_schema | ||
| return item | ||
|
|
||
|
|
||
|
|
@@ -114,5 +152,14 @@ | |
| if base_properties: | ||
| parts.append({'type': 'object', 'properties': base_properties}) | ||
| parts.extend(oneof_constraints) | ||
| components[name] = parts[0] if len(parts) == 1 else {'allOf': parts} | ||
| schema: dict[str, Any] = parts[0] if len(parts) == 1 else {'allOf': parts} | ||
| # Provide a single concrete example using the first oneof variant so Swagger | ||
| # doesn't expand every branch into separate array items. | ||
| first_oneof_field = real_oneofs[0].fields[0] | ||
| schema['example'] = { | ||
| first_oneof_field.name: first_oneof_field.name | ||
| if _is_required(first_oneof_field) | ||
| else '' | ||
| } | ||
|
martimfasantos marked this conversation as resolved.
Outdated
|
||
| components[name] = schema | ||
| return ref | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I'd just do
as fb:The entire
_proto_schema.pyis already marked as internal and also it's not an established practice in the repo.