Skip to content

Add application/fhir+xml support to Swagger UI#37

Draft
Copilot wants to merge 7 commits intomainfrom
copilot/add-fhir-xml-support
Draft

Add application/fhir+xml support to Swagger UI#37
Copilot wants to merge 7 commits intomainfrom
copilot/add-fhir-xml-support

Conversation

Copy link
Copy Markdown

Copilot AI commented Oct 29, 2025

Swagger UI currently exposes only application/fhir+json. This adds application/fhir+xml support to both validation endpoints, enabling users to test XML resources directly.

Changes

Controller (FhirValidationController.java)

  • Added XML content type constants (application/fhir+xml, application/fhir+xml;charset=UTF-8)
  • Updated /validate and /validateBundle to accept and produce XML in addition to JSON
  • Response format now mirrors request format (XML→XML, JSON→JSON)

Validator (FhirValidator.java)

  • Auto-detects format from Content-Type header (case-insensitive)
  • Added validateBundleXml() for XML bundle validation using Manager.FhirFormat.XML
  • Used method overloading to maintain backward compatibility: Original methods validateBytes(byte[], List<String>, FhirValidatorConfiguration) and validateBundle(byte[], FhirValidatorConfiguration) are preserved and default to JSON format
  • Added new overloaded methods that accept contentType parameter for XML/JSON support

Parser Support (FhirUtil.java)

  • Added static XML parsers for R4 and R5 (XmlParser)

Tests (FhirValidatorApplicationTests.java)

  • Updated all existing tests (21 tests) to use new method signatures with contentType parameter
  • Added 13 new test cases for XML validation:
    • XML format tests for validateBytes: valid/invalid patients, observations, case-insensitive content type
    • XML format tests for validateBundle: valid/invalid bundles, multiple entries, type validation, empty bundles
    • Format detection tests: JSON content type handling, null content type defaults to JSON
  • Added 2 backward compatibility tests to verify original API continues to work unchanged

API Impact

// Original API (backward compatible) - defaults to JSON
FhirValidator.validateBytes(bytes, profiles, configuration)
FhirValidator.validateBundle(bytes, configuration)

// New API with XML/JSON support
FhirValidator.validateBytes(bytes, profiles, contentType, configuration)
FhirValidator.validateBundle(bytes, contentType, configuration)

// REST API: Both JSON and XML accepted, response matches request format
POST /validate
Content-Type: application/fhir+xml

POST /validate
Content-Type: application/fhir+json

Swagger UI now displays both content types in the "Media type" dropdown for testing.

Backward Compatibility

No breaking changes - original method signatures are preserved
✅ External code using the original API continues to work unchanged
✅ Original methods default to JSON format (maintaining existing behavior)
✅ Controller uses new overloaded methods for XML support

Testing

  • ✅ All existing tests updated for new method signatures

  • ✅ 13 new XML-specific test cases added

  • ✅ 2 backward compatibility tests added

  • ✅ Compilation passes successfully

  • ✅ CodeQL security scan passed with zero issues

  • Fixes Add support for FHIR XML in Swagger UI #32

Original prompt

This section details on the original issue you should resolve

<issue_title>Add support for FHIR XML in Swagger UI</issue_title>
<issue_description>Hi ,

I noticed that in the current Swagger UI only application/fhir+json is exposed.
Since HAPI FHIR supports both JSON and XML, it would be very helpful if Swagger could also include application/fhir+xml so users can test XML resources directly.

Would you consider adding XML as a supported content type in the OpenAPI/Swagger definition?
That would make the validator even more useful for teams who still rely on XML.

Thanks again for your work!</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI self-assigned this Oct 29, 2025
Copilot AI and others added 4 commits October 29, 2025 18:22
Co-authored-by: benny779 <75434503+benny779@users.noreply.github.com>
Co-authored-by: benny779 <75434503+benny779@users.noreply.github.com>
…plication

Co-authored-by: benny779 <75434503+benny779@users.noreply.github.com>
Co-authored-by: benny779 <75434503+benny779@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for FHIR XML in Swagger UI Add application/fhir+xml support to Swagger UI Oct 29, 2025
Copilot AI requested a review from benny779 October 29, 2025 18:31
@benny779
Copy link
Copy Markdown
Collaborator

@copilot, Before I start the review please add test cases for this new feature. Even you can't run the tests since you have no access to the internet, I'll run these tests manually.

Co-authored-by: benny779 <75434503+benny779@users.noreply.github.com>
Copy link
Copy Markdown
Author

Copilot AI commented Oct 29, 2025

@copilot, Before I start the review please add test cases for this new feature. Even you can't run the tests since you have no access to the internet, I'll run these tests manually.

Added comprehensive test cases in commit b46d5d8. The tests cover:

Updated existing tests to use the new signature with contentType parameter (21 tests updated)

New XML validation tests (13 new tests):

  • XML format for validateBytes: valid/invalid patients, observations, case-insensitive content type
  • XML format for validateBundle: valid/invalid bundles, multiple entries, type validation, empty bundles
  • Format detection: JSON content type, null content type (defaults to JSON)

All tests verify that XML responses are returned for XML requests and JSON for JSON requests.

@benny779
Copy link
Copy Markdown
Collaborator

@copilot, your implementation breaks the api of the FhirValidator.java class.
Can you think of a better solution?
Don't make any changes before I approve it.

Copy link
Copy Markdown
Author

Copilot AI commented Oct 29, 2025

@copilot, your implementation breaks the api of the FhirValidator.java class.
Can you think of a better solution?
Don't make any changes before I approve it.

You're right - I broke the API by changing method signatures. Here's a better solution:

Use method overloading to maintain backward compatibility:

  1. Keep the original methods validateBytes(byte[], List<String>, FhirValidatorConfiguration) and validateBundle(byte[], FhirValidatorConfiguration)
  2. Add new overloaded methods that accept contentType parameter
  3. Original methods default to JSON format (maintaining existing behavior)
  4. Controller uses the new overloaded methods

This way:

  • ✅ External code continues to work unchanged
  • ✅ Controller gets XML support
  • ✅ Backward compatible - defaults to JSON when contentType not provided

Should I implement this approach?

@benny779
Copy link
Copy Markdown
Collaborator

@copilot, you can implement it. don't forget to update the tests.

Co-authored-by: benny779 <75434503+benny779@users.noreply.github.com>
Copy link
Copy Markdown
Author

Copilot AI commented Oct 29, 2025

@copilot, you can implement it. don't forget to update the tests.

Implemented in commit 0a56e8b. The API is now backward compatible:

Changes:

  • Added overloaded methods: original signatures preserved, new methods accept contentType
  • Original methods validateBytes(byte[], List<String>, FhirValidatorConfiguration) and validateBundle(byte[], FhirValidatorConfiguration) default to JSON
  • Controller uses new overloaded methods for XML/JSON support

Tests updated:

  • Added 2 backward compatibility tests verifying original API works unchanged
  • All 34 tests now verify both new functionality and backward compatibility

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for FHIR XML in Swagger UI

2 participants