Skip to content

Named object component aliases emit duplicate provided type #477

Closed

Description

@lukaszkrzywizna

Minimal reproduction

With SwaggerProvider 4.0.0 or 4.1.0 and .NET SDK 10.0.301:

open SwaggerProvider

type ReproApi =
    OpenApiClientProvider<"repro.json", SsrfProtection=false>

let client = ReproApi.Client()
{
  "openapi": "3.0.0",
  "info": { "title": "Named object alias reproduction", "version": "1.0.0" },
  "paths": {
    "/parent": {
      "get": {
        "operationId": "getParent",
        "responses": {
          "200": {
            "description": "Parent response",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Parent" }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Parent": {
        "oneOf": [
          { "$ref": "#/components/schemas/Parent_Child" }
        ]
      },
      "Parent_Child": {
        "type": "object",
        "required": ["childValue"],
        "properties": {
          "childValue": { "type": "string" }
        }
      }
    }
  }
}

The equivalent single-reference anyOf and allOf component aliases fail in the same way.

Version matrix

SwaggerProvider Result
4.0.0 Reproduced
4.1.0 Reproduced

Actual behavior

dotnet build fails while emitting the provided assembly:

FS3033: The type provider 'SwaggerProvider.OpenApiClientTypeProvider' reported an error: duplicate entry 'Parent_Child' in type index table

Expected behavior

  • Parent resolves to the generated Parent_Child object type.
  • The Parent component path is cached as an alias to that type.
  • Parent_Child is added to the generated namespace exactly once.
  • An operation returning Parent exposes the properties generated for Parent_Child.

Root-cause analysis

DefinitionCompiler.compileSingleRefOrNewObject recognizes that Parent is a single-reference alias, releases the Parent name reservation, and resolves Parent_Child.

At the end of compileBySchema, the unconditional registerNew(tyName, tyType) call for fromByPathCompiler both caches the component path and registers the returned type in the namespace. The returned ProvidedTypeDefinition is the already-generated Parent_Child, so it is registered under both component entries. NamespaceAbstraction.GetProvidedTypes() consequently returns the same provided type more than once, and assembly emission fails with the duplicate type-index entry.

The fix should separate component-path caching from namespace registration for aliases, rather than suppressing, renaming, dropping, or weakening the generated type.

PR #375 did not cover this case. Its regression tests place a single-reference wrapper on an object property that points to a primitive alias. They do not compile a named top-level component that aliases another named object component, so no duplicate ProvidedTypeDefinition is emitted.

Activity

  1. github-actions commented on Jul 28, 2026

    @github-actions
    Contributor

    🤖 This is an automated response from Repo Assist.

    Your root-cause analysis is spot-on. The fix is now in a draft PR — see the PR for details.

    Root cause (confirmed): registerInNsAndInDef was calling ns.RegisterType unconditionally, which re-added the Parent_Child ProvidedTypeDefinition to the namespace under the alias name "Parent". Since ReleaseNameReservation had removed "Parent" from providedTys, the registration hit the false, _ (no existing slot) branch in RegisterType, inserting the PTD a second time. GetProvidedTypes() then returned the same PTD twice → duplicate entry 'Parent_Child' in type index table.

    Fix applied: before calling ns.RegisterType, check via reference equality whether the PTD is already in pathToType.Values. If it is (i.e., this is an alias path), skip namespace registration — only the pathToType cache entry is added.

    The same latent issue affected allOf/anyOf single-ref wrappers and the pre-existing PetRef/DogRef/CatRef tests (they passed because duplicate PTDs in the list happen to have the same .Name, and the tests only check for presence, not assembly-emit integrity). The fix covers all three variants; 4 new regression tests added.

    Generated by 🌈 Repo Assist, see workflow run. Learn more.

    Add this agentic workflows to your repo

    To install this agentic workflow, run

    gh aw add githubnext/agentics/workflows/repo-assist.md@e15e57b40918dbca11b350c55d02ab61934afa75
    
  2. lukaszkrzywizna commented on Jul 28, 2026

    @lukaszkrzywizna
    ContributorAuthor

    [AGENT] This comment was written and published by an AI coding agent on behalf of the contributor.

    I opened #479 as an alternative implementation to the automatically generated #478.

    Both changes address the duplicate ProvidedTypeDefinition, but #479 separates the two responsibilities that caused the bug instead of detecting aliases by scanning pathToType.Values with reference equality:

    • newly created object and enum types register themselves in the generated namespace;
    • compileByPath caches every resolved component path, including alias paths;
    • resolving an alias therefore cannot add the target ProvidedTypeDefinition to the namespace a second time;
    • no reference-identity heuristic or repeated scan of the type-path cache is required.

    #479 also exercises the complete emitted-provider path requested by this issue. Operations return the aliased components, and the tests assert the response CLR type and access the target object's property. Coverage includes oneOf, anyOf, allOf, equal leaf names in separate generated namespaces, two aliases sharing one object, and an alias chain.

    Validation completed locally with .NET SDK 10.0.301:

    • CheckFormat: succeeded;
    • release build: 209 warnings, 0 errors;
    • SwaggerProvider.Tests: 552 total, 0 failed/errors, 1 skipped;
    • SwaggerProvider.ProviderTests: 133 total, 0 failed/errors;
    • full RunTests: Status: Ok in 53.6228 seconds;
    • the committed Stampli OpenAPI snapshot emitted successfully, and the real Stampli.VistaSync.IntegrationTests project built against the local provider with 0 errors.

    PR: #479

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions