-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_openapi_import.py
More file actions
167 lines (142 loc) · 5.26 KB
/
Copy path07_openapi_import.py
File metadata and controls
167 lines (142 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""OpenAPI / JSON Schema Import — Generate Data from API Specs.
Real-world scenario: You have an OpenAPI specification for your REST API
and need to generate realistic test payloads. The OpenAPIParser reads your
spec (JSON or YAML), resolves $ref references, maps types and formats to
DataForge providers, and creates Schema objects that generate conforming data.
This example demonstrates:
- Parsing JSON Schema definitions
- Parsing OpenAPI 3.x specifications
- Resolving $ref references
- Generating data that conforms to the schema
- Type and format mapping (email, date-time, uuid, etc.)
"""
import json
from dataforge import DataForge
from dataforge.openapi import OpenAPIParser
forge = DataForge(seed=42)
parser = OpenAPIParser(forge)
# --- Example 1: Simple JSON Schema ---------------------------------------
print("=== Simple JSON Schema ===\n")
user_schema_def = {
"type": "object",
"properties": {
"name": {"type": "string"},
"email": {"type": "string", "format": "email"},
"website": {"type": "string", "format": "uri"},
"created_at": {"type": "string", "format": "date-time"},
"is_active": {"type": "boolean"},
},
}
user_schema = parser.from_json_schema(user_schema_def, name="User")
rows = user_schema.generate(count=5)
print("Generated users from JSON Schema:")
for row in rows:
print(f" {row}")
print()
# --- Example 2: Full OpenAPI 3.x specification ----------------------------
print("=== OpenAPI 3.x Specification ===\n")
openapi_doc = {
"openapi": "3.0.0",
"info": {"title": "E-Commerce API", "version": "1.0.0"},
"paths": {},
"components": {
"schemas": {
"Customer": {
"type": "object",
"properties": {
"id": {"type": "string", "format": "uuid"},
"first_name": {"type": "string"},
"last_name": {"type": "string"},
"email": {"type": "string", "format": "email"},
"phone": {"type": "string", "format": "phone"},
"city": {"type": "string"},
"created_at": {"type": "string", "format": "date-time"},
},
},
"Product": {
"type": "object",
"properties": {
"id": {"type": "string", "format": "uuid"},
"title": {"type": "string"},
"description": {"type": "string"},
"company": {"type": "string"},
"url": {"type": "string", "format": "uri"},
},
},
"Order": {
"type": "object",
"properties": {
"id": {"type": "string", "format": "uuid"},
"customer": {"$ref": "#/components/schemas/Customer"},
"created_at": {"type": "string", "format": "date-time"},
"ip_address": {"type": "string", "format": "ipv4"},
},
},
},
},
}
schemas = parser.from_openapi(openapi_doc)
print(f"Parsed {len(schemas)} schemas: {list(schemas.keys())}")
print()
# Generate data for each schema
for name, schema in schemas.items():
print(f"--- {name} ---")
rows = schema.generate(count=3)
for row in rows:
print(f" {row}")
print()
# --- Example 3: Schema with $ref references -------------------------------
print("=== $ref Resolution ===\n")
doc_with_refs = {
"openapi": "3.0.0",
"info": {"title": "Test API", "version": "1.0.0"},
"paths": {},
"components": {
"schemas": {
"Address": {
"type": "object",
"properties": {
"city": {"type": "string"},
"state": {"type": "string"},
"country": {"type": "string"},
"zip_code": {"type": "string"},
},
},
"Person": {
"type": "object",
"properties": {
"first_name": {"type": "string"},
"last_name": {"type": "string"},
"email": {"type": "string", "format": "email"},
# Nested $ref - the parser flattens this
"address": {"$ref": "#/components/schemas/Address"},
},
},
},
},
}
schemas = parser.from_openapi(doc_with_refs)
print(f"Schemas found: {list(schemas.keys())}")
for name, schema in schemas.items():
rows = schema.generate(count=2)
print(f"\n{name}:")
for row in rows:
print(f" {row}")
print()
# --- Example 4: Generate API test payloads --------------------------------
print("=== Generate API Test Payloads ===\n")
api_schema_def = {
"type": "object",
"properties": {
"username": {"type": "string"},
"email": {"type": "string", "format": "email"},
"password": {"type": "string", "format": "password"},
"ip_address": {"type": "string", "format": "ipv4"},
},
}
payload_schema = parser.from_json_schema(api_schema_def, name="CreateUser")
payloads = payload_schema.generate(count=3)
print("API test payloads (ready for POST requests):")
for i, payload in enumerate(payloads):
print(f"\n Request {i + 1}:")
print(f" {json.dumps(payload, indent=4)}")