Skip to content
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

tsp-openapi3 - allOf with more than one member not supported #4152

Closed
4 tasks done
chrisradek opened this issue Aug 12, 2024 · 0 comments · Fixed by #4232
Closed
4 tasks done

tsp-openapi3 - allOf with more than one member not supported #4152

chrisradek opened this issue Aug 12, 2024 · 0 comments · Fixed by #4232
Assignees
Labels
bug Something isn't working openapi3:converter Issues for @typespec/openapi3 openapi to typespec converter triaged:core

Comments

@chrisradek
Copy link
Member

Describe the bug

Currently tsp-openapi3 only includes allOf members in a generated TypeSpec model if there is one referenced member. In this case, the generated model extends the allOf member.

Instead the default behavior should be that all allOf members are spread into the generated TypeSpec model.

We may also need to support extending one of the allOf members if all of the following is true:

  • One and only one of the members has a discriminator
  • The discriminated member is a reference and not an in-line schema

Reproduction

openapi: 3.0.0
info:
  title: Sample
  version: 0.0.0
tags: []
paths: {}
components:
  schemas:
    Cat:
      allOf:
        - $ref: "#/components/schemas/Pet"
        - type: object
          required:
            - kind
            - name
          properties:
            kind:
              type: string
              enum:
                - cat
            name:
              type: string
    Pet:
      type: object
      required:
        - kind
      properties:
        kind:
          type: string
      discriminator:
        propertyName: kind
        mapping:
          cat: "#/components/schemas/Cat"

currently generates

import "@typespec/http";
import "@typespec/openapi";
import "@typespec/openapi3";

using Http;
using OpenAPI;

@service({
  title: "Sample",
})
@info({
  version: "0.0.0",
})
namespace Sample;

model Cat {}

@discriminator("kind")
model Pet {
  kind: string;
}

The Cat model is empty since more than 1 allOf is not supported. However, if the in-lined schema was moved from Cat's allOf to the schema's root:

openapi: 3.0.0
info:
  title: Sample
  version: 0.0.0
tags: []
paths: {}
components:
  schemas:
    Cat:
      type: object
      required:
        - kind
        - name
      properties:
        kind:
          type: string
          enum:
            - cat
        name:
          type: string
      allOf:
        - $ref: "#/components/schemas/Pet"
    Pet:
      type: object
      required:
        - kind
      properties:
        kind:
          type: string
      discriminator:
        propertyName: kind
        mapping:
          cat: "#/components/schemas/Cat"

then the correct output is generated:

import "@typespec/http";
import "@typespec/openapi";
import "@typespec/openapi3";

using Http;
using OpenAPI;

@service({
  title: "Sample",
})
@info({
  version: "0.0.0",
})
namespace Sample;

model Cat extends Pet {
  kind: "cat";
  name: string;
}

@discriminator("kind")
model Pet {
  kind: string;
}

Checklist

@chrisradek chrisradek added the bug Something isn't working label Aug 12, 2024
@chrisradek chrisradek added the openapi3:converter Issues for @typespec/openapi3 openapi to typespec converter label Aug 12, 2024
github-merge-queue bot pushed a commit that referenced this issue Aug 12, 2024
…peSpec data types (#4149)

Fixes #4080  and #4088

This also fixes a few bugs/rough edges around generating TypeSpec types
from OpenAPI3 component schemas.

Still 2 things to follow up with that I'll create issues for:
1. Support `allOf` with more than 1 member. Likely this will result in
spreading all the members, except extending 1 if there is 1 that has a
discriminator. (#4152)
2. Place all component schemas under a `Schemas` namespace (and do
something similar for `Parameters`) to prevent name collisions.
(#4151)

---------

Co-authored-by: Christopher Radek <Christopher.Radek@microsoft.com>
@markcowl markcowl added this to the [2024] September milestone Aug 19, 2024
@chrisradek chrisradek self-assigned this Aug 20, 2024
github-merge-queue bot pushed a commit that referenced this issue Aug 29, 2024
Fixes #4152
Depends on #4216

This PR updates how tsp-openapi3 handles generating models for schemas
that use `allOf`.

Currently `allOf` is ignored unless there is only 1 member and that
member is a schema reference. In this scenario, the model extends the
single member.

This update now takes all of the schema `allOf` members into
consideration when generating a model.
- inline-schemas have their properties merged into the model's
properties
- schema references without a discriminator defined are spread into the
model
- if only 1 schema reference contains a discriminator, then the model
extends it, otherwise these schema references are spread as well.

---------

Co-authored-by: Christopher Radek <Christopher.Radek@microsoft.com>
weidongxu-microsoft pushed a commit to weidongxu-microsoft/typespec that referenced this issue Sep 3, 2024
…soft#4232)

Fixes microsoft#4152
Depends on microsoft#4216

This PR updates how tsp-openapi3 handles generating models for schemas
that use `allOf`.

Currently `allOf` is ignored unless there is only 1 member and that
member is a schema reference. In this scenario, the model extends the
single member.

This update now takes all of the schema `allOf` members into
consideration when generating a model.
- inline-schemas have their properties merged into the model's
properties
- schema references without a discriminator defined are spread into the
model
- if only 1 schema reference contains a discriminator, then the model
extends it, otherwise these schema references are spread as well.

---------

Co-authored-by: Christopher Radek <Christopher.Radek@microsoft.com>
sarangan12 pushed a commit to sarangan12/typespec that referenced this issue Sep 16, 2024
…peSpec data types (microsoft#4149)

Fixes microsoft#4080  and microsoft#4088

This also fixes a few bugs/rough edges around generating TypeSpec types
from OpenAPI3 component schemas.

Still 2 things to follow up with that I'll create issues for:
1. Support `allOf` with more than 1 member. Likely this will result in
spreading all the members, except extending 1 if there is 1 that has a
discriminator. (microsoft#4152)
2. Place all component schemas under a `Schemas` namespace (and do
something similar for `Parameters`) to prevent name collisions.
(microsoft#4151)

---------

Co-authored-by: Christopher Radek <Christopher.Radek@microsoft.com>
sarangan12 pushed a commit to sarangan12/typespec that referenced this issue Sep 16, 2024
…soft#4232)

Fixes microsoft#4152
Depends on microsoft#4216

This PR updates how tsp-openapi3 handles generating models for schemas
that use `allOf`.

Currently `allOf` is ignored unless there is only 1 member and that
member is a schema reference. In this scenario, the model extends the
single member.

This update now takes all of the schema `allOf` members into
consideration when generating a model.
- inline-schemas have their properties merged into the model's
properties
- schema references without a discriminator defined are spread into the
model
- if only 1 schema reference contains a discriminator, then the model
extends it, otherwise these schema references are spread as well.

---------

Co-authored-by: Christopher Radek <Christopher.Radek@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi3:converter Issues for @typespec/openapi3 openapi to typespec converter triaged:core
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants