Skip to content

Fix invalid OpenAPI schema generation for models extending Array #7535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 3, 2025

When a TypeSpec model extends an Array, the generated OpenAPI schema was invalid because it contained both type: "object" and allOf: [{ type: "array", ... }]. This creates a contradictory schema that violates the OpenAPI specification.

Before (Invalid):

model A extends Array<int32> {}

Generated:

A:
  type: object
  allOf:
    - type: array
      items:
        type: integer
        format: int32

After (Valid):

model A extends Array<int32> {}

Generated:

A:
  type: array
  items:
    type: integer
    format: int32

Solution:
Modified modelDeclaration in schema-emitter.ts to detect when a model extends an array type using isArrayModelType. In such cases:

  1. Extract the element type from the base array model's indexer
  2. Generate an array schema directly instead of an object schema
  3. Skip adding the baseModel to allOf for array extensions

This ensures that model A extends Array<T> {} and model A is Array<T> {} generate identical, valid schemas.

Tests Added:

  • Verification that extends and is Array generate identical schemas
  • Support for complex element types (custom models)
  • Edge cases with different primitive types

All existing tests pass, confirming no regression in normal model inheritance or array handling.

Fixes #7519.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • http://168.63.129.16:80/machine/
    • Triggering command: /usr/bin/python3 -u bin/WALinuxAgent-2.13.1.1-py3.9.egg -collect-logs (http block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: chrisradek <14189820+chrisradek@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] openapi3 - model extends Array generates invalid schema Fix invalid OpenAPI schema generation for models extending Array Jun 3, 2025
@Copilot Copilot AI requested a review from chrisradek June 3, 2025 20:16
Copilot finished work on behalf of chrisradek June 3, 2025 20:16
required: this.#requiredModelProperties(model, visibility),
properties: this.emitter.emitModelProperties(model),
});

Copy link
Member

Choose a reason for hiding this comment

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

@copilot, we believe this could be simplified.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Simplified the logic by eliminating code duplication. The array extension check and element type extraction are now combined, and I use a ternary operator to create the appropriate schema directly instead of having separate if/else blocks with redundant object schema creation. Commit 1e5abdd.

Copy link
Member

Choose a reason for hiding this comment

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

Never mind, go back to the original commit.

Co-authored-by: richardpark-msft <51494936+richardpark-msft@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 3, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • http://168.63.129.16:80/machine/
    • Triggering command: /usr/bin/python3 -u bin/WALinuxAgent-2.13.1.1-py3.9.egg -collect-logs (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of richardpark-msft June 3, 2025 20:26
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.

openapi3 - model extends Array generates invalid schema
3 participants