documentOperationResponses method Null safety

  1. @override
Map<String, APIResponse> documentOperationResponses(
  1. APIDocumentContext context,
  2. Operation? operation
)
override

Returns a map of possible responses for operation.

To provide documentation for an operation, you must override this method and return a map of possible responses. The key is a String representation of a status code (e.g., "200") and the value is an APIResponse object.

Implementation

@override
Map<String, APIResponse> documentOperationResponses(
    APIDocumentContext context, Operation? operation) {
  switch (operation!.method) {
    case "GET":
      if (operation.pathVariables.isEmpty) {
        return {
          "200": APIResponse.schema(
              "Returns a list of objects.",
              APISchemaObject.array(
                  ofSchema: context.schema.getObjectWithType(InstanceType))),
          "400": APIResponse.schema("Invalid request.",
              APISchemaObject.object({"error": APISchemaObject.string()}))
        };
      }

      return {
        "200": APIResponse.schema("Returns a single object.",
            context.schema.getObjectWithType(InstanceType)),
        "404": APIResponse("No object found.")
      };
    case "PUT":
      return {
        "200": APIResponse.schema("Returns updated object.",
            context.schema.getObjectWithType(InstanceType)),
        "404": APIResponse("No object found."),
        "400": APIResponse.schema("Invalid request.",
            APISchemaObject.object({"error": APISchemaObject.string()})),
        "409": APIResponse.schema("Object already exists",
            APISchemaObject.object({"error": APISchemaObject.string()})),
      };
    case "POST":
      return {
        "200": APIResponse.schema("Returns created object.",
            context.schema.getObjectWithType(InstanceType)),
        "400": APIResponse.schema("Invalid request.",
            APISchemaObject.object({"error": APISchemaObject.string()})),
        "409": APIResponse.schema("Object already exists",
            APISchemaObject.object({"error": APISchemaObject.string()}))
      };
    case "DELETE":
      return {
        "200": APIResponse("Object successfully deleted."),
        "404": APIResponse("No object found."),
      };
  }

  return {};
}