Skip to content

feat: add XML serialization support for Pydantic models#5

Draft
mischadiehm wants to merge 1 commit into
pydantify:mainfrom
mischadiehm:feature/xml-serialization
Draft

feat: add XML serialization support for Pydantic models#5
mischadiehm wants to merge 1 commit into
pydantify:mainfrom
mischadiehm:feature/xml-serialization

Conversation

@mischadiehm
Copy link
Copy Markdown

Summary

This PR adds XML serialization capabilities to XMLPydantifyModel, enabling Pydantic models to be serialized to XML with proper namespace handling.

Changes

New Features

  • XMLPydantifyModel.model_dump_xml(): Recursive XML generation with:

    • Proper namespace declarations (only when namespace differs from parent)
    • Wrapper model passthrough for single-field models
    • List field expansion (each item becomes separate element)
    • Optional field handling (None values skipped)
  • model_dump_xml_string(): String serialization helper with:

    • Pretty-print support
    • Optional NETCONF <data> root wrapper

Exports

  • XMLPydantifyModel
  • model_dump_xml_string
  • NETCONF_BASE_NS

Testing

  • 16 new unit tests covering all features
  • 2 integration tests validating against expected XML output
  • All tests passing (18/18)
  • Linter passing

Files Changed

  • src/pydantify_common/model.py - Core serialization logic
  • src/pydantify_common/helper.py - String helper function
  • src/pydantify_common/__init__.py - Updated exports
  • tests/test_model_dump_xml.py - New unit tests
  • tests/test_examples.py - Updated integration tests

This PR was fully created by GitHub Copilot 🤖

- Add XMLPydantifyModel.model_dump_xml() for recursive XML generation
- Add model_dump_xml_string() helper with pretty-print and data root wrapper
- Export XMLPydantifyModel, model_dump_xml_string, NETCONF_BASE_NS
- Add comprehensive unit tests (16 test cases)
- Update integration tests for XML output validation

Co-authored-by: github-copilot[bot] <github-copilot[bot]@users.noreply.github.com>
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.

1 participant