Fern Definition

Types

Types describe the data model of your API.

Built-in types

  • string
  • integer
  • long
  • double
  • boolean
  • datetime An RFC 3339, section 5.6 datetime. For example, 2017-07-21T17:32:28Z.
  • date An RFC 3339, section 5.6 date (YYYY-MM-DD). For example, 2017-07-21.
  • uuid
  • base64
  • list e.g., list<string>
  • set e.g., set<string>
  • map e.g., map<string, integer>
  • optional e.g., optional<string>
  • literal e.g., literal<"Plants">
  • unknown Represents arbitrary JSON.

Custom types

Creating your own types is easy in Fern!

Objects

The most common custom types are objects.

In Fern, you use the "properties" key to create an object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
types:
  Person:
    properties: # <---
      name: string
      address: Address

  Address:
    properties: # <---
      line1: string
      line2: optional<string>
      city: string
      state: string
      zip: string
      country: literal<"USA">

These represent JSON objects:

1
2
3
4
5
6
7
8
9
10
{
  "name": "Alice",
  "address": {
    "line1": "123 Happy Lane",
    "city": "New York",
    "state": "NY",
    "zip": "10001",
    "country": "USA"
  }
}

You can also use extends to compose objects:

1
2
3
4
5
6
7
8
types:
  Pet:
    properties:
      name: string
  Dog:
    extends: Pet
    properties:
      breed: string

You can extend multiple objects:

1
2
3
4
5
6
7
types:
  GoldenRetriever:
    extends:
      - Dog
      - Pet
    properties:
      isGoodBoy: boolean

Aliases

An Alias type is a renaming of an existing type. This is usually done for clarity.

1
2
3
4
5
6
7
8
types:
  # UserId is an alias of string
  UserId: string

  User:
    properties:
      id: UserId
      name: string

Enums

An enum represents a string with a set of allowed values.

In Fern, you use the "enum" key to create an enum:

1
2
3
4
5
6
7
types:
  WeatherReport:
    enum: # <---
      - SUNNY
      - CLOUDY
      - RAINING
      - SNOWING

Enum names are restricted to A-Z, a-z, 0-9, and _ to ensure that generated code can compile across all of the languages that Fern can output. If you have an enum that doesn't follow this convention, you can use the "name" key to specify a custom name:

1
2
3
4
5
6
7
8
9
types:
  Operator:
    enum:
      - name: LESS_THAN # <--- the name that will be used in SDKs
        value: < # <--- the value that will be serialized
      - name: GREATER_THAN
        value: >
      - name: NOT_EQUAL
        value: !=

Unions

Fern supports tagged unions (a.k.a. discriminated unions). Unions are useful for polymorphism. This is similar to the oneOf concept in OpenAPI.

In Fern, you use the "union" key to create an union:

1
2
3
4
5
6
7
8
9
10
11
types:
  Animal:
    union:
      dog: Dog
      cat: Cat
  Dog:
    properties:
      likesToWoof: boolean
  Cat:
    properties:
      likesToMeow: boolean

In JSON, unions have a discriminant property to differentiate between different members of the union. By default, Fern uses "type" as the discriminant property:

1
2
3
4
{
  "type": "dog",
  "likesToWoof": true
}

You can customize the discriminant property using the "discriminant" key:

1
2
3
4
5
6
7
8
9
10
11
12
 types:
   Animal:
+    discriminant: animalType
     union:
       dog: Dog
       cat: Cat
   Dog:
     properties:
       likesToWoof: boolean
   Cat:
     properties:
       likesToMeow: boolean

This corresponds to a JSON object like this:

1
2
3
4
{
  "animalType": "dog",
  "likesToWoof": true
}

You can also have a union without a discriminant.

1
2
3
4
5
MyUnion:
  discriminated: false
  union:
    - string
    - integer

Documentation

You can add documentation for types. These docs are passed into the compiler, and are incredibly useful in the generated outputs (e.g., docstrings in SDKs).

1
2
3
4
5
6
7
8
types:
  Person:
    docs: A person represents a human being
    properties:
      name: string
      age:
        docs: age in years
        type: integer
1
2
3
4
5
6
7
8
/**
 * A person represents a human being
 */
interface Person {
  name: string;
  // age in years
  age: number;
}