Skip to main content

Code generation for surety contracts

Project description

surety-scaffold

Code generation toolkit for the surety contract-testing framework.

Bootstraps surety schemas, contracts, callers, and test scaffolding from existing JSON payloads, JSON Schema, and OpenAPI specs — so you can start contract-based testing without writing boilerplate by hand.


Installation

pip install surety-scaffold

Requires surety>=0.0.4.


Features

Extract contract from JSON

Turn a raw JSON payload into surety Dictionary class definitions.

Programmatic:

import json
from surety.scaffold import extract_from_json

payload = json.dumps({
    "order_id": 42,
    "status": "pending",
    "total": 199.99,
    "is_express": False,
    "customer": {
        "customer_id": 7,
        "email": "jane@example.com"
    },
    "items": [
        {"sku": "ABC-1", "qty": 2}
    ]
})

print(extract_from_json(payload, class_name="Order"))

Output:

from surety import Array, Bool, Dictionary, Float, Int, String


class Customer(Dictionary):
    CustomerId = Int(name='customer_id', required=True)
    Email = String(name='email', required=True)


class Items(Dictionary):
    Sku = String(name='sku', required=True)
    Qty = Int(name='qty', required=True)


class Order(Dictionary):
    OrderId = Int(name='order_id', required=True)
    Status = String(name='status', required=True)
    Total = Float(name='total', required=True)
    IsExpress = Bool(name='is_express', required=True)
    Customer = Customer(name='customer', required=True)
    Items = Array(Items, name='items', required=True)

Type inference rules:

JSON value Surety type
true / false Bool
integer Int
float Float
UUID string Uuid
ISO 8601 datetime or date string DateTime
any other string String
object {} nested Dictionary subclass
array of objects Array(NestedClass, ...)
array of primitives Array(PrimitiveType, ...)
null String(allow_none=True)

Nested objects produce their own Dictionary subclass and are always emitted before the class that references them. Arrays of objects merge all items across the array to capture the full set of fields. Duplicate class names (same key appearing at different nesting levels) are disambiguated automatically with a numeric suffix.

Key naming: JSON keys are converted to PascalCase for both the class attribute name and the generated class name. snake_case, camelCase, and kebab-case are all handled.


CLI:

# Pass JSON as an argument
surety-scaffold '{"order_id": 1, "status": "pending"}' --class-name Order

# Pipe from a file or command
cat response.json | surety-scaffold --class-name Order

# Skip clipboard copy
surety-scaffold '{"x": 1}' --no-clipboard

The result is printed to stdout and automatically copied to the clipboard (pbcopy on macOS, xclip on Linux).

usage: surety-scaffold [-h] [--class-name CLASS_NAME] [--no-clipboard] [json]

positional arguments:
  json                  JSON string to extract from. Reads from stdin if omitted.

options:
  --class-name, -c      Name for the top-level generated class (default: Schema).
  --no-clipboard        Do not copy the result to the clipboard.

Roadmap

The following features are planned for this library. They complement the core surety framework with static analysis, code generation, and developer tooling.

In scope for surety-scaffold

# Feature Status
1 Extract contract from JSON — infer surety schemas from raw API responses done
4 Parse JSON Schema / OpenAPI — generate surety Dictionary classes from JSON Schema or OpenAPI 3.x specs, resolving $ref and mapping constraints planned
5 Generate callers — produce ApiContract boilerplate and caller function stubs from OpenAPI operations planned
12 Generate test scenarios (basic) — scaffold positive pytest test cases (required fields, full fields, with_values overrides) for a given contract planned

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

surety_scaffold-0.0.2.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

surety_scaffold-0.0.2-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file surety_scaffold-0.0.2.tar.gz.

File metadata

  • Download URL: surety_scaffold-0.0.2.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for surety_scaffold-0.0.2.tar.gz
Algorithm Hash digest
SHA256 6e873edc46541d4a8cb377f582c731aeef5e5968c50a7e944943f1df4b1f0ac4
MD5 9ca0fc27c2e652844b8f21ca93b3cccc
BLAKE2b-256 cdaa5527d6b5f7a42610c806748b848357ee68a6aaeda2decff9f9bf01ced63d

See more details on using hashes here.

File details

Details for the file surety_scaffold-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for surety_scaffold-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a6596396f76dc57bc751a21d07da8cf8888737fdda7fb777cafeff2ca6503e0d
MD5 fa2d6d0221cf515928098ae879c7fef7
BLAKE2b-256 c9687bc4bfde6bbe54e311a7f110f17574ceb4b9920c3253dcbe8046caf905d8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page