Metadata-Version: 2.5
Name: contractradius
Version: 0.1.0
Summary: Usage-aware OpenAPI breaking-change detection: know which API changes actually break your frontend.
Project-URL: Homepage, https://github.com/example/contractradius
Project-URL: Issues, https://github.com/example/contractradius/issues
Author: Bindu
License-Expression: MIT
License-File: LICENSE
Keywords: api,breaking-change,ci,fastapi,openapi,static-analysis,typescript
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Quality Assurance
Classifier: Topic :: Software Development :: Testing
Requires-Python: >=3.11
Requires-Dist: httpx>=0.27
Requires-Dist: pydantic>=2.5
Requires-Dist: pyyaml>=6.0
Requires-Dist: tree-sitter-typescript>=0.21
Requires-Dist: tree-sitter>=0.22
Requires-Dist: typer>=0.12
Provides-Extra: dev
Requires-Dist: mypy>=1.9; extra == 'dev'
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: ruff>=0.4; extra == 'dev'
Requires-Dist: types-pyyaml; extra == 'dev'
Provides-Extra: rich
Requires-Dist: rich>=13.0; extra == 'rich'
Description-Content-Type: text/markdown

# ContractRadius

**Find the API changes that are most likely to affect your frontend — not just the changes that look dangerous in the API diff.**

When a backend API changes, existing tools can identify changes to the API contract, such as a removed response field or a newly required request field.

But a contract change does not necessarily mean that a frontend application is affected.

For example, suppose an API removes:

```json
{
  "name": "Bindu",
  "email": "bindu@example.com",
  "age": 25
}
```

and the new API no longer returns `age`.

A normal API diff can report:

```text
Breaking change:
User.age was removed
```

But what if the frontend never uses `age`?

ContractRadius goes one step further.

It compares:

```text
Old API
   +
New API
   +
Frontend source code
```

and determines whether the changed API fields are actually referenced by the frontend.

The goal is to reduce unnecessary warnings and highlight API changes that have **evidence of affecting real frontend code**.

For example:

```text
$ contractradius diff openapi_old.json openapi_new.json \
    --frontend ./frontend/src

ENDPOINT          FIELD          CHANGE         CONFIDENCE   LOCATION

GET /users/{id}   user.email     field_removed  0.8          src/api/users.ts:14

Total API changes: 6
Potentially breaking: 4
Frontend-impacting: 1
```

Instead of simply saying:

> "The API changed."

ContractRadius aims to tell the developer:

> **"This API field changed, and we found frontend code that appears to use it."**

---
## Usage

Once installed (`pip install contractradius`), running it takes two things: two OpenAPI spec files to compare, and your frontend source directory.

### 1. Get two specs to compare

Usually "before" (main/production) and "after" (your current changes). The quickest way is to curl them from a running server:

```bash
curl http://localhost:8000/openapi.json > openapi_old.json
# make your changes
curl http://localhost:8000/openapi.json > openapi_new.json
```

### 2. Run the diff

```bash
contractradius diff openapi_old.json openapi_new.json --frontend ./src
```

This prints only the breaking changes confirmed to hit real call sites in your frontend, with exact file and line numbers — not every technical change in the spec.

### 3. Other output formats

```bash
contractradius diff openapi_old.json openapi_new.json --frontend ./src --format json       # machine-readable
contractradius diff openapi_old.json openapi_new.json --frontend ./src --format markdown    # for docs/reports
contractradius diff openapi_old.json openapi_new.json --frontend ./src --format github       # PR comment body
```

### 4. Use in CI

```yaml
- run: pip install contractradius
- run: contractradius diff openapi_old.json openapi_new.json --frontend ./src
```

Exits non-zero if a real breaking change is found, so the step fails the build automatically. Add `--no-fail-ci` if you just want the report without failing the pipeline.

### 5. Configuration (optional)

If your API client isn't `axios`/`apiClient`/`api`/`client`/`http`/`httpClient`, or you want to ignore certain paths/fields, add a `.contractradius.yaml` to your repo root:

```yaml
client_names: [axios, myCustomClient]
ignore_paths: ["/internal/*"]
ignore_fields: ["*.debug_info"]
min_confidence: 0.5
fail_on: breaking   # breaking | potentially_breaking | none
```

