Skip to content

Fix issue 2462 - #3873

Open
terdskadoosh wants to merge 5 commits into
Azure:mainfrom
terdskadoosh:fix-issue-2462
Open

terdskadoosh wants to merge 5 commits into
Azure:mainfrom
terdskadoosh:fix-issue-2462

Conversation

@terdskadoosh

Copy link
Copy Markdown

Why make this change?

  • Resolves [Enh]: OpenAPI does not show multiple data sources #2462 — When a runtime configuration contains entities mapped to multiple data sources, the OpenAPI/Swagger generation previously only used metadata from the default data source. As a result, entities backed by non-default data sources were omitted from the generated OpenAPI document and Swagger UI only showed types from the first configuration.
  • This change ensures the OpenAPI document includes schemas and paths for entities from all configured data sources, preventing incomplete API documentation for multi-datasource setups.
  • Related discussion: [Enh]: OpenAPI does not show multiple data sources #2462

What is this change?

  • Summary

    • Resolve each entity's metadata provider from the entity-to-data-source mapping in the runtime configuration instead of assuming the default data source contains every entity.
    • Iterate runtimeConfig.Entities (the configured entities) when building OpenAPI paths and component schemas, and for each entity resolve the correct ISqlMetadataProvider via the metadata provider factory.
    • Skip entities gracefully when an entity is not present in its resolved metadata provider, and log a helpful warning/error.
    • Reuse tags from the global tags dictionary to avoid duplicate groups in Swagger UI.
    • Update BuildPaths and CreateComponentSchemas to accept RuntimeConfig instead of relying on the default data source name, and add a helper to resolve the proper metadata provider for each entity.
  • Files changed (key)

    • src/Core/Services/OpenAPI/OpenApiDocumentor.cs
      • Added GetEntityMetadataProvider(RuntimeConfig runtimeConfig, string entityName)
      • BuildPaths and CreateComponentSchemas now accept RuntimeConfig and use per-entity metadata resolution
      • Added logging for entities that cannot be resolved in their configured data source
    • src/Service.Tests/UnitTests/OpenApiDocumentorUnitTests.cs
      • Added CreateDocument_UsesEntitySpecificDataSourceMetadataForSchemas
      • Added test helpers to mock per-datasource metadata providers for Book and Review entities
  • Behavior change

    • OpenAPI schemas and paths will now include entities mapped to non-default data sources when those entities exist in their configured data source metadata.
    • Entities missing from their resolved metadata provider are excluded with a log message instead of silently producing incorrect docs.

How was this tested?

  • Integration Tests
  • Unit Tests

Unit test added:

  • CreateDocument_UsesEntitySpecificDataSourceMetadataForSchemas (OpenApiDocumentorUnitTests)
    • Sets up a RuntimeConfig with two data sources and two entities mapped to different data sources (Book -> books-ds, Review -> reviews-ds).
    • Mocks per-datasource ISqlMetadataProvider instances with distinct schema shapes.
    • Calls documentor.CreateDocument() and asserts the generated OpenAPI JSON contains:
      • components.schemas.Book with expected properties (e.g., title)
      • components.schemas.Review with expected properties (e.g., review_id, content)

Notes:

  • No integration tests included in this PR; recommend running the full test suite or an end-to-end test against a multi-datasource runtime config for additional verification.

Sample Request(s)

Example: Verify OpenAPI JSON contains schemas for entities that belong to different datasources.

  1. Start Data API Builder with a runtime config where:

    • Book -> data source books-ds
    • Review -> data source reviews-ds
  2. Fetch the OpenAPI JSON:

    • curl -sS -H "Accept: application/json" http://localhost:<port>/openapi.json

Expected (excerpt):

{
  "components": {
    "schemas": {
      "Book": { /* schema with properties such as "title" */ },
      "Review": { /* schema with properties such as "review_id", "content" */ }
    }
  }
}

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Merge-conflict markers block compilation, and the new regression test loses its data-source mappings during configuration reload.

2 open issues
What changed in this PR

Addresses #2462 by using entity-specific data sources when generating DAB’s OpenAPI documentation and routing REST mutations.

Changes:

  • Generate OpenAPI schemas and paths using each entity’s metadata provider.
  • Pass data-source names through REST mutation execution and improve missing-metadata errors.
  • Add a multi-source OpenAPI regression test.
File Description
src/​Service.Tests/​UnitTests/​OpenApiDocumentorUnitTests.cs Adds multi-source schema coverage and helpers.
src/​Core/​Services/​RestService.cs Passes the entity’s data source to mutations.
src/​Core/​Services/​OpenAPI/​OpenApiDocumentor.cs Resolves metadata per entity for schemas and paths.
src/​Core/​Resolvers/​SqlMutationEngine.cs Uses the supplied REST mutation data source.
src/​Core/​Resolvers/​IMutationEngine.cs Adds an optional data-source parameter.
src/​Core/​Resolvers/​CosmosMutationEngine.cs Aligns the REST mutation signature.
src/​Core/​Resolvers/​BaseQueryStructure.cs Reports missing entity metadata explicitly.

Review effort: Balanced

Comment on lines +251 to +255
<<<<<<< HEAD
Paths = BuildPaths(runtimeConfig, globalTagsDict, role, runtimeConfig.IsRequestBodyStrict),
=======
Paths = BuildPaths(runtimeConfig, globalTagsDict, role, isRequestBodyStrict: runtimeConfig.IsRequestBodyStrict),
>>>>>>> b8af66daa10568c50397314a8d18a9e49f4c78cd
Comment on lines +279 to +280
RuntimeConfigProvider configProvider = TestHelper.GenerateInMemoryRuntimeConfigProvider(config);
RuntimeConfig loadedConfig = configProvider.GetConfig();
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Enh]: OpenAPI does not show multiple data sources

3 participants