|
| 1 | +# VS Code Configuration for APIM Policy Development |
| 2 | + |
| 3 | +This document explains the VS Code configuration optimized for working with Azure API Management (APIM) policy files. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +APIM policies are XML files that often contain C# expressions, making them challenging to validate with standard XML tools. This configuration provides the best balance of helpful features while avoiding false validation errors. |
| 8 | + |
| 9 | +## Extensions Required |
| 10 | + |
| 11 | +The following extensions are required for optimal APIM policy development: |
| 12 | + |
| 13 | +1. **Azure API Management** (`ms-azuretools.vscode-apimanagement`) - Provides APIM-specific features |
| 14 | +2. **XML** (`redhat.vscode-xml`) - XML language support with formatting and basic validation |
| 15 | +3. **Auto Close Tag** (`formulahendry.auto-close-tag`) - Automatically closes XML tags |
| 16 | +4. **Auto Rename Tag** (`formulahendry.auto-rename-tag`) - Renames paired XML tags |
| 17 | + |
| 18 | +## Key Configuration Settings |
| 19 | + |
| 20 | +### XML Validation |
| 21 | +```json |
| 22 | +"xml.validation.enabled": true, |
| 23 | +"xml.validation.namespaces.enabled": "never", |
| 24 | +"xml.validation.schema.enabled": "never" |
| 25 | +``` |
| 26 | +- Enables basic XML syntax validation but disables strict schema and namespace validation |
| 27 | +- Prevents false errors on APIM-specific elements and C# expressions |
| 28 | + |
| 29 | +### File Associations |
| 30 | +```json |
| 31 | +"files.associations": { |
| 32 | + "*.xml": "xml", |
| 33 | + "**/apim-policies/*.xml": "xml", |
| 34 | + "**/samples/**/*.xml": "xml", |
| 35 | + "pf-*.xml": "xml", |
| 36 | + "hr_*.xml": "xml" |
| 37 | +} |
| 38 | +``` |
| 39 | +- Ensures all APIM policy files are treated as XML |
| 40 | +- Covers policy fragments (pf-*.xml) and sample files |
| 41 | + |
| 42 | +### Validation Filters |
| 43 | +```json |
| 44 | +"xml.validation.filters": [ |
| 45 | + { |
| 46 | + "pattern": "**/apim-policies/*.xml", |
| 47 | + "noGrammar": "ignore" |
| 48 | + }, |
| 49 | + { |
| 50 | + "pattern": "**/samples/**/*.xml", |
| 51 | + "noGrammar": "ignore" |
| 52 | + } |
| 53 | +] |
| 54 | +``` |
| 55 | +- Suppresses "no grammar" warnings for APIM policy files |
| 56 | +- Allows VS Code to provide formatting and basic syntax checking without schema validation errors |
| 57 | + |
| 58 | +### APIM-Specific Settings |
| 59 | +```json |
| 60 | +"azureApiManagement.policies.validateSyntax": true, |
| 61 | +"azureApiManagement.policies.showCodeLens": true |
| 62 | +``` |
| 63 | +- Enables APIM extension's built-in policy syntax validation |
| 64 | +- Shows code lens information for policy elements |
| 65 | + |
| 66 | +## Features Enabled |
| 67 | + |
| 68 | +1. **XML Formatting**: Automatic formatting with proper indentation |
| 69 | +2. **Tag Auto-completion**: Auto-closing and auto-renaming of XML tags |
| 70 | +3. **Syntax Highlighting**: Full XML syntax highlighting |
| 71 | +4. **APIM Policy Validation**: Basic validation through the APIM extension |
| 72 | +5. **Quick Suggestions**: Intelligent suggestions for XML content |
| 73 | +6. **Bracket Matching**: Automatic closing of brackets and quotes |
| 74 | + |
| 75 | +## Working with C# Expressions |
| 76 | + |
| 77 | +APIM policies often contain C# expressions within XML attributes and content. The configuration: |
| 78 | + |
| 79 | +- Allows C# code within XML without validation errors |
| 80 | +- Preserves formatting of C# expressions |
| 81 | +- Provides basic bracket matching for C# code |
| 82 | + |
| 83 | +Example C# expression in APIM policy: |
| 84 | +```xml |
| 85 | +<set-variable name="authz_roles" value="{{HRAdministratorRoleId}},{{HRAssociateRoleId}}" /> |
| 86 | +<when condition="@(((Jwt)context.Variables["jwt"]).Claims["roles"].Contains(context.Variables["authz_roles"]))"> |
| 87 | +``` |
| 88 | + |
| 89 | +## Common APIM Policy Elements |
| 90 | + |
| 91 | +The configuration recognizes and provides support for common APIM policy elements: |
| 92 | + |
| 93 | +- `<policies>`, `<inbound>`, `<outbound>`, `<backend>`, `<on-error>` |
| 94 | +- `<set-variable>`, `<set-header>`, `<set-body>` |
| 95 | +- `<validate-jwt>`, `<cors>`, `<rate-limit>` |
| 96 | +- `<choose>`, `<when>`, `<otherwise>` |
| 97 | +- `<return-response>`, `<forward-request>` |
| 98 | +- `<include-fragment>` for policy fragments |
| 99 | + |
| 100 | +## Troubleshooting |
| 101 | + |
| 102 | +### Schema Validation Errors |
| 103 | +If you see schema validation errors: |
| 104 | +1. Check that `xml.validation.schema.enabled` is set to `"never"` |
| 105 | +2. Verify the validation filters are correctly configured |
| 106 | +3. Ensure the APIM extension is installed and enabled |
| 107 | + |
| 108 | +### Missing Intellisense |
| 109 | +If you're not getting XML completion: |
| 110 | +1. Verify the file is recognized as XML (check the language mode in the status bar) |
| 111 | +2. Ensure `xml.completion.autoCloseTags` is enabled |
| 112 | +3. Check that the file associations are correctly configured |
| 113 | + |
| 114 | +### C# Expression Errors |
| 115 | +If C# expressions in policies show errors: |
| 116 | +- This is normal - VS Code cannot fully validate C# within XML context |
| 117 | +- The APIM extension provides the actual validation |
| 118 | +- Use the APIM test console for runtime validation |
| 119 | + |
| 120 | +## Best Practices |
| 121 | + |
| 122 | +1. **Use Policy Fragments**: Create reusable policy fragments for common patterns |
| 123 | +2. **Test in APIM**: Always test policies in the actual APIM instance |
| 124 | +3. **Format Regularly**: Use VS Code's format document command (`Shift+Alt+F`) |
| 125 | +4. **Use Named Values**: Reference configuration through APIM named values |
| 126 | +5. **Comment Policies**: Use XML comments to document complex policy logic |
| 127 | + |
| 128 | +## Files Created |
| 129 | + |
| 130 | +- `.vscode/settings.json` - Main VS Code configuration |
| 131 | +- `.vscode/extensions.json` - Recommended extensions |
| 132 | +- `.vscode/apim-policy.xsd` - Basic XSD schema for APIM policies (reference only) |
| 133 | +- `.vscode/xml-catalog.xml` - XML catalog for schema resolution (if needed) |
| 134 | + |
| 135 | +This configuration provides an optimal development experience for APIM policies while avoiding the common validation errors that occur when using generic XML tools with APIM-specific content. |
0 commit comments