Skip to content

Instantly share code, notes, and snippets.

@darrelmiller
Last active August 28, 2022 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darrelmiller/551465458ada8521fbbe309a989e4afb to your computer and use it in GitHub Desktop.
Save darrelmiller/551465458ada8521fbbe309a989e4afb to your computer and use it in GitHub Desktop.
CADL for Kevin
import "@cadl-lang/rest";
import "@cadl-lang/openapi3";
using Cadl.Http;
@serviceTitle("ConferenceService")
namespace ConferenceApi {
alias Error = NotFound | InternalServerError;
@route("conferences")
interface ConferenceResource {
@route("conferences/{id}")
@get Retrieve(...RetrieveConference): ConferenceRetrieved | Error;
@post Create(...CreateConference): ConferenceCreated | Error;
}
model RetrieveConference {
@path
id: string;
}
model CreateConference {
@body
conference: ConferenceSchema;
}
model ConferenceRetrieved extends ConferenceSchema {
@statusCode statusCode: 200;
id: string;
}
model ConferenceCreated {
@statusCode statusCode: 201;
}
model NotFound {
@statusCode statusCode: 404;
}
model InternalServerError {
@statusCode statusCode: 500;
}
model ConferenceSchema {
address: Address;
}
model Address {
street: string;
}
}
@darrelmiller
Copy link
Author

darrelmiller commented Aug 28, 2022

Here's the outputted OpenAPI

{
  "openapi": "3.0.0",
  "info": {
    "title": "ConferenceService",
    "version": "0000-00-00"
  },
  "tags": [],
  "paths": {
    "/conferences/conferences/{id}": {
      "get": {
        "operationId": "ConferenceResource_Retrieve",
        "parameters": [
          {
            "$ref": "#/components/parameters/RetrieveConference"
          }
        ],
        "responses": {
          "200": {
            "description": "The request has succeeded.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ConferenceRetrieved"
                }
              }
            }
          },
          "404": {
            "description": "The server cannot find the requested resource."
          },
          "500": {
            "description": "Server Error"
          }
        }
      }
    },
    "/conferences": {
      "post": {
        "operationId": "ConferenceResource_Create",
        "parameters": [],
        "responses": {
          "201": {
            "description": "The request has succeeded and a new resource has been created as a result."
          },
          "404": {
            "description": "The server cannot find the requested resource."
          },
          "500": {
            "description": "Server Error"
          }
        },
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ConferenceSchema"
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "parameters": {
      "RetrieveConference": {
        "name": "id",
        "in": "path",
        "required": true,
        "schema": {
          "type": "string"
        }
      }
    },
    "schemas": {
      "ConferenceRetrieved": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          }
        },
        "required": [
          "id"
        ],
        "allOf": [
          {
            "$ref": "#/components/schemas/ConferenceSchema"
          }
        ]
      },
      "ConferenceSchema": {
        "type": "object",
        "properties": {
          "address": {
            "$ref": "#/components/schemas/Address"
          }
        },
        "required": [
          "address"
        ]
      },
      "Address": {
        "type": "object",
        "properties": {
          "street": {
            "type": "string"
          }
        },
        "required": [
          "street"
        ]
      }
    }
  }
}

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