Skip to content

Revert "Update Microsoft.OpenApi to version 2 (#1766)"#1785

Merged
ivarne merged 1 commit into
mainfrom
revert-openapi-v2
May 29, 2026
Merged

Revert "Update Microsoft.OpenApi to version 2 (#1766)"#1785
ivarne merged 1 commit into
mainfrom
revert-openapi-v2

Conversation

@ivarne
Copy link
Copy Markdown
Member

@ivarne ivarne commented May 29, 2026

This reverts commit d0ed567.

It requires changes to Program.cs with makes updating apps harder

Summary by CodeRabbit

  • New Features

    • Added OpenAPI readers support for improved schema parsing and validation.
  • Bug Fixes

    • Simplified error response definitions in OpenAPI specifications for better consistency.
  • Chores

    • Updated NuGet dependencies: downgraded Swashbuckle.AspNetCore and Microsoft.OpenApi to stable versions.
    • Updated namespace references across configuration files for compatibility.

Review Change Stack

Copilot AI review requested due to automatic review settings May 29, 2026 11:50
@ivarne ivarne added the feature Label Pull requests with new features. Used when generation releasenotes label May 29, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Reverts the previous Microsoft.OpenApi v2 upgrade by downgrading OpenAPI/Swashbuckle dependencies and adjusting OpenAPI generation + tests to match the older Microsoft.OpenApi APIs and serialization behavior.

Changes:

  • Downgrade Microsoft.OpenApi (and Swashbuckle.AspNetCore) and add Microsoft.OpenApi.Readers where needed.
  • Update OpenAPI document generation/serialization to use the v1.x reader/serializer APIs.
  • Update OpenAPI snapshot tests and public API baselines to match the reverted dependency behavior.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
Directory.Packages.props Downgrades Microsoft.OpenApi and Swashbuckle versions; adds Microsoft.OpenApi.Readers version.
src/Altinn.App.Api/Controllers/CustomOpenApiController.cs Refactors OpenAPI document construction/serialization to work with Microsoft.OpenApi v1.x.
src/Altinn.App.Api/Extensions/SwaggerFilterExtensions.cs Updates OpenAPI namespace import to Microsoft.OpenApi.Models.
test/Altinn.App.Api.Tests/Altinn.App.Api.Tests.csproj Adds Microsoft.OpenApi.Readers to support parsing OpenAPI docs in tests.
test/Altinn.App.Api.Tests/OpenApi/OpenApiSpecChangeDetection.cs Updates OpenAPI parsing/serialization to v1.x APIs and aligns output with controller settings.
test/Altinn.App.Api.Tests/OpenApi/OpenApiSpecChangeDetection.SaveCustomOpenApiSpec.verified.json Updates verified OpenAPI output snapshot for reverted libraries.
test/Altinn.App.Api.Tests/OpenApi/OpenApiSpecChangeDetection.SaveJsonSwagger.verified.json Updates verified OpenAPI output snapshot for reverted libraries.
test/Altinn.App.Api.Tests/Program.cs Updates OpenAPI namespace import to Microsoft.OpenApi.Models.
test/Altinn.App.Api.Tests/PublicApiTests.PublicApi_ShouldNotChange_Unintentionally.verified.txt Updates public API baseline to reflect current build output.
test/Altinn.App.Api.Tests/Data/apps/tdd/contributer-restriction/config/applicationmetadata.json Adjusts test app metadata used by OpenAPI generation tests.
test/Altinn.App.Analyzers.Tests/testapp/App/Program.cs Updates OpenAPI namespace import to Microsoft.OpenApi.Models.
test/Altinn.App.Integration.Tests/_testapps/basic/App/Program.cs Updates OpenAPI namespace import to Microsoft.OpenApi.Models.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Altinn.App.Api/Controllers/CustomOpenApiController.cs
Comment thread src/Altinn.App.Api/Controllers/CustomOpenApiController.cs
Comment thread src/Altinn.App.Api/Controllers/CustomOpenApiController.cs
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 29, 2026

📝 Walkthrough

Walkthrough

