[Repo Assist] fix: resolve oneOf/anyOf with single $ref to referenced type#375
Merged
sergey-tihon merged 2 commits intomasterfrom Apr 13, 2026
Conversation
When a schema uses `oneOf: [$ref]` or `anyOf: [$ref]` with a single entry, resolve to the referenced type instead of falling through to `obj`. This pattern is common in OpenAPI specs generated by tools (e.g. NSwag, Kiota, AutoRest) and in OpenAPI 3.1 schemas that use oneOf/anyOf for nullable type annotations. Previously only `allOf: [$ref]` (the OpenAPI 3.0 canonical form) was handled; the equivalent oneOf and anyOf forms produced `obj` instead of the referenced type. Changes: - DefinitionCompiler.fs: add oneOf/anyOf single-ref handlers after the existing allOf single-ref handler; also extend the type-inference guard to check oneOf/anyOf single-entry type when schemaObj.Type is missing - Schema.TypeMappingTests.fs: add 6 tests covering oneOf and anyOf with primitive and object aliases, required and optional variants Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes OpenAPI v3 schema compilation so that oneOf: [ $ref ] and anyOf: [ $ref ] (single-entry wrappers) resolve to the referenced schema type rather than incorrectly falling back to obj, matching the existing allOf: [ $ref ] handling in DefinitionCompiler.
Changes:
- Extend v3
DefinitionCompilerto resolveoneOf/anyOfwith a single$refentry to the referenced type (mirroring theallOfsingle-ref behavior). - Improve type inference when
schemaObj.Typeis missing by looking into single-entryoneOf/anyOfsubschemas. - Add v3 unit tests validating
oneOf/anyOfsingle-ref alias resolution for required and optional properties.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/SwaggerProvider.DesignTime/v3/DefinitionCompiler.fs |
Adds single-$ref handlers for oneOf/anyOf and expands missing-Type inference to cover those wrappers. |
tests/SwaggerProvider.Tests/v3/Schema.TypeMappingTests.fs |
Adds tests ensuring oneOf/anyOf single-ref wrappers resolve to the referenced primitive alias type (including optional/value-type wrapping). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 This PR was created by Repo Assist, an automated AI assistant.
Closes #243
Root Cause
The v3
DefinitionCompileralready handlesallOf: [$ref]with a single entry — it resolves the reference and returns the correct type. However,oneOf: [$ref]andanyOf: [$ref]with a single entry were not handled, so they fell through tocompileNewObject()→obj.These patterns are semantically equivalent to a direct
$refand are common in:anyOf: [$ref]as the nullable type annotation (alongsidenullable: true)Fix
Add handlers for
oneOf/anyOfwith a single$refentry immediately after the existingallOfsingle-ref handler, following the same pattern:Also extend the
resolvedTypeinference guard to checkoneOf/anyOfsingle-entry types whenschemaObj.Typeis absent.Changes
src/SwaggerProvider.DesignTime/v3/DefinitionCompiler.fsoneOf/anyOfsingle-ref handlers and type inferencetests/SwaggerProvider.Tests/v3/Schema.TypeMappingTests.fsoneOf/anyOfwith string and int aliases, required and optionalTest Status
✅ Build: succeeded (0 errors, pre-existing warnings only)
✅ Unit tests: 328 passed, 0 failed, 2 skipped (all pass, 6 new tests added)
✅ Format check:
fantomas --checkpassed on changed filesIntegration/provider tests require a running Swashbuckle test server. The changed code is in the compile-time schema resolution path; it does not affect HTTP, serialization, or runtime behaviour.