@@ -28,7 +28,6 @@ use openapiv3::{
28
28
RequestBody , Response , Responses , StatusCode , Example
29
29
} ;
30
30
use clients_schema:: SchemaExample ;
31
- use serde_json:: json;
32
31
33
32
use crate :: components:: TypesAndComponents ;
34
33
@@ -40,12 +39,12 @@ pub fn add_endpoint(
40
39
out : & mut Paths ,
41
40
) -> anyhow:: Result < ( ) > {
42
41
if endpoint. request . is_none ( ) {
43
- tracing:: warn!( "Endpoint {} is missing a request -- ignored" , & endpoint. name) ;
42
+ // tracing::warn!("Endpoint {} is missing a request -- ignored", &endpoint.name);
44
43
return Ok ( ( ) ) ;
45
44
}
46
45
47
46
if endpoint. response . is_none ( ) {
48
- tracing:: warn!( "Endpoint {} is missing a response -- ignored" , & endpoint. name) ;
47
+ // tracing::warn!("Endpoint {} is missing a response -- ignored", &endpoint.name);
49
48
return Ok ( ( ) ) ;
50
49
}
51
50
@@ -122,28 +121,48 @@ pub fn add_endpoint(
122
121
// This function converts the IndexMap<String, SchemaExample> examples of
123
122
// schema.json to IndexMap<String, ReferenceOr<Example>> which is the format
124
123
// that OpenAPI expects.
125
- fn get_openapi_examples ( schema_examples : IndexMap < String , SchemaExample > ) -> IndexMap < String , ReferenceOr < Example > > {
124
+ fn get_openapi_examples ( schema_examples : & IndexMap < String , SchemaExample > ) -> IndexMap < String , ReferenceOr < Example > > {
126
125
let mut openapi_examples = indexmap ! { } ;
127
126
for ( name, schema_example) in schema_examples {
127
+ let example = match & schema_example. value {
128
+ None => None ,
129
+ // Examples should be objects - https://spec.openapis.org/oas/v3.1.1.html#example-object
130
+ // Disabled for now, as some examples use multi-line json as in the Kibana console.
131
+ Some ( text) => Some ( serde_json:: Value :: String ( text. clone ( ) ) ) ,
132
+ // Some(text) => {
133
+ // match serde_json::from_str::<serde_json::Value>(&text) {
134
+ // Ok(json) => {
135
+ // Some(json)
136
+ // }
137
+ // // Cannot parse json: assume it's text (e.g. cat requests)
138
+ // // but should be validated by looking at the media-type
139
+ // Err(err) => {
140
+ // tracing::warn!("Cannot parse example: {}\n{}", err, text);
141
+ // Some(serde_json::Value::String(text.clone()))
142
+ // }
143
+ // }
144
+ // }
145
+ } ;
146
+
128
147
let openapi_example = Example {
129
- value : Some ( json ! ( schema_example . value ) ) ,
148
+ value : example ,
130
149
description : schema_example. description . clone ( ) ,
131
150
summary : schema_example. summary . clone ( ) ,
132
151
external_value : None ,
133
152
extensions : Default :: default ( ) ,
134
153
} ;
135
154
openapi_examples. insert ( name. clone ( ) , ReferenceOr :: Item ( openapi_example) ) ;
136
155
}
137
- return openapi_examples;
156
+ openapi_examples
138
157
}
139
158
140
-
141
- let mut request_examples: IndexMap < String , ReferenceOr < Example > > = indexmap ! { } ;
142
159
// If this endpoint request has examples in schema.json, convert them to the
143
160
// OpenAPI format and add them to the endpoint request in the OpenAPI document.
144
- if request. examples . is_some ( ) {
145
- request_examples = get_openapi_examples ( request. examples . as_ref ( ) . unwrap ( ) . clone ( ) ) ;
146
- }
161
+ let request_examples = if let Some ( examples) = & request. examples {
162
+ get_openapi_examples ( examples)
163
+ } else {
164
+ IndexMap :: new ( )
165
+ } ;
147
166
148
167
let request_body = tac. convert_request ( request) ?. map ( |schema| {
149
168
let media = MediaType {
@@ -176,12 +195,13 @@ pub fn add_endpoint(
176
195
// FIXME: buggy for responses with no body
177
196
// TODO: handle binary responses
178
197
let response_def = tac. model . get_response ( endpoint. response . as_ref ( ) . unwrap ( ) ) ?;
179
- let mut response_examples: IndexMap < String , ReferenceOr < Example > > = indexmap ! { } ;
180
198
// If this endpoint response has examples in schema.json, convert them to the
181
199
// OpenAPI format and add them to the endpoint response in the OpenAPI document.
182
- if response_def. examples . is_some ( ) {
183
- response_examples = get_openapi_examples ( response_def. examples . as_ref ( ) . unwrap ( ) . clone ( ) ) ;
184
- }
200
+ let response_examples = if let Some ( examples) = & response_def. examples {
201
+ get_openapi_examples ( examples)
202
+ } else {
203
+ IndexMap :: new ( )
204
+ } ;
185
205
let response = Response {
186
206
description : "" . to_string ( ) ,
187
207
headers : Default :: default ( ) ,
0 commit comments