Skip to content

Commit 05825d1

Browse files
committed
test: consolidate schema generation test in capture_log block
Move all schema generation and verification into the capture_log block to ensure any warnings during the process are captured for verification. This provides better test coverage for the recursion detection logging.
1 parent 6fb9210 commit 05825d1

File tree

1 file changed

+38
-41
lines changed

1 file changed

+38
-41
lines changed

test/acceptance/json_schema_test.exs

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -332,44 +332,54 @@ defmodule Test.Acceptance.JsonSchemaTest do
332332
end
333333
end
334334

335-
# Generate spec outside capture_log first to check it
336-
spec = AshJsonApi.OpenApi.spec(domain: [RecursiveInputTest.BlogDomain])
335+
log =
336+
capture_log(fn ->
337+
# Generate spec outside capture_log first to check it
338+
spec = AshJsonApi.OpenApi.spec(domain: [RecursiveInputTest.BlogDomain])
337339

338-
# Verify spec generation completes without stack overflow
339-
assert %OpenApiSpex.OpenApi{} = spec
340+
# Verify spec generation completes without stack overflow
341+
assert %OpenApiSpex.OpenApi{} = spec
340342

341-
# Check that the create operation request body is properly generated
342-
create_path = spec.paths["/articles"]
343-
assert create_path != nil
343+
# Check that the create operation request body is properly generated
344+
create_path = spec.paths["/articles"]
345+
assert create_path != nil
344346

345-
create_op = create_path.post
346-
assert create_op != nil
347+
create_op = create_path.post
348+
assert create_op != nil
347349

348-
# Verify the request body schema exists
349-
assert create_op.requestBody != nil
350+
# Verify the request body schema exists
351+
assert create_op.requestBody != nil
350352

351-
# The key test is that we got here without stack overflow
352-
# Additional checks to verify the schemas are properly structured
353-
schemas = spec.components.schemas
353+
# The key test is that we got here without stack overflow
354+
# Additional checks to verify the schemas are properly structured
355+
schemas = spec.components.schemas
354356

355-
# Verify the main resource schema exists
356-
assert Map.has_key?(schemas, "article-with-comments")
357+
# Verify the main resource schema exists
358+
assert Map.has_key?(schemas, "article-with-comments")
357359

358-
# Verify the embedded recursive-comment schema exists
359-
assert Map.has_key?(schemas, "recursive-comment")
360+
# Verify the embedded recursive-comment schema exists
361+
assert Map.has_key?(schemas, "recursive-comment")
360362

361-
# Check patch operation as well
362-
patch_path = spec.paths["/articles/{id}"]
363-
assert patch_path != nil
363+
# Check patch operation as well
364+
patch_path = spec.paths["/articles/{id}"]
365+
assert patch_path != nil
364366

365-
patch_op = patch_path.patch
366-
assert patch_op != nil
367-
assert patch_op.requestBody != nil
367+
patch_op = patch_path.patch
368+
assert patch_op != nil
369+
assert patch_op.requestBody != nil
368370

369-
# Now capture logs to verify warnings
370-
log =
371-
capture_log(fn ->
372-
AshJsonApi.OpenApi.spec(domain: [RecursiveInputTest.BlogDomain])
371+
# Verify that any referenced schemas exist in components
372+
# This ensures client generation tools won't fail with missing $ref errors
373+
schema_keys = Map.keys(schemas)
374+
375+
# If there are any input schemas, they should be properly defined
376+
input_schemas = Enum.filter(schema_keys, &String.contains?(&1, "-input-"))
377+
378+
Enum.each(input_schemas, fn schema_name ->
379+
schema = Map.get(schemas, schema_name)
380+
assert schema != nil, "Schema #{schema_name} should be defined"
381+
assert schema.type == :object, "Schema #{schema_name} should be an object type"
382+
end)
373383
end)
374384

375385
# Additionally verify that warnings were logged for recursive input types
@@ -378,19 +388,6 @@ defmodule Test.Acceptance.JsonSchemaTest do
378388
assert log =~ "recursive" or log =~ "Recursive",
379389
"Expected some indication of recursive type handling in logs"
380390
end
381-
382-
# Verify that any referenced schemas exist in components
383-
# This ensures client generation tools won't fail with missing $ref errors
384-
schema_keys = Map.keys(schemas)
385-
386-
# If there are any input schemas, they should be properly defined
387-
input_schemas = Enum.filter(schema_keys, &String.contains?(&1, "-input-"))
388-
389-
Enum.each(input_schemas, fn schema_name ->
390-
schema = Map.get(schemas, schema_name)
391-
assert schema != nil, "Schema #{schema_name} should be defined"
392-
assert schema.type == :object, "Schema #{schema_name} should be an object type"
393-
end)
394391
end
395392
end
396393
end

0 commit comments

Comments
 (0)