Skip to content

Conversation

FilPag
Copy link
Contributor

@FilPag FilPag commented Aug 12, 2025

Policy Fragments Implementation

Updated [Document] attribute to include type parameter for distinguishing between policy documents and policy fragments.

Changes

  • type: DocumentType.Fragment - Creates policy file with <fragment> as root tag
  • type: DocumentType.Policy - Creates policy file with <policies> as root tag (default if no parameter is defined)

Example Usage

[Document("authentication-fragment", type: DocumentType.Fragment)]
public class AuthenticationFragment : IFragment
{
    public void Fragment(IFragmentContext context)
    {
        context.SetHeader("X-Auth-Fragment", "true");
        context.AuthenticationBasic("{{username}}", "{{password}}");
    }
}

Compilation Output

Fragment (DocumentType.Fragment):

<fragment>
  <set-header name="X-Auth-Fragment" exists-action="override">
    <value>true</value>
  </set-header>
  <authentication-basic username="{{username}}" password="{{password}}" />
</fragment>

Policy (DocumentType.Policy - default):

<policies>
  <inbound>
    <!-- policies here -->
  </inbound>
</policies>

Copy link
Collaborator

@Mielek Mielek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FilPag Thank you so much for this contribution. We appreciate your time and effort doing that.

Change looks really nice. It is as well prepared for the emulator which we plan on doing. I think with minor change which I posted in one of the file we can merge it. I think this change would make tests pass.

Would you mind creating some example Fragment in the example project? It is not a blocker but It would allow other folks which want to only use the library easy way of seeing how to create a fragment.

Note for future me. It is not needed here. Adding IncludeFragment override method in interfaces (eg context.InlineFragment() or context.InlineFragment(new SomeFragment())) and handling it in compiler would allow easy way to include fragments in policies without remembering string names of the fragment.

@FilPag FilPag requested a review from Mielek August 14, 2025 13:46
Copy link
Collaborator

@Mielek Mielek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for your contribution!

@Mielek Mielek merged commit 7322537 into Azure:main Aug 14, 2025
7 checks passed
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.

2 participants