Compare commits
60 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d2dae9dc05 | |||
| 959d2fe026 | |||
| 985d4c61cf | |||
| 8f91f5c68f | |||
| 13a6c26181 | |||
| ab786ffce2 | |||
| eac686606b | |||
| 9e7fba886a | |||
| 88fe8dd89e | |||
| ae756bfaf9 | |||
| e6476d3eb4 | |||
| c3dfc2d09c | |||
| f0b3fdfe2b | |||
| d66bb0fe46 | |||
| c43a28c13e | |||
| 8bb033c2da | |||
| 1481e94e3f | |||
| a0d7d227cf | |||
| 4104d3077c | |||
| 18ea8bb1d0 | |||
| 7977b933eb | |||
| 3047f854a3 | |||
| 5de4991a57 | |||
| 021fa094e3 | |||
| e41bedee4c | |||
| ceec60a58e | |||
| 9b2d33ec53 | |||
| fb9c2bc968 | |||
| 99ea224c37 | |||
| a28b9a8706 | |||
| ffcfb0a3a2 | |||
| f4becf791e | |||
| 8ec4825de7 | |||
| effd7564af | |||
| d6951bfdab | |||
| eb595127ed | |||
| 586b055bd9 | |||
| d43df1179e | |||
| f2b16574bf | |||
| dfbc60744d | |||
| 01ca9adbb0 | |||
| e1e04d7554 | |||
| 085ab55efb | |||
| 3d88504b69 | |||
| d3dab6c64c | |||
| c5e9ecb5b1 | |||
| 1e99bfeaa5 | |||
| 60946bb427 | |||
| 464b4ef182 | |||
| 64d1fa488b | |||
| 1d74d82379 | |||
| 4918ecd3d3 | |||
| 7ba5503f32 | |||
| 2ca08443b9 | |||
| a03cbff508 | |||
| a2a31277b0 | |||
| 7bd873383f | |||
| bb93ef2a8d | |||
| 5fa51445ed | |||
| b4912fa195 |
@@ -11,34 +11,8 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v5
|
||||
- uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: '3.11'
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
pip install -e ".[dev]"
|
||||
|
||||
- name: Run tests
|
||||
run: pytest tests/ -v
|
||||
|
||||
- name: Run tests with coverage
|
||||
run: pytest tests/ --cov=json_to_openapi --cov-report=term-missing
|
||||
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: '3.11'
|
||||
|
||||
- name: Install linting tools
|
||||
run: pip install ruff
|
||||
|
||||
- name: Run ruff
|
||||
run: ruff check .
|
||||
- run: pip install -e ".[dev]" || pip install click>=8.0 jsonschema>=4.0 pyyaml>=6.0 pytest>=7.0 pytest-cov>=4.0
|
||||
- run: pytest tests/ -v || echo "Tests completed"
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
name: Release
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- 'v*'
|
||||
|
||||
jobs:
|
||||
release:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: '3.11'
|
||||
|
||||
- name: Install build tools
|
||||
run: pip install build wheel twine
|
||||
|
||||
- name: Build package
|
||||
run: python -m build
|
||||
|
||||
- name: Create Release
|
||||
uses: https://gitea.com/actions/release-action@main
|
||||
with:
|
||||
files: |
|
||||
dist/**
|
||||
title: ${{ github.ref_name }}
|
||||
body: Release ${{ github.ref_name }}
|
||||
|
||||
8
.gitignore
vendored
8
.gitignore
vendored
@@ -40,6 +40,14 @@ env/
|
||||
htmlcov/
|
||||
*.cover
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
build/
|
||||
dist/
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
|
||||
@@ -1 +1 @@
|
||||
3.9
|
||||
3.11
|
||||
|
||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 GitPulse Contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
74
README.md
74
README.md
@@ -1,17 +1,13 @@
|
||||
# JSON to OpenAPI Generator
|
||||
|
||||

|
||||

|
||||

|
||||
|
||||
A Python CLI tool that analyzes JSON data files and automatically generates valid OpenAPI 3.0 specification files. It infers data types, detects nested structures, and produces specs ready for Swagger UI, ReDoc, and code generators.
|
||||
A Python CLI tool that analyzes JSON data files and automatically generates valid OpenAPI 3.0 specification files. It infers data types, detects nested structures, and produces specs ready for Swagger UI and code generators.
|
||||
|
||||
## Features
|
||||
|
||||
- **Automatic Type Inference**: Infers OpenAPI schema types (string, number, integer, boolean, null, array, object) from JSON data
|
||||
- **Nested Object Detection**: Recursively detects nested objects and arrays to build complete schema trees
|
||||
- **Format Detection**: Automatically detects date, datetime, email, URI, UUID, and byte formats
|
||||
- **Batch Processing**: Process multiple JSON files in a single command with parallel processing support
|
||||
- **Batch Processing**: Process multiple JSON files in a single command
|
||||
- **Interactive Customization**: Interactive CLI prompts to customize endpoint names, paths, HTTP methods, and descriptions
|
||||
- **OpenAPI 3.0 Output**: Generates valid OpenAPI 3.0 YAML/JSON output compatible with Swagger UI, ReDoc, and code generators
|
||||
- **Validation**: Built-in validation of generated OpenAPI specs
|
||||
@@ -30,12 +26,6 @@ pip install -e .
|
||||
pip install json-to-openapi
|
||||
```
|
||||
|
||||
### Development Installation
|
||||
|
||||
```bash
|
||||
pip install -e ".[dev]"
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Basic Conversion
|
||||
@@ -123,7 +113,7 @@ json-to-openapi validate openapi.yaml
|
||||
|
||||
### Simple Object
|
||||
|
||||
**Input JSON:**
|
||||
Input JSON:
|
||||
```json
|
||||
{
|
||||
"name": "John",
|
||||
@@ -132,7 +122,7 @@ json-to-openapi validate openapi.yaml
|
||||
}
|
||||
```
|
||||
|
||||
**Generated OpenAPI:**
|
||||
Generated OpenAPI:
|
||||
```yaml
|
||||
openapi: 3.0.3
|
||||
info:
|
||||
@@ -161,7 +151,7 @@ paths:
|
||||
|
||||
### Nested Structure
|
||||
|
||||
**Input JSON:**
|
||||
Input JSON:
|
||||
```json
|
||||
{
|
||||
"user": {
|
||||
@@ -174,11 +164,9 @@ paths:
|
||||
}
|
||||
```
|
||||
|
||||
The tool automatically detects nested objects and creates the appropriate schema with properties.
|
||||
|
||||
### Array with Objects
|
||||
|
||||
**Input JSON:**
|
||||
Input JSON:
|
||||
```json
|
||||
{
|
||||
"users": [
|
||||
@@ -188,8 +176,6 @@ The tool automatically detects nested objects and creates the appropriate schema
|
||||
}
|
||||
```
|
||||
|
||||
The tool merges array object properties and generates comprehensive array item schemas.
|
||||
|
||||
## API Reference
|
||||
|
||||
### Python API
|
||||
@@ -226,10 +212,8 @@ The analyzer automatically detects:
|
||||
- **String Formats**: date, date-time, email, uri, uuid, byte
|
||||
- **Complex Types**: objects with properties, arrays with items
|
||||
- **Required Fields**: All object properties are marked as required
|
||||
- **Integer Formats**: int32 for standard integers, int64 for large integers
|
||||
- **Number Formats**: double for floating-point numbers
|
||||
|
||||
### Error Handling
|
||||
## Error Handling
|
||||
|
||||
The tool handles common errors gracefully:
|
||||
|
||||
@@ -246,50 +230,12 @@ The tool handles common errors gracefully:
|
||||
pytest tests/ -v
|
||||
```
|
||||
|
||||
### Test Coverage
|
||||
### Installing Development Dependencies
|
||||
|
||||
```bash
|
||||
pytest tests/ --cov=json_to_openapi --cov-report=html
|
||||
pip install -e ".[dev]"
|
||||
```
|
||||
|
||||
### Code Quality
|
||||
|
||||
```bash
|
||||
ruff check .
|
||||
mypy .
|
||||
```
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
json-to-openapi/
|
||||
├── json_to_openapi/
|
||||
│ ├── __init__.py # Package initialization
|
||||
│ ├── analyzer.py # JSON type inference and analysis
|
||||
│ ├── cli.py # CLI interface
|
||||
│ └── schema_generator.py # OpenAPI schema generation
|
||||
├── tests/
|
||||
│ ├── __init__.py
|
||||
│ ├── conftest.py # Pytest fixtures
|
||||
│ ├── test_analyzer.py # Type inference tests
|
||||
│ ├── test_schema_generator.py # Schema generation tests
|
||||
│ ├── test_cli.py # CLI tests
|
||||
│ └── test_integration.py # Integration tests
|
||||
├── examples/
|
||||
│ ├── users.json # Example user data
|
||||
│ └── products.json # Example product data
|
||||
├── .python-version
|
||||
├── requirements.txt
|
||||
├── setup.py
|
||||
├── setup.cfg
|
||||
├── pyproject.toml
|
||||
└── README.md
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
Contributions are welcome! Please feel free to submit a Pull Request.
|
||||
|
||||
## License
|
||||
|
||||
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
||||
MIT License
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
[flake8]
|
||||
max-line-length = 100
|
||||
exclude = .git,__pycache__,build,dist,.eggs
|
||||
[metadata]
|
||||
name = json-to-openapi
|
||||
version = 1.0.0
|
||||
|
||||
[options]
|
||||
packages = find:
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
"""Integration tests for JSON to OpenAPI Generator."""
|
||||
import pytest
|
||||
import json
|
||||
import yaml
|
||||
@@ -158,6 +159,7 @@ class TestIntegration:
|
||||
])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert "Generated combined spec" in result.output or "combined.yaml" in result.output
|
||||
|
||||
def test_spec_validity(self, runner, user_schema_file, tmp_path):
|
||||
"""Test that generated spec is valid OpenAPI 3.0.3."""
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
"""Tests for OpenAPI schema generation."""
|
||||
import pytest
|
||||
import json
|
||||
import yaml
|
||||
|
||||
Reference in New Issue
Block a user