8000 fix(http): tag update by deepankarm · Pull Request #3108 · jina-ai/serve · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix(http): tag update #3108

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

Merged
merged 1 commit into from
Aug 5, 2021
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
8000
Diff view
16 changes: 11 additions & 5 deletions jina/peapods/runtimes/gateway/http/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ def tags_updater(cls, values):
extra_fields = {k: values[k] for k in set(values).difference(cls.__fields__)}
if extra_fields:
if 'tags' not in values:
values['tags'] = cls.__fields__['tags'].default
values['tags'] = {}
if isinstance(values['tags'], Dict):
values['tags'].update({i: j for i, j in extra_fields.items()})
values['tags'].update(extra_fields)
return values

return root_validator(pre=True, allow_reuse=True)(tags_updater)
Expand Down Expand Up @@ -152,6 +152,7 @@ def protobuf_to_pydantic_model(

field_type = PROTOBUF_TO_PYTHON_TYPE[f.type]
default_value = f.default_value
default_factory = None

if f.containing_oneof:
# Proto Field type: oneof
Expand All @@ -169,11 +170,11 @@ def protobuf_to_pydantic_model(
if f.message_type.name == 'Struct':
# Proto Field Type: google.protobuf.Struct
field_type = Dict
default_value = {}
default_factory = dict
elif f.message_type.name == 'Timestamp':
# Proto Field Type: google.protobuf.Timestamp
field_type = datetime
default_value = datetime.now()
default_factory = datetime.now
else:
# Proto field type: Proto message defined in jina.proto
if f.message_type.name == model_name:
Expand All @@ -187,7 +188,12 @@ def protobuf_to_pydantic_model(
if f.label == FieldDescriptor.LABEL_REPEATED:
field_type = List[field_type]

all_fields[field_name] = (field_type, Field(default=default_value))
all_fields[field_name] = (
field_type,
Field(default_factory=default_factory)
if default_factory
else Field(default=default_value),
)

# some fixes on Doc.scores and Doc.evaluations
if field_name in ('scores', 'evaluations'):
Expand Down
2 changes: 0 additions & 2 deletions tests/unit/flow/test_flow_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def validate(req):
assert len(chunk_ids) == 80


# TODO(Deepankar): Gets stuck when `restful: True` - issues with `needs='gateway'`
@pytest.mark.skip(
'this should fail as explained in https://github.com/jina-ai/jina/pull/730'
)
Expand All @@ -56,7 +55,6 @@ def test_this_will_fail(mocker, protocol):
validate_callback(response_mock, validate)


# TODO(Deepankar): Gets stuck when `restful: True` - issues with `needs='gateway'`
@pytest.mark.timeout(180)
@pytest.mark.parametrize('protocol', ['websocket', 'grpc', 'http'])
def test_this_should_work(mocker, protocol):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pytest
import requests as req
from fastapi.testclient import TestClient

from jina import Executor, requests, Flow, DocumentArray
from jina.logging.logger import JinaLogger
from jina.parsers import set_gateway_parser
from jina.peapods.runtimes.gateway.http import get_fastapi_app
Expand All @@ -17,3 +19,27 @@ def test_custom_swagger(p):
with TestClient(app) as client:
assert any('/docs' in r.path for r in app.routes)
assert any('/openapi.json' in r.path for r in app.routes)


class TestExecutor(Executor):
@requests
def empty(self, docs: 'DocumentArray', **kwargs):
print(f"# docs {docs}")


def test_tag_update():
PORT_EXPOSE = 33300

f = Flow(
port_expose=PORT_EXPOSE,
restful=True,
protocol='http',
).add(uses=TestExecutor)

with f:
d1 = {"data": [{"id": "1", "prop1": "val"}]}
d2 = {"data": [{"id": "2", "prop2": "val"}]}
r1 = req.post(f'http://localhost:{PORT_EXPOSE}/index', json=d1)
assert r1.json()['data']['docs'][0]['tags'] == {'prop1': 'val'}
r2 = req.post(f'http://localhost:{PORT_EXPOSE}/index', json=d2)
assert r2.json()['data']['docs'][0]['tags'] == {'prop2': 'val'}
0