microsoft / typespec

https://typespec.io/
MIT License
3.92k stars 176 forks source link

Add support for extending OpenApi `info` object via extensions #3446

Closed irvinesunday closed 1 week ago

irvinesunday commented 1 month ago

Clear and concise description of the problem

Given the below Typespec example, which passes a custom decorator @skill() on a namespace that aims to extend the OpenApi info object via extensions:

main.tsp

import "@typespec/http";
import "@typespec/rest";
import "@typespec/openapi3";
// IMPORTED CUSTOM SKILLS DECORATOR

using TypeSpec.Http;
using TypeSpec.Rest;
// using TypeSpec.Skills; (uncomment when ready)

alias sunoSkill = {
  name_for_human: "Suno";
  description_for_human: "Suno is a tool that can generate songs based on a description. The user provides a description of the song they want to create, and Suno generates the song lyrics. Suno is a fun and creative way to create songs for any occasion.";
  logo_url: "https://th.bing.com/th?id=OSK.935650835684F7E18AC3F31034DE6DF3";
  contact_email: "ionutro@microsoft.com";
  privacy_policy_url: "https://app.suno.ai/privacy";
  legal_info_url: "https://app.suno.ai/legal";
};

/**
 * Suno is a tool that can generate songs based on a description. The user provides a description of the song they want to create, and Suno generates the song lyrics. Suno is a fun and creative way to create songs for any occasion.
 */
@service({
  title: "Suno"
})
@skill(sunoSkill)
@route("/createSong")
namespace Suno {
  /** Create a song given the user preferences */
  @get op createSong(
    /** The requested song description, eg. `a country song about Thanksgiving` */
    @query topic: string): string;
}

The expected OpenApi file output should be:

openapi.yaml

openapi: 3.0.0
info:
  title: Suno
  description: Create any song you want based on a description
  version: 1.0.0
  contact:
    email: ionutro@microsoft.com
  x-logo: https://w7.pngwing.com/pngs/208/848/png-transparent-musical-note-logo-clef-music-logo-design-other-text-musical-composition.png
  x-ai-description: Suno is a tool that can generate songs based on a description. The user provides a description of the song they want to create, and Suno generates the song lyrics. Suno is a fun and creative way to create songs for any occasion.
  x-legal-info-url: https://app.suno.ai/legal
  x-privacy-policy-url: https://app.suno.ai/privacy
tags: []
paths:
  /createSong:
    get:
      operationId: createSong
      description: Create a song given the user preferences
      parameters:
        - name: topic
          in: query
          required: true
          description: The requested song description, eg. `a country song about Thanksgiving`
          schema:
            type: string
      responses:
        '200':
          description: The request has succeeded.
          content:
            application/json:
              schema:
                type: string
components: {}

However, this is currently not supported. We need to have a way of extending an OpenApi info object via extensions.

Checklist

timotheeguerin commented 1 month ago

The implementation of this should basically be the same as setExtension helper.

but setInfoExtension(key: string, value: unknown): void and then add those values to the info data

wanlwanl commented 1 month ago

Maybe it can be workaround for now? @irvinesunday Typespec:

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

using TypeSpec.Http;
using TypeSpec.Rest;
using TypeSpec.OpenAPI;

/**
 * This is a sample server Petstore server.
 */
@info({
    title: "title - info",
    "x-cutome-info": {
        name_for_human: "Suno";
        description_for_human: "Suno is a tool that can generate songs based on a description. The user provides a description of the song they want to create, and Suno generates the song lyrics. Suno is a fun and creative way to create songs for any occasion.";
        logo_url: "https://th.bing.com/th?id=OSK.935650835684F7E18AC3F31034DE6DF3";
        contact_email: "ionutro@microsoft.com";
        privacy_policy_url: "https://app.suno.ai/privacy";
        legal_info_url: "https://app.suno.ai/legal";
      },
})
@service
namespace PetStore;

@extension("x-custom", "My value")
op readxxx(): string;

Result:

openapi: 3.0.0
info:
  title: title - info
  x-cutome-info:
    name_for_human: Suno
    description_for_human: Suno is a tool that can generate songs based on a description. The user provides a description of the song they want to create, and Suno generates the song lyrics. Suno is a fun and creative way to create songs for any occasion.
    logo_url: https://th.bing.com/th?id=OSK.935650835684F7E18AC3F31034DE6DF3
    contact_email: ionutro@microsoft.com
    privacy_policy_url: https://app.suno.ai/privacy
    legal_info_url: https://app.suno.ai/legal
  description: This is a sample server Petstore server.
  version: 0.0.0
tags: []
paths:
  /:
    get:
      operationId: readxxx
      parameters: []
      responses:
        '200':
          description: The request has succeeded.
          content:
            application/json:
              schema:
                type: string
      x-custom: My value
components: {}