No config file is required to get started — every setting has a sensible default.

### As a Python library

```python
from contractradius import analyze

report = analyze(
    old_spec="openapi_old.json",
    new_spec="openapi_new.json",
    frontend_src="./src",
)

for real_break in report.real_breaks:
    print(real_break.summary)
```
## How it works

```text
                OpenAPI
              Old + New
                   │
                   ▼
             API Diff Engine
                   │
                   │
Frontend ───────► Usage Analyzer
                   │
                   ▼
          Impact Correlation
                   │
                   ▼
        Frontend-impacting changes
                   │
             ┌─────┴─────┐
             ▼           ▼
          Report        AI
                       Explain
```

### 1. Compare the API

ContractRadius reads the old and new OpenAPI specifications and identifies changes such as:

* removed fields
* changed field types
* newly required fields
* changed endpoints
* changed request/response schemas

### 2. Analyze frontend code

It analyzes TypeScript/TSX source code and looks for common API usage patterns such as:

```typescript
fetch(...)
```

```typescript
axios.get(...)
```

```typescript
axios.post(...)
```

and common API client objects such as:

```typescript
apiClient
api
client
```

It then looks for code that accesses response fields, for example:

```typescript
response.data.user.email
```

or:

```typescript
const { data: { user } } = await axios.get(...)
```

### 3. Connect the two

ContractRadius compares:

```text
API changes
     ↓
Fields affected by those changes
     ↓
Fields referenced in frontend code
```

If an API field changed but there is no detected frontend usage, it can be reported with lower priority or excluded according to configuration.

If frontend usage is found, ContractRadius reports the source file and line where the field is referenced.

Because static analysis cannot always determine application behavior with certainty, findings should include a **confidence score** rather than claiming that every detected change will definitely cause a runtime failure.

---

# AI-assisted explanations

AI is an optional feature, not the core detection mechanism.

The Python analysis engine first determines which API changes appear to affect the frontend.

Only those findings are sent to the AI model.

The AI can then produce a human-friendly explanation such as:

```text
⚠ Potential frontend impact

The `email` field was removed from GET /users/{id}.

ContractRadius found a reference to `user.email`
in:

src/api/users.ts:14

Recommended action:
Either keep the field in the API response or update
the frontend code before deploying the change.
```

The AI should **not decide whether a breaking change exists**.

The deterministic analysis should make that determination first, and AI should only explain the already-detected findings.

This keeps the AI feature useful while reducing the possibility of AI-generated false warnings.

---

# CLI

A developer can run:

```bash
contractradius diff openapi_old.json openapi_new.json \
    --frontend ./frontend/src
```

The result can eventually be produced as:

* terminal output
* JSON
* Markdown
* GitHub pull-request comments
* CI exit codes

---

# Python API

ContractRadius should also be usable as a normal Python library:

```python
from contractradius import analyze

report = analyze(
    old_spec="openapi_old.json",
    new_spec="openapi_new.json",
    frontend_src="./frontend/src",
)

for finding in report.findings:
    print(finding.summary)
```

---

# Initial Scope

The first version should intentionally support a limited set of frontend patterns.

For example:

* TypeScript
* TSX
* `fetch`
* Axios
* common API client objects
* common response-field access patterns

It should clearly report when a pattern cannot be analyzed instead of pretending to understand arbitrary JavaScript/TypeScript code.

Future versions can improve:

* TypeScript type awareness
* more HTTP clients
* more complex data-flow analysis
* framework support
* backend consumer analysis
* AI-assisted fixes
* IDE integrations

---

# Core Philosophy

ContractRadius is **not another OpenAPI diff tool**.

It is an attempt to answer a more useful question:

> **"Which API changes have evidence of affecting the code that consumes this API?"**

The core value comes from combining:

**API contract analysis + frontend static analysis + impact correlation**

with optional **AI-assisted explanations**.

The project should remain focused on this problem rather than becoming a general-purpose AI agent framework.

## Contributing

See [`docs/contributing.md`](docs/contributing.md).

## License

MIT — see [`LICENSE`](LICENSE).
