Files
herolib/lib/schemas/openrpc/custom.v
Mahmoud-Emad 44ec137db5 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()`.
2025-09-22 16:04:26 +03:00

51 lines
1.3 KiB
V

module openrpc
import json
import x.json2
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_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() != '' {
// Build JSON field manually to avoid sumtype conflicts
example_parts << '"${param_desc.name}": ${schema.example.json_str()}'
}
}
}
}
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() != '' {
// Use json_str() to avoid json2.Any sumtype conflicts
schema.example.json_str()
} else {
'{"result": "success"}'
}
} else {
'{"result": "success"}'
}
} else {
'{"result": "success"}'
}
return example_call, example_response
}