Skip to content

Commit be70904

Browse files
authored
Merge pull request #676 from aiven/eliax1996/add-subjects-only-with-header-specified
2 parents ca96e88 + 7c510c7 commit be70904

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

karapace/schema_registry_apis.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ async def schemas_get(
472472
status=HTTPStatus.NOT_FOUND,
473473
)
474474

475+
include_subjects = request.query.get("includeSubjects", "false").lower() == "true"
475476
fetch_max_id = request.query.get("fetchMaxId", "false").lower() == "true"
476477
schema = self.schema_registry.schemas_get(parsed_schema_id, fetch_max_id=fetch_max_id)
477478

@@ -505,9 +506,11 @@ def _has_subject_with_id() -> bool:
505506
status=HTTPStatus.NOT_FOUND,
506507
)
507508

508-
subjects = self.schema_registry.database.subjects_for_schema(parsed_schema_id)
509+
response_body = {"schema": schema.schema_str}
510+
511+
if include_subjects:
512+
response_body["subjects"] = self.schema_registry.database.subjects_for_schema(parsed_schema_id)
509513

510-
response_body = {"schema": schema.schema_str, "subjects": subjects}
511514
if schema.schema_type is not SchemaType.AVRO:
512515
response_body["schemaType"] = schema.schema_type
513516
if schema.references:

karapace/serialization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ async def get_latest_schema(self, subject: str) -> Tuple[SchemaId, ParsedTypedSc
115115
raise SchemaRetrievalError(f"Failed to parse schema string from response: {json_result}") from e
116116

117117
async def get_schema_for_id(self, schema_id: SchemaId) -> Tuple[TypedSchema, List[Subject]]:
118-
result = await self.client.get(f"schemas/ids/{schema_id}")
118+
result = await self.client.get(f"schemas/ids/{schema_id}", params={"includeSubjects": "True"})
119119
if not result.ok:
120120
raise SchemaRetrievalError(result.json()["message"])
121121
json_result = result.json()

tests/integration/test_schema.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ async def test_union_comparing_to_other_types(registry_async_client: Client) ->
962962
res = await registry_async_client.post(f"subjects/{subject}/versions", json={"schema": json.dumps(plain_schema)})
963963
assert res.status_code == status_code
964964

965-
res = await registry_async_client.get(f"/schemas/ids/{initial_schema_id}")
965+
res = await registry_async_client.get(f"/schemas/ids/{initial_schema_id}", params={"includeSubjects": "True"})
966966
assert subject in res.json()["subjects"]
967967

968968
expected_results = [("BACKWARD", 200), ("FORWARD", 409), ("FULL", 409)]
@@ -2352,9 +2352,16 @@ async def test_malformed_kafka_message(
23522352
sleep=1,
23532353
)
23542354
res_data = res.json()
2355-
expected_payload = {"schema": json_encode({"foo": "bar"}, compact=True), "subjects": ["foo"]}
2355+
expected_payload = {"schema": json_encode({"foo": "bar"}, compact=True)}
23562356
assert res_data == expected_payload, res_data
23572357

2358+
with_subjects_reply = await registry_async_client.get(path, params={"includeSubjects": "True"})
2359+
assert with_subjects_reply.ok, "a subsequent request once the server is up should be valid"
2360+
json_reply = with_subjects_reply.json()
2361+
assert "subjects" in json_reply, "subjects should be present if specified"
2362+
expected_payload["subjects"] = ["foo"]
2363+
assert expected_payload == json_reply, "the reply should be equal as the previous one with additional subjects field"
2364+
23582365

23592366
async def test_inner_type_compat_failure(registry_async_client: Client) -> None:
23602367
subject = create_subject_name_factory("test_inner_type_compat_failure")()

0 commit comments

Comments
 (0)