oaspec
Generate Gleam code from an OpenAPI 3.x document.
oaspec is a command line tool (gleam run -m oaspec -- generate), and
the same steps are available as functions: read a document, resolve
its $refs, generate the modules and write them.
write stores the modules as generated, without formatting them. The
CLI runs gleam format on the output directories afterwards; do the
same if the files should match what oaspec generate writes.
import oaspec
import oaspec/config
pub fn main() {
let assert Ok(cfg) = config.load("oaspec.yaml")
let assert Ok(spec) = oaspec.parse_file(cfg.input)
let assert Ok(generated) = oaspec.generate(spec, cfg)
let assert Ok(_paths) = oaspec.write(generated)
}
Types
A reason generate or validate refused a document.
pub type GenerateError {
InvalidSpec(errors: List(openapi.Diagnostic))
InvalidConfig(error: config.ConfigError)
}
Constructors
-
InvalidSpec(errors: List(openapi.Diagnostic))The document uses features oaspec cannot generate code for, or has references that do not resolve. Each problem is one diagnostic.
-
InvalidConfig(error: config.ConfigError)The config’s output directories do not fit its
package: the server directory must end in the package path, the client directory in the package path or<package>_client, and asrcdirectory may only be the direct parent of the package directory. These are the checks the CLI runs after loadingoaspec.yaml.
The modules generate produced, and the warnings it reported.
pub type Generated {
Generated(
files: List(GeneratedFile),
warnings: List(openapi.Diagnostic),
)
}
Constructors
-
Generated( files: List(GeneratedFile), warnings: List(openapi.Diagnostic), )
One generated module.
pub type GeneratedFile {
GeneratedFile(
path: String,
content: String,
write_mode: WriteMode,
)
}
Constructors
-
GeneratedFile( path: String, content: String, write_mode: WriteMode, )Arguments
- path
-
Where the file goes: the output directory from the config joined with the module path, such as
./src/api/types.gleam.
A reason write stopped. Files written before the failure stay on
disk.
pub type WriteError {
CannotCreateDirectory(
path: String,
reason: simplifile.FileError,
)
CannotWriteFile(path: String, reason: simplifile.FileError)
}
Constructors
-
CannotCreateDirectory(path: String, reason: simplifile.FileError) -
CannotWriteFile(path: String, reason: simplifile.FileError)
Values
pub fn generate(
from spec: openapi.OpenApiSpec(openapi.Unresolved),
with config: config.Config,
) -> Result(Generated, GenerateError)
Generates the Gleam modules for a document, without writing them.
config decides what is generated (mode, include,
handler_layout) and where each file goes. Its output directories are
checked against its package first, as the CLI does. Problems that do
not stop generation come back in warnings.
Examples
oaspec.generate(spec, cfg)
// -> Ok(Generated(files: [GeneratedFile(path: "./src/api/types.gleam", ..), ..], warnings: []))
// The document has a requestBody type oaspec cannot generate.
oaspec.generate(spec, cfg)
// -> Error(InvalidSpec([Diagnostic(..)]))
// cfg.package is "api" but cfg.output_server is "./src/server"
oaspec.generate(spec, cfg)
// -> Error(InvalidConfig(config.InvalidValue(field: "output.server", ..)))
pub fn main() -> Nil
Runs the oaspec command line tool with the program’s arguments.
Errors go to stderr and requested output, such as --help, to stdout.
The process exits with status 1 on failure.
pub fn parse_file(
path: String,
) -> Result(
openapi.OpenApiSpec(openapi.Unresolved),
openapi.Diagnostic,
)
Reads an OpenAPI document from a YAML (.yaml, .yml) or JSON
(.json) file.
$refs to other local files are loaded and merged in; URLs are not
followed. The result is not resolved yet: pass it to generate, or to
resolve to inspect it.
A file larger than 16 MiB is refused with a parse_limit_exceeded
diagnostic before it is parsed.
Examples
oaspec.parse_file("openapi.yaml")
// -> Ok(OpenApiSpec(openapi: "3.0.3", ..))
oaspec.parse_file("missing.yaml")
// -> Error(Diagnostic(code: "file_error", ..))
pub fn parse_string(
content: String,
) -> Result(
openapi.OpenApiSpec(openapi.Unresolved),
openapi.Diagnostic,
)
Parses an OpenAPI document from a string. Content that starts with {
or [ is read as JSON, anything else as YAML.
$refs to other files are left as they are. Content larger than 16 MiB
is refused with a parse_limit_exceeded diagnostic before it is parsed.
Examples
oaspec.parse_string("openapi: 3.0.3\ninfo: {title: Pets, version: '1'}\npaths: {}")
// -> Ok(OpenApiSpec(openapi: "3.0.3", ..))
oaspec.parse_string("openapi: 3.0.3\npaths: {}")
// -> Error(Diagnostic(code: "missing_field", ..))
pub fn resolve(
spec: openapi.OpenApiSpec(openapi.Unresolved),
) -> Result(
openapi.OpenApiSpec(openapi.Resolved),
openapi.Diagnostic,
)
Resolves the $refs in a parsed document so every RefOr is a
Value, after rewriting OAS 3.1 forms to their 3.0 equivalents.
Fails on a $ref that points at nothing or on a cycle of references,
with the first problem found.
Examples
let assert Ok(spec) = oaspec.parse_file("openapi.yaml")
oaspec.resolve(spec)
// -> Ok(OpenApiSpec(..))
// components.schemas.Pet is { $ref: '#/components/schemas/Missing' }
oaspec.resolve(spec)
// -> Error(Diagnostic(code: "resolve_error", ..))
pub fn validate(
from spec: openapi.OpenApiSpec(openapi.Unresolved),
with config: config.Config,
) -> Result(List(openapi.Diagnostic), GenerateError)
Runs the same checks as generate, including the config checks,
without generating code, and returns the warnings.
Examples
oaspec.validate(spec, cfg)
// -> Ok([])
oaspec.validate(spec, cfg)
// -> Error(InvalidSpec([Diagnostic(..)]))
pub fn write(
generated: Generated,
) -> Result(List(String), WriteError)
Writes the generated files, creating directories as needed, and returns the paths it wrote in order.
A SkipIfExists file that already exists is left alone and is not in
the returned list.
The files are written unformatted. The CLI runs gleam format on the
output directories after writing; run it yourself to get the same
result.
Examples
oaspec.write(generated)
// -> Ok(["./src/api/types.gleam", "./src/api/decode.gleam", ..])
// ./src is read-only
oaspec.write(generated)
// -> Error(CannotCreateDirectory("./src/api", simplifile.Eacces))