ballerina-platform / ballerina-library

The Ballerina Library
https://ballerina.io/learn/api-docs/ballerina/
Apache License 2.0
137 stars 58 forks source link

Distinct Error Type Missing in the Generated OpenAPI Spec #6777

Closed DimuthuMadushan closed 2 months ago

DimuthuMadushan commented 2 months ago

Description: Consider the following http service.

import ballerina/http;

type ServerError distinct error;

type Error ServerError;

type Album readonly & record {
    string title;
    string artist;
};

service / on new http:Listener(9090) {

    resource function get artist() returns string|Error {
        http:Client|error albumClient = new ("localhost:9091");
        if albumClient is error {
            return error ServerError(albumClient.message(), albumClient.cause());
        }
        Album[]|error albums = albumClient->/albums;
        if albums is error {
            return error ServerError(albums.message(), albums.cause());
        }
        return "First artist name: " + albums[0].artist;
    }
}

The generated OpenAPI spec for the above service doesn't include the ServerError type.

openapi: 3.0.1
info:
  title: Openapi Openapi Yaml
  version: 0.0.0
servers:
- url: "{server}:{port}/"
  variables:
    server:
      default: http://localhost
    port:
      default: "9090"
paths: {}
components:
  schemas:
    Error:
      $ref: '#/components/schemas/ServerError'

However it returns the following warning while generating the spec.

OpenAPI definition(s) generated successfully and copied to :
-- openapi_openapi.yaml
WARNING [openapi.bal:(3:6,3:17)] Generated OpenAPI definition does not contain information for Ballerina type error.
TharmiganK commented 2 months ago

May I know the ballerina version you are using?

This works in the latest Ballerina version - Ballerina SwanLake Update 9(2201.9.2)

Generated OpenAPI specification:

openapi: 3.0.1
info:
  title: Sample Openapi Yaml
  version: 0.0.0
servers:
- url: "{server}:{port}/"
  variables:
    server:
      default: http://localhost
    port:
      default: "9090"
paths:
  /artist:
    get:
      operationId: getArtist
      responses:
        "200":
          description: Ok
          content:
            text/plain:
              schema:
                type: string
        "500":
          description: InternalServerError
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ServerError'
components:
  schemas:
    ErrorPayload:
      required:
      - message
      - method
      - path
      - reason
      - status
      - timestamp
      type: object
      properties:
        timestamp:
          type: string
        status:
          type: integer
          format: int64
        reason:
          type: string
        message:
          type: string
        path:
          type: string
        method:
          type: string
    ServerError:
      $ref: '#/components/schemas/ErrorPayload'
DimuthuMadushan commented 2 months ago

@TharmiganK 2201.8.6

TharmiganK commented 2 months ago

Closing this issue since this is already fixed in the latest version