typespec1-3 Exercise 1: A Recipe Model =========================================== recipe.tsp (compiled with TypeSpec 1.17.0, OpenAPI 3.1.0): import "@typespec/http"; using Http; @service(#{ title: "Recipe Service" }) namespace RecipeService; enum Unit { gram, millilitre, piece } model Ingredient { name: string; quantity: decimal; unit: Unit; } model Recipe { id: string; title: string; servings: int32 = 4; prepTime: duration; ingredients: Ingredient[]; tags?: string[]; addedOn: plainDate; } @route("/recipes") interface Recipes { @get list(): Recipe[]; } The emitted schemas (the part of the file after 'components:'): components: schemas: Ingredient: type: object required: - name - quantity - unit properties: name: type: string quantity: type: number format: decimal unit: $ref: '#/components/schemas/Unit' Recipe: type: object required: - id - title - servings - prepTime - ingredients - addedOn properties: id: type: string title: type: string servings: type: integer format: int32 default: 4 prepTime: type: string format: duration ingredients: type: array items: $ref: '#/components/schemas/Ingredient' tags: type: array items: type: string addedOn: type: string format: date Unit: type: string enum: - gram - millilitre - piece PROPERTY BY PROPERTY (Recipe) id required; type: string title required; type: string servings required AND defaulted (default: 4); integer, format int32. It is in the required list because the property is not marked optional: a default does not make it optional prepTime required; string with format duration (an ISO 8601 duration) ingredients required; an array whose items reference the Ingredient schema tags OPTIONAL (the ?); an array of strings; not in the required list addedOn required; string with format date (a plainDate: no time of day) (Ingredient) name: string; quantity: decimal, emitted as number with format decimal; unit: a reference to the Unit enum, whose values are gram, millilitre and piece. WHY THIS WORKS AS AN ANSWER --------------------------- The model uses one of each kind of thing from the chapter: a default, an optional array, a named enum, a nested model, and three kinds of scalar. Reading the emitted schema line by line, rather than trusting the TypeSpec, is the habit to build. If you want servings to be omittable with a default of 4, change it to servings?: int32 = 4.