|
| 1 | +# Experimental APIs |
| 2 | + |
| 3 | +The Microsoft.OpenApi library includes a set of experimental APIs that are available for evaluation. |
| 4 | +These APIs are subject to change or removal in future versions without following the usual deprecation process. |
| 5 | + |
| 6 | +Using an experimental API will produce a compiler diagnostic that must be explicitly suppressed |
| 7 | +to acknowledge the experimental nature of the API. |
| 8 | + |
| 9 | +## Suppressing Experimental API Diagnostics |
| 10 | + |
| 11 | +To use an experimental API, suppress the corresponding diagnostic in your project: |
| 12 | + |
| 13 | +### Per call site |
| 14 | + |
| 15 | +```csharp |
| 16 | +#pragma warning disable OAI020 |
| 17 | +var v2Path = OpenApiPathHelper.GetVersionedPath(path, OpenApiSpecVersion.OpenApi2_0); |
| 18 | +#pragma warning restore OAI020 |
| 19 | +``` |
| 20 | + |
| 21 | +### Per project (in `.csproj`) |
| 22 | + |
| 23 | +```xml |
| 24 | +<PropertyGroup> |
| 25 | + <NoWarn>$(NoWarn);OAI020</NoWarn> |
| 26 | +</PropertyGroup> |
| 27 | +``` |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## OAI020 — Path Version Conversion |
| 32 | + |
| 33 | +| Diagnostic ID | Applies to | Since | |
| 34 | +|---|---|---| |
| 35 | +| `OAI020` | `OpenApiPathHelper`, `OpenApiValidatorError.GetVersionedPointer` | v3.6.0 | |
| 36 | + |
| 37 | +### Overview |
| 38 | + |
| 39 | +The path version conversion APIs translate JSON Pointer paths produced by the `OpenApiWalker` |
| 40 | +(which always uses the v3 document model) into their equivalents for a specified OpenAPI |
| 41 | +specification version. |
| 42 | + |
| 43 | +This is useful when validation errors or walker paths need to be reported relative to |
| 44 | +the original document version (e.g., Swagger v2) rather than the internal v3 representation. |
| 45 | + |
| 46 | +### APIs |
| 47 | + |
| 48 | +#### `OpenApiPathHelper.GetVersionedPath(string path, OpenApiSpecVersion targetVersion)` |
| 49 | + |
| 50 | +Converts a v3-style JSON Pointer path to its equivalent for the target specification version. |
| 51 | + |
| 52 | +**Parameters:** |
| 53 | + |
| 54 | +- `path` — The v3-style JSON Pointer (e.g., `#/paths/~1items/get/responses/200/content/application~1json/schema`). |
| 55 | +- `targetVersion` — The target OpenAPI specification version. |
| 56 | + |
| 57 | +**Returns:** The equivalent path in the target version, the original path unchanged if no |
| 58 | +transformation is needed, or `null` if the construct has no equivalent in the target version. |
| 59 | + |
| 60 | +**Example:** |
| 61 | + |
| 62 | +```csharp |
| 63 | +#pragma warning disable OAI020 |
| 64 | +// v3 path from the walker |
| 65 | +var v3Path = "#/paths/~1items/get/responses/200/content/application~1octet-stream/schema"; |
| 66 | + |
| 67 | +// Convert to v2 equivalent |
| 68 | +var v2Path = OpenApiPathHelper.GetVersionedPath(v3Path, OpenApiSpecVersion.OpenApi2_0); |
| 69 | +// Result: "#/paths/~1items/get/responses/200/schema" |
| 70 | +
|
| 71 | +// Convert to v3.2 (no transformation needed) |
| 72 | +var v32Path = OpenApiPathHelper.GetVersionedPath(v3Path, OpenApiSpecVersion.OpenApi3_2); |
| 73 | +// Result: "#/paths/~1items/get/responses/200/content/application~1octet-stream/schema" |
| 74 | +
|
| 75 | +// v3-only construct with no v2 equivalent |
| 76 | +var serversPath = "#/servers/0"; |
| 77 | +var v2Result = OpenApiPathHelper.GetVersionedPath(serversPath, OpenApiSpecVersion.OpenApi2_0); |
| 78 | +// Result: null |
| 79 | +#pragma warning restore OAI020 |
| 80 | +``` |
| 81 | + |
| 82 | +#### `OpenApiValidatorError.GetVersionedPointer(OpenApiSpecVersion targetVersion)` |
| 83 | + |
| 84 | +A convenience method on validation errors that translates the error's `Pointer` property to |
| 85 | +the equivalent path for the target specification version. |
| 86 | + |
| 87 | +**Example:** |
| 88 | + |
| 89 | +```csharp |
| 90 | +var validator = new OpenApiValidator(ValidationRuleSet.GetDefaultRuleSet()); |
| 91 | +var walker = new OpenApiWalker(validator); |
| 92 | +walker.Walk(document); |
| 93 | + |
| 94 | +foreach (var error in validator.Errors) |
| 95 | +{ |
| 96 | + if (error is OpenApiValidatorError validatorError) |
| 97 | + { |
| 98 | +#pragma warning disable OAI020 |
| 99 | + var v2Pointer = validatorError.GetVersionedPointer(OpenApiSpecVersion.OpenApi2_0); |
| 100 | +#pragma warning restore OAI020 |
| 101 | + if (v2Pointer is not null) |
| 102 | + { |
| 103 | + Console.WriteLine($"Error at {v2Pointer}: {validatorError.Message}"); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +### Supported Transformations (v2 target) |
| 110 | + |
| 111 | +| v3 Path Pattern | v2 Equivalent | |
| 112 | +|---|---| |
| 113 | +| `#/components/schemas/{name}/**` | `#/definitions/{name}/**` | |
| 114 | +| `#/components/parameters/{name}/**` | `#/parameters/{name}/**` | |
| 115 | +| `#/components/responses/{name}/**` | `#/responses/{name}/**` | |
| 116 | +| `#/components/securitySchemes/{name}/**` | `#/securityDefinitions/{name}/**` | |
| 117 | +| `.../responses/{code}/content/{mediaType}/schema/**` | `.../responses/{code}/schema/**` | |
| 118 | +| `.../headers/{name}/schema/**` | `.../headers/{name}/**` | |
| 119 | + |
| 120 | +### Paths With No v2 Equivalent (returns `null`) |
| 121 | + |
| 122 | +- `#/servers/**` |
| 123 | +- `#/webhooks/**` |
| 124 | +- `.../callbacks/**` |
| 125 | +- `.../links/**` |
| 126 | +- `.../requestBody/**` |
| 127 | +- `.../content/{mediaType}/encoding/**` |
| 128 | +- `#/components/examples/**`, `#/components/headers/**`, `#/components/pathItems/**`, |
| 129 | + `#/components/links/**`, `#/components/callbacks/**`, `#/components/requestBodies/**`, |
| 130 | + `#/components/mediaTypes/**` |
| 131 | + |
| 132 | +### Why This Is Experimental |
| 133 | + |
| 134 | +The set of path transformations may evolve as edge cases are discovered and additional |
| 135 | +specification versions are released. The API surface and behavior may change in future versions |
| 136 | +based on community feedback. |
0 commit comments