This PR downgrades Microsoft.OpenApi from 2.7.4 to 1.6.29 and Swashbuckle.AspNetCore from 10.1.7 to 9.0.6, requiring extensive refactoring of the OpenAPI document generation in CustomOpenApiController to adapt to the older library's API surface and serialization methods.

Changes

OpenAPI Model Version Downgrade and API Adaptation

Layer / File(s) Summary
Central package version updates
Directory.Packages.props
Downgrade Microsoft.OpenApi to 1.6.29, add Microsoft.OpenApi.Readers at 1.6.29, and downgrade Swashbuckle.AspNetCore to 9.0.6.
CustomOpenApiController API adaptation
src/Altinn.App.Api/Controllers/CustomOpenApiController.cs
Refactor document setup to populate components directly, change parameter/response reference types from *Reference classes to OpenApiParameter/OpenApiReference, switch serialization to document.Serialize(SpecFormat.Json), and convert all endpoint operations from Dictionary<HttpMethod,...> to OperationType-indexed patterns with inline object initialization. Make SchemaPostVisitor public with schema post-processing for altinnRowId removal, XML clearing, and id as required field.
OpenAPI namespace import alignment
src/Altinn.App.Api/Extensions/SwaggerFilterExtensions.cs, test/Altinn.App.Analyzers.Tests/testapp/App/Program.cs, test/Altinn.App.Api.Tests/Program.cs, test/Altinn.App.Integration.Tests/_testapps/basic/App/Program.cs
Update using directives from Microsoft.OpenApi to Microsoft.OpenApi.Models across configuration and test application files.
Test project OpenAPI.Readers dependency
test/Altinn.App.Api.Tests/Altinn.App.Api.Tests.csproj
Add Microsoft.OpenApi.Readers package reference to support document reading in snapshot tests.
Test snapshot reading and serialization update
test/Altinn.App.Api.Tests/OpenApi/OpenApiSpecChangeDetection.cs
Replace OpenApiDocument.LoadAsync() with OpenApiStreamReader.Read(), change snapshot serialization to document.Serialize(CustomOpenApiController.SpecVersion, CustomOpenApiController.SpecFormat), and update using directives.
Verified OpenAPI spec snapshots and test data
test/Altinn.App.Api.Tests/Data/apps/tdd/contributer-restriction/config/applicationmetadata.json, test/Altinn.App.Api.Tests/OpenApi/OpenApiSpecChangeDetection.SaveCustomOpenApiSpec.verified.json, test/Altinn.App.Api.Tests/OpenApi/OpenApiSpecChangeDetection.SaveJsonSwagger.verified.json, test/Altinn.App.Api.Tests/PublicApiTests.PublicApi_ShouldNotChange_Unintentionally.verified.txt
Update verified snapshots to reflect refactored controller: simplify parameters and responses to use component references, standardize endpoint descriptions, remove attachment schema type: string fields, remove document-level tags array, and remove test metadata descriptions. Update public API surface to include new SchemaPostVisitor and Snippets types.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • Altinn/app-lib-dotnet#1766: Adapts the same OpenAPI implementation code—CustomOpenApiController, Snippets, and SchemaPostVisitor—to work with Microsoft.OpenApi library version changes.

Suggested labels

dependency

Suggested reviewers

  • HauklandJ
  • martinothamar
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 30.77% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title accurately describes the main change: reverting a previous update to Microsoft.OpenApi version 2, which aligns with the actual changes shown in the raw_summary.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch revert-openapi-v2

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
6.0% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 5

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/Altinn.App.Api/Controllers/CustomOpenApiController.cs (1)

283-289: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Use a valid Encoding.ContentType value (comma-separated MIME types).

CustomOpenApiController builds OpenApiEncoding.ContentType with string.Join(' ', contentTypes), producing values like "image/png image/jpeg". OpenAPI 3.0 expects a single media type or a comma-separated list (e.g., "image/png, image/jpeg"), so multipart encoding becomes malformed when multiple content types are allowed.

