feat: Improve example generation for API specs
- Enhance `extract_type_from_schema` to detail array and object types. - Introduce `generate_example_value` for dynamic example generation. - Add `generate_array_example` and `generate_map_example` helper functions. - Refactor `Method.example` to build JSON manually and use `json_str()`.
This commit is contained in:
@@ -7,28 +7,35 @@ import freeflowuniverse.herolib.schemas.jsonschema
|
||||
// In Method struct
|
||||
pub fn (method Method) example() (string, string) {
|
||||
// Extract user-provided examples from the OpenRPC specification
|
||||
// Build JSON manually to avoid json2.Any sumtype conflicts
|
||||
|
||||
mut example_params := map[string]json2.Any{}
|
||||
mut example_parts := []string{}
|
||||
for param in method.params {
|
||||
if param is ContentDescriptor {
|
||||
param_desc := param as ContentDescriptor
|
||||
if param_desc.schema is jsonschema.Schema {
|
||||
schema := param_desc.schema as jsonschema.Schema
|
||||
if schema.example.str() != '' {
|
||||
example_params[param_desc.name] = schema.example
|
||||
// Build JSON field manually to avoid sumtype conflicts
|
||||
example_parts << '"${param_desc.name}": ${schema.example.json_str()}'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
example_call := json.encode(example_params)
|
||||
example_call := if example_parts.len > 0 {
|
||||
'{${example_parts.join(', ')}}'
|
||||
} else {
|
||||
'{}'
|
||||
}
|
||||
|
||||
example_response := if method.result is ContentDescriptor {
|
||||
result_desc := method.result as ContentDescriptor
|
||||
if result_desc.schema is jsonschema.Schema {
|
||||
schema := result_desc.schema as jsonschema.Schema
|
||||
if schema.example.str() != '' {
|
||||
json.encode(schema.example)
|
||||
// Use json_str() to avoid json2.Any sumtype conflicts
|
||||
schema.example.json_str()
|
||||
} else {
|
||||
'{"result": "success"}'
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user