@@ -246,3 +246,95 @@ def local_endpoint():
246246 assert body ["info" ]["title" ] == "Local API"
247247 assert "/local" in body ["paths" ]
248248 assert "/users" not in body ["paths" ]
249+
250+
251+ def test_openapi_merge_with_all_optional_fields ():
252+ # GIVEN an OpenAPIMerge with all optional config fields
253+ from aws_lambda_powertools .event_handler .openapi .models import (
254+ Contact ,
255+ ExternalDocumentation ,
256+ License ,
257+ Server ,
258+ Tag ,
259+ )
260+
261+ merge = OpenAPIMerge (
262+ title = "Full Config API" ,
263+ version = "1.0.0" ,
264+ summary = "API summary" ,
265+ description = "API description" ,
266+ terms_of_service = "https://example.com/tos" ,
267+ contact = Contact (name = "Support" , email = "support@example.com" ),
268+ license_info = License (name = "MIT" ),
269+ servers = [Server (url = "https://api.example.com" )],
270+ tags = [Tag (name = "users" , description = "User operations" ), "orders" ],
271+ security = [{"api_key" : []}],
272+ security_schemes = {"api_key" : {"type" : "apiKey" , "in" : "header" , "name" : "X-API-Key" }},
273+ external_documentation = ExternalDocumentation (url = "https://docs.example.com" ),
274+ openapi_extensions = {"x-custom" : "value" },
275+ )
276+ merge .discover (path = MERGE_HANDLERS_PATH , pattern = "**/users_handler.py" )
277+
278+ # WHEN getting schema
279+ schema = merge .get_openapi_schema ()
280+
281+ # THEN all optional fields should be present
282+ assert schema ["info" ]["summary" ] == "API summary"
283+ assert schema ["info" ]["description" ] == "API description"
284+ assert schema ["info" ]["termsOfService" ] == "https://example.com/tos"
285+ assert schema ["info" ]["contact" ]["name" ] == "Support"
286+ assert schema ["info" ]["license" ]["name" ] == "MIT"
287+ assert schema ["servers" ][0 ]["url" ] == "https://api.example.com"
288+ assert schema ["security" ] == [{"api_key" : []}]
289+ assert "api_key" in schema ["components" ]["securitySchemes" ]
290+ assert "https://docs.example.com" in str (schema ["externalDocs" ]["url" ])
291+ assert schema ["x-custom" ] == "value"
292+ # Tags should include both config tags and schema tags
293+ tag_names = [t ["name" ] for t in schema ["tags" ]]
294+ assert "users" in tag_names
295+ assert "orders" in tag_names
296+
297+
298+ def test_openapi_merge_add_file ():
299+ # GIVEN an OpenAPIMerge instance
300+ merge = OpenAPIMerge (title = "Add File API" , version = "1.0.0" )
301+
302+ # WHEN adding a file manually
303+ handler_path = MERGE_HANDLERS_PATH / "users_handler.py"
304+ merge .add_file (handler_path )
305+
306+ # THEN it should be in discovered files
307+ assert handler_path .resolve () in merge .discovered_files
308+
309+ # AND adding the same file again should not duplicate
310+ merge .add_file (handler_path )
311+ assert len ([f for f in merge .discovered_files if f .name == "users_handler.py" ]) == 1
312+
313+
314+ def test_openapi_merge_add_file_with_resolver_name ():
315+ # GIVEN an OpenAPIMerge instance
316+ merge = OpenAPIMerge (title = "Add File API" , version = "1.0.0" )
317+
318+ # WHEN adding a file with custom resolver name
319+ handler_path = MERGE_HANDLERS_PATH / "users_handler.py"
320+ merge .add_file (handler_path , resolver_name = "app" )
321+
322+ # THEN it should update the resolver name
323+ schema = merge .get_openapi_schema ()
324+ assert "/users" in schema ["paths" ]
325+
326+
327+ def test_openapi_merge_add_schema ():
328+ # GIVEN an OpenAPIMerge instance
329+ merge = OpenAPIMerge (title = "Add Schema API" , version = "1.0.0" )
330+
331+ # WHEN adding a schema manually
332+ merge .add_schema (
333+ {
334+ "paths" : {"/external" : {"get" : {"summary" : "External endpoint" }}},
335+ },
336+ )
337+
338+ # THEN it should be included in the merged schema
339+ schema = merge .get_openapi_schema ()
340+ assert "/external" in schema ["paths" ]
0 commit comments