Suggested fix
-                        ? string.Join(' ', contentTypes)
+                        ? string.Join(", ", contentTypes)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/Altinn.App.Api/Controllers/CustomOpenApiController.cs` around lines 283 -
289, CustomOpenApiController currently builds OpenApiEncoding.ContentType using
string.Join(' ', contentTypes) which yields space-separated MIME types like
"image/png image/jpeg" and is invalid; change the construction in the
multipartMediaType.Encoding.Add call (referencing dataType.Id,
OpenApiEncoding.ContentType and dataType.AllowedContentTypes) to join allowed
content types with a comma separator (e.g., string.Join(", ", contentTypes)) and
still fall back to "application/octet-stream" when AllowedContentTypes is empty
so ContentType is a valid comma-separated list or a single media type.
🧹 Nitpick comments (1)
test/Altinn.App.Api.Tests/OpenApi/OpenApiSpecChangeDetection.cs (1)

43-50: 💤 Low value

Consider enabling diagnostic error assertion or removing the commented line.

The commented-out Assert.Empty(diagnostic.Errors) line could hide parsing issues during test runs. If parsing errors are expected due to library differences, document why; otherwise, consider uncommenting to catch regressions.

Suggested change
         var reader = new OpenApiStreamReader();
         OpenApiDocument document = reader.Read(stream, out OpenApiDiagnostic diagnostic);
-        // Assert.Empty(diagnostic.Errors);
+        Assert.Empty(diagnostic.Errors);
         document.Info.Version = "";
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/Altinn.App.Api.Tests/OpenApi/OpenApiSpecChangeDetection.cs` around lines
43 - 50, Uncomment or remove the commented Assert.Empty(diagnostic.Errors) in
the OpenApi parsing test so parsing issues are not silently ignored: in the
OpenApiSpecChangeDetection test, after using OpenApiStreamReader and obtaining
OpenApiDocument and the diagnostic object, either restore
Assert.Empty(diagnostic.Errors) to fail on unexpected parse errors or replace it
with a clear assertion/guard that documents expected errors (e.g.,
Assert.Equal(expectedCount, diagnostic.Errors.Count) or a comment explaining why
errors are allowed) before proceeding to document.Info.Version = "" and calling
VerifyJson(document.Serialize(CustomOpenApiController.SpecVersion,
CustomOpenApiController.SpecFormat), _verifySettings).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/Altinn.App.Api/Controllers/CustomOpenApiController.cs`:
- Line 1284: The types Snippets and SchemaPostVisitor are declared public but
are internal implementation helpers; change their accessibility from public to
internal to avoid expanding the assembly API surface. Locate the type
declarations named Snippets (the static helper class) and SchemaPostVisitor (the
visitor class) and replace the public modifier with internal for each, ensuring
any usages within the same file/assembly still compile and no external code
relies on them.
- Around line 1201-1206: The OpenAPI schema for the application/octet-stream
media type currently sets only Format = "binary" which must be paired with Type
= "string"; locate the OpenApiMediaType initialization where Schema = new
OpenApiSchema() { Format = "binary" } in CustomOpenApiController (the
application/octet-stream block) and add Type = "string" to that OpenApiSchema
instance so the schema becomes explicitly type string with format binary.
- Around line 328-333: Remove the bogus content entry that adds ["empty"] = new
OpenApiMediaType(...) in CustomOpenApiController (the OpenAPI request body
builder code), since RequestBody.Required = false already marks the body
optional and "empty" is not a valid media type and schema.type = "null" is
unsupported; delete that dictionary entry (or omit adding any content when there
are no real media types) so only valid media type keys and schemas are emitted
for the RequestBody.

In
`@test/Altinn.App.Api.Tests/PublicApiTests.PublicApi_ShouldNotChange_Unintentionally.verified.txt`:
- Around line 597-601: The SchemaPostVisitor type is declared public and
unsealed; change its declaration to internal sealed to follow project
guidelines. Update the class modifier on SchemaPostVisitor (and keep the
existing parameterless constructor and Visit override intact) so the type is
internal sealed SchemaPostVisitor, ensuring no external inheritance or public
exposure.
- Around line 623-637: The Snippets helper type is declared public but should be
internal per project guidelines; change the declaration of the Snippets type
from public static class Snippets to internal static class Snippets and update
any internal references (e.g., AltinnTokenSecurityScheme,
ProblemDetailsResponseReference, CommonParameters, DataGuidParameterReference,
InstanceGuidParameterReference, InstanceOwnerPartyIdParameterReference,
InstanceWriteSchema, LanguageParameterReference, PatchSchema,
ProblemDetailsResponseSchema, AddCommonErrorResponses) so compilation and tests
still pass; ensure no external assemblies rely on Snippets before making it
internal.

---

Outside diff comments:
In `@src/Altinn.App.Api/Controllers/CustomOpenApiController.cs`:
- Around line 283-289: CustomOpenApiController currently builds
OpenApiEncoding.ContentType using string.Join(' ', contentTypes) which yields
space-separated MIME types like "image/png image/jpeg" and is invalid; change
the construction in the multipartMediaType.Encoding.Add call (referencing
dataType.Id, OpenApiEncoding.ContentType and dataType.AllowedContentTypes) to
join allowed content types with a comma separator (e.g., string.Join(", ",
contentTypes)) and still fall back to "application/octet-stream" when
AllowedContentTypes is empty so ContentType is a valid comma-separated list or a
single media type.

---

Nitpick comments:
In `@test/Altinn.App.Api.Tests/OpenApi/OpenApiSpecChangeDetection.cs`:
- Around line 43-50: Uncomment or remove the commented
Assert.Empty(diagnostic.Errors) in the OpenApi parsing test so parsing issues
are not silently ignored: in the OpenApiSpecChangeDetection test, after using
OpenApiStreamReader and obtaining OpenApiDocument and the diagnostic object,
either restore Assert.Empty(diagnostic.Errors) to fail on unexpected parse
errors or replace it with a clear assertion/guard that documents expected errors
(e.g., Assert.Equal(expectedCount, diagnostic.Errors.Count) or a comment
explaining why errors are allowed) before proceeding to document.Info.Version =
"" and calling
VerifyJson(document.Serialize(CustomOpenApiController.SpecVersion,
CustomOpenApiController.SpecFormat), _verifySettings).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c0734924-2493-437b-83ee-1e901d0d85fc

📥 Commits

Reviewing files that changed from the base of the PR and between 7ba5478 and 78fb444.

📒 Files selected for processing (12)
  • Directory.Packages.props
  • src/Altinn.App.Api/Controllers/CustomOpenApiController.cs
  • src/Altinn.App.Api/Extensions/SwaggerFilterExtensions.cs
  • test/Altinn.App.Analyzers.Tests/testapp/App/Program.cs
  • test/Altinn.App.Api.Tests/Altinn.App.Api.Tests.csproj
  • test/Altinn.App.Api.Tests/Data/apps/tdd/contributer-restriction/config/applicationmetadata.json
  • test/Altinn.App.Api.Tests/OpenApi/OpenApiSpecChangeDetection.SaveCustomOpenApiSpec.verified.json
  • test/Altinn.App.Api.Tests/OpenApi/OpenApiSpecChangeDetection.SaveJsonSwagger.verified.json
  • test/Altinn.App.Api.Tests/OpenApi/OpenApiSpecChangeDetection.cs
  • test/Altinn.App.Api.Tests/Program.cs
  • test/Altinn.App.Api.Tests/PublicApiTests.PublicApi_ShouldNotChange_Unintentionally.verified.txt
  • test/Altinn.App.Integration.Tests/_testapps/basic/App/Program.cs
💤 Files with no reviewable changes (1)
  • test/Altinn.App.Api.Tests/Data/apps/tdd/contributer-restriction/config/applicationmetadata.json

Comment thread src/Altinn.App.Api/Controllers/CustomOpenApiController.cs
Comment thread src/Altinn.App.Api/Controllers/CustomOpenApiController.cs
Comment thread src/Altinn.App.Api/Controllers/CustomOpenApiController.cs
@ivarne ivarne enabled auto-merge (squash) May 29, 2026 12:13
@ivarne ivarne merged commit fcdd72e into main May 29, 2026
14 of 16 checks passed
@ivarne ivarne deleted the revert-openapi-v2 branch May 29, 2026 12:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Label Pull requests with new features. Used when generation releasenotes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants