satay-codegen 0.7.0

Generate Rust client code from OpenAPI 3.1 documents
Documentation
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use std::fs;

use crate::ast::*;
use crate::common::*;

#[test]
fn inline_enum_generates_proper_enum_types() {
    let files = satay_codegen::generate(INLINE_ENUM).expect("generate inline-enum fixture");

    let types_rs = parse_rust(find_file(&files, "types.rs"));
    let item = find_struct(&types_rs, "Item");
    assert_field(item, "category", "ItemCategory");
    assert_field(item, "condition", "ItemCondition");

    let category = find_enum(&types_rs, "ItemCategory");
    assert_eq!(variant_names(category), ["Electronics", "Clothing", "Food"]);
    let category_as_str = find_method(&types_rs, "ItemCategory", "as_str");
    assert!(is_pub(&category_as_str.vis));
    assert_eq!(
        norm(&category_as_str.sig),
        norm_str("const fn as_str(&self) -> &'static str")
    );

    let condition = find_enum(&types_rs, "ItemCondition");
    assert_eq!(variant_names(condition), ["New", "Used", "Refurbished"]);
    assert!(!contains_tokens(&types_rs, "serde ( other )"));
}

#[test]
fn x_satay_enum_variants_generate_named_variants() {
    let files = satay_codegen::generate(
        r#"
openapi: 3.1.0
info:
  title: Enum Variants API
  version: 1.0.0
paths:
  /arrival:
    get:
      operationId: getArrival
      responses:
        '200':
          description: Arrival
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/BusArrivalTiming'
components:
  schemas:
    BusArrivalTiming:
      type: object
      required:
        - Type
      properties:
        Type:
          type: string
          description: Vehicle type.
          enum:
            - SD
            - DD
            - BD
            - ""
          example: SD
          x-satay:
            enum-variants:
              SD: SingleDecker
              DD: DoubleDecker
              BD: Bendy
              "": Unknown
"#,
    )
    .expect("generate enum variants fixture");

    let types_rs = parse_rust(find_file(&files, "types.rs"));
    let timing = find_enum(&types_rs, "BusArrivalTimingType");
    assert_eq!(
        variant_names(timing),
        ["SingleDecker", "DoubleDecker", "Bendy", "Unknown"]
    );
    assert_attr_contains(
        &variant(timing, "SingleDecker").attrs,
        "cfg_attr",
        r#"serde(rename = "SD")"#,
    );
    assert_attr_contains(
        &variant(timing, "DoubleDecker").attrs,
        "cfg_attr",
        r#"serde(rename = "DD")"#,
    );
    assert_attr_contains(
        &variant(timing, "Bendy").attrs,
        "cfg_attr",
        r#"serde(rename = "BD")"#,
    );
    assert_attr_contains(
        &variant(timing, "Unknown").attrs,
        "cfg_attr",
        r#"serde(rename = "")"#,
    );
    assert!(!contains_tokens(&types_rs, "serde ( other )"));
}

#[test]
fn closed_enum_can_generate_other_variant_for_other_wire_value() {
    let files = satay_codegen::generate(
        r#"
openapi: 3.1.0
info:
  title: Reason API
  version: 1.0.0
paths:
  /reason:
    get:
      operationId: getReason
      responses:
        '200':
          description: Reason
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Reason'
components:
  schemas:
    Reason:
      type: string
      enum:
        - content_filter
        - other
"#,
    )
    .expect("generate closed enum with other fixture");

    let types_rs = parse_rust(find_file(&files, "types.rs"));
    let reason = find_enum(&types_rs, "Reason");
    assert_eq!(variant_names(reason), ["ContentFilter", "Other"]);
}

#[test]
fn any_of_string_and_enum_generates_open_string_enum() {
    let files = satay_codegen::generate(
        r#"
openapi: 3.1.0
info:
  title: Audio API
  version: 1.0.0
paths:
  /transcription:
    get:
      operationId: getTranscription
      responses:
        '200':
          description: Transcription
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AudioTranscription'
components:
  schemas:
    AudioTranscription:
      type: object
      properties:
        model:
          anyOf:
            - type: string
            - type: string
              description: Known transcription models.
              enum:
                - whisper-1
                - gpt-4o-mini-transcribe
                - gpt-4o-mini-transcribe-2025-12-15
                - gpt-4o-transcribe
                - gpt-4o-transcribe-diarize
                - gpt-realtime-whisper
"#,
    )
    .expect("generate open string enum fixture");

    let types_rs = parse_rust(find_file(&files, "types.rs"));
    let transcription = find_struct(&types_rs, "AudioTranscription");
    assert_field(transcription, "model", "Option<AudioTranscriptionModel>");

    let model = find_enum(&types_rs, "AudioTranscriptionModel");
    assert_doc(&model.attrs, "Known transcription models.");
    assert_eq!(
        variant_names(model),
        [
            "Whisper1",
            "Gpt4oMiniTranscribe",
            "Gpt4oMiniTranscribe20251215",
            "Gpt4oTranscribe",
            "Gpt4oTranscribeDiarize",
            "GptRealtimeWhisper",
            "Other"
        ]
    );
    let model_as_str = find_method(&types_rs, "AudioTranscriptionModel", "as_str");
    assert!(is_pub(&model_as_str.vis));
    assert_eq!(
        norm(&model_as_str.sig),
        norm_str("fn as_str(&self) -> &str")
    );
    assert_eq!(norm(&variant(model, "Other").fields), norm_str("(String)"));
    assert!(contains_tokens(
        &types_rs,
        "impl serde::Serialize for AudioTranscriptionModel"
    ));
    assert!(contains_tokens(
        &types_rs,
        "impl < 'de > serde::Deserialize < 'de > for AudioTranscriptionModel"
    ));
}

#[test]
fn open_string_enum_mangles_other_known_value_and_keeps_fallback() {
    let files = satay_codegen::generate(
        r#"
openapi: 3.1.0
info:
  title: Event API
  version: 1.0.0
paths:
  /event:
    get:
      operationId: getEvent
      responses:
        '200':
          description: Event
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Event'
components:
  schemas:
    Event:
      type: object
      properties:
        reason:
          anyOf:
            - type: string
            - type: string
              enum:
                - content_filter
                - other
"#,
    )
    .expect("generate open string enum with other fixture");

    let types_rs = parse_rust(find_file(&files, "types.rs"));
    let event = find_struct(&types_rs, "Event");
    assert_field(event, "reason", "Option<EventReason>");

    let reason = find_enum(&types_rs, "EventReason");
    assert_eq!(variant_names(reason), ["ContentFilter", "Other_2", "Other"]);
    assert_eq!(norm(&variant(reason, "Other").fields), norm_str("(String)"));
}

#[test]
fn generated_inline_enum_compiles_and_rejects_unknown() {
    let files = satay_codegen::generate(INLINE_ENUM).expect("generate inline-enum fixture");

    let temp = tempfile::tempdir().expect("create temp crate");
    let crate_dir = temp.path();
    let generated_dir = crate_dir.join("src/generated");

    let runtime_path = runtime_path_toml();

    write_manifest(crate_dir, &runtime_path, false, false);
    write_generated_files(&generated_dir, &files);
    let lib_contents = r##"pub mod generated;

#[cfg(test)]
mod tests {
    use super::generated::*;

    const CATEGORY: &str = ItemCategory::Electronics.as_str();

    #[test]
    fn known_enum_variants_deserialize() {
        let json = br#"{"id":"1","name":"Widget","category":"electronics","condition":"new","notes":"test"}"#.to_vec();
        let response = satay_runtime::ResponseParts {
            status: http::StatusCode::OK,
            headers: http::HeaderMap::new(),
            body: json,
        };
        let decoded = decode_get_item_response(response).expect("decoded response");
        match decoded {
            GetItemResponse::Ok(item) => {
                assert_eq!(item.id, "1");
                assert_eq!(item.name, "Widget");
                assert_eq!(CATEGORY, "electronics");
                assert_eq!(item.category, ItemCategory::Electronics);
                assert_eq!(item.condition, ItemCondition::New);
                assert_eq!(item.notes, Some("test".to_owned()));
            }
            other => panic!("unexpected response: {other:?}"),
        }
    }

    #[test]
    fn unknown_closed_enum_variant_is_rejected() {
        let json = br#"{"id":"2","name":"Gadget","category":"unknown_category","condition":"new","notes":null}"#.to_vec();
        let response = satay_runtime::ResponseParts {
            status: http::StatusCode::OK,
            headers: http::HeaderMap::new(),
            body: json,
        };
        assert!(decode_get_item_response(response).is_err());
    }
}
"##;
    fs::write(crate_dir.join("src/lib.rs"), lib_contents).expect("write lib");

    run_temp_cargo(crate_dir, "test", &[], "inline-enum generated crate tests");
}

