Skip to content

generate model

Create a Codable model, optionally inferred from a JSON sample.

Creates a Codable struct. Without a sample, it’s an empty skeleton. With --from, fields are inferred from a real JSON payload.

Terminal window
swiftspawn generate model <Name> [--from <path-or-url>] [--in <feature>] [--dry-run] [--force] [--quiet] [--verbose]

generate model is meant to be the fastest path from an example JSON response to a Swift type that decodes it.

Without --from, it produces an empty Codable struct with a comment placeholder, ready for you to fill in.

With --from <path-or-url>, the generator:

  1. Loads the JSON sample (file path or HTTP URL).
  2. Walks every field; infers its Swift type (Int, Double, String, Bool, arrays, nested structs).
  3. Marks fields Optional if they’re null in the sample.
  4. Converts snake_case JSON keys to camelCase Swift properties and emits a CodingKeys enum.

A single sample only sees one shape of the response. If the API returns null for a field that’s non-null in your sample, the generated type will fail to decode at runtime; open the file and add ? to the affected fields.

Terminal window
swiftspawn generate model Genre
swiftspawn generate model Movie --from sample.json --in Movies
swiftspawn generate model User --from https://api.example.com/users/1

With --in <feature>:

Features/<Feature>/Models/<Name>.swift

Without --in:

Models/<Name>.swift

None.

FlagDescription
--from <path-or-url>Path or URL to a JSON sample to infer the schema from.
--in <feature>Feature folder. Defaults to top-level Models/. Bare in <feature> also works.
--dry-runPreview without writing.
--forceOverwrite existing file.
CodeMeaning
2Invalid name.
5Not in a swiftspawn project.
9File not found at --from path.
10Failed to fetch URL at --from.
11Malformed JSON at --from.