#[test]
fn generated_open_string_enum_preserves_unknown_values() {
    let files = satay_codegen::generate(
        r#"
openapi: 3.1.0
info:
  title: Audio API
  version: 1.0.0
paths:
  /transcription:
    get:
      operationId: getTranscription
      responses:
        '200':
          description: Transcription
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AudioTranscription'
components:
  schemas:
    AudioTranscription:
      type: object
      properties:
        model:
          anyOf:
            - type: string
            - type: string
              enum:
                - whisper-1
                - gpt-4o-mini-transcribe
                - gpt-4o-transcribe
"#,
    )
    .expect("generate open string enum runtime fixture");

    let temp = tempfile::tempdir().expect("create temp crate");
    let crate_dir = temp.path();
    let generated_dir = crate_dir.join("src/generated");

    let runtime_path = runtime_path_toml();

    write_manifest(crate_dir, &runtime_path, false, false);
    write_generated_files(&generated_dir, &files);
    let lib_contents = r##"pub mod generated;

#[cfg(test)]
mod tests {
    use super::generated::*;

    #[test]
    fn known_open_enum_value_deserializes_to_known_variant() {
        let response = satay_runtime::ResponseParts {
            status: http::StatusCode::OK,
            headers: http::HeaderMap::new(),
            body: br#"{"model":"gpt-4o-transcribe"}"#.to_vec(),
        };

        let decoded = decode_get_transcription_response(response).expect("decoded response");
        match decoded {
            GetTranscriptionResponse::Ok(value) => {
                assert_eq!(
                    value.model,
                    Some(AudioTranscriptionModel::Gpt4oTranscribe)
                );
            }
            other => panic!("unexpected response: {other:?}"),
        }
    }

    #[test]
    fn unknown_open_enum_value_deserializes_to_other() {
        let response = satay_runtime::ResponseParts {
            status: http::StatusCode::OK,
            headers: http::HeaderMap::new(),
            body: br#"{"model":"gpt-custom-transcribe"}"#.to_vec(),
        };

        let decoded = decode_get_transcription_response(response).expect("decoded response");
        match decoded {
            GetTranscriptionResponse::Ok(value) => {
                assert_eq!(
                    value.model,
                    Some(AudioTranscriptionModel::Other(
                        "gpt-custom-transcribe".to_owned(),
                    ))
                );
            }
            other => panic!("unexpected response: {other:?}"),
        }
    }

    #[test]
    fn other_open_enum_value_serializes_as_string() {
        let value = AudioTranscriptionModel::Other("gpt-custom-transcribe".to_owned());
        assert_eq!(value.as_str(), "gpt-custom-transcribe");

        let encoded = serde_json::to_value(value).expect("serialized model");
        assert_eq!(encoded, serde_json::json!("gpt-custom-transcribe"));
    }
}
"##;
    fs::write(crate_dir.join("src/lib.rs"), lib_contents).expect("write lib");

    run_temp_cargo(
        crate_dir,
        "test",
        &[],
        "open string enum generated crate tests",
    );
}