Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "openapi-to-rust"
version = "0.2.1"
version = "0.3.0"
edition = "2024"
rust-version = "1.85.0"
authors = ["James Lal"]
Expand Down
36 changes: 27 additions & 9 deletions src/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,19 +344,37 @@ impl SchemaDetails {
}

/// Check if this is a string enum
///
/// A standalone string `const` (no `enum` array) is treated as a
/// degenerate single-value enum so the generator emits a tightly-typed
/// single-variant enum instead of a bare `String`. See issue #10.
pub fn is_string_enum(&self) -> bool {
self.enum_values.is_some()
self.enum_values.is_some() || self.const_string_value().is_some()
}

/// Get enum values as strings if this is a string enum
/// Get enum values as strings if this is a string enum.
///
/// Falls back to `[const_value]` when `enum` is absent but `const` is a
/// string, so a property like `{ "type": "string", "const": "X" }`
/// produces a single-variant enum.
pub fn string_enum_values(&self) -> Option<Vec<String>> {
self.enum_values.as_ref().map(|values| {
values
.iter()
.filter_map(|v| v.as_str())
.map(|s| s.to_string())
.collect()
})
if let Some(values) = self.enum_values.as_ref() {
return Some(
values
.iter()
.filter_map(|v| v.as_str())
.map(|s| s.to_string())
.collect(),
);
}
self.const_string_value().map(|s| vec![s])
}

fn const_string_value(&self) -> Option<String> {
self.const_value
.as_ref()
.and_then(|v| v.as_str())
.map(|s| s.to_string())
}

/// Check if a field is required
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,19 @@ pub enum ToolsRequestToolsItemUnion {
pub struct TextTool {
pub name: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Default)]
pub enum TextToolType {
#[default]
#[serde(rename = "text")]
Text,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct CodeTool {
pub language: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Default)]
pub enum CodeToolType {
#[default]
#[serde(rename = "code")]
Code,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
source: src/test_helpers.rs
expression: "&generated_code"
---
//! Generated types from OpenAPI specification
//!
//! This file contains all the generated types for the API.
//! Do not edit manually - regenerate using the appropriate script.
#![allow(clippy::large_enum_variant)]
#![allow(clippy::format_in_format_args)]
#![allow(clippy::let_unit_value)]
#![allow(unreachable_patterns)]
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ConstModifier {
#[serde(rename = "someConstant", skip_serializing_if = "Option::is_none")]
pub some_constant: Option<ConstModifierSomeConstant>,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Default)]
pub enum ConstModifierSomeConstant {
#[default]
#[serde(rename = "TheOnlyValidValue")]
TheOnlyValidValue,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: src/test_helpers.rs
expression: "&generated_code"
---
//! Generated types from OpenAPI specification
//!
//! This file contains all the generated types for the API.
//! Do not edit manually - regenerate using the appropriate script.
#![allow(clippy::large_enum_variant)]
#![allow(clippy::format_in_format_args)]
#![allow(clippy::let_unit_value)]
#![allow(unreachable_patterns)]
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RequiredConst {
pub kind: RequiredConstKind,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Default)]
pub enum RequiredConstKind {
#[default]
#[serde(rename = "fixed")]
Fixed,
}
8 changes: 7 additions & 1 deletion src/snapshots/openapi_to_rust__test_helpers__debug_test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@ expression: "&generated_code"
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct TypedEvent {
pub r#type: String,
pub r#type: TypedEventType,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Default)]
pub enum TypedEventType {
#[default]
#[serde(rename = "event.created")]
EventCreated,
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,24 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ImageBlock {
pub source: ImageBlockSource,
pub r#type: String,
pub r#type: ImageBlockType,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct URLImageSource {
pub url: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Default)]
pub enum URLImageSourceType {
#[default]
#[serde(rename = "url")]
Url,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Default)]
pub enum ImageBlockType {
#[default]
#[serde(rename = "image")]
Image,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(tag = "type")]
pub enum ImageBlockSource {
Expand All @@ -32,3 +44,9 @@ pub enum ImageBlockSource {
pub struct Base64ImageSource {
pub data: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Default)]
pub enum Base64ImageSourceType {
#[default]
#[serde(rename = "base64")]
Base64,
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@ expression: "&generated_code"
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct TypedEvent {
pub r#type: String,
pub r#type: TypedEventType,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Default)]
pub enum TypedEventType {
#[default]
#[serde(rename = "event.created")]
EventCreated,
}
76 changes: 76 additions & 0 deletions tests/const_only_property_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//! Regression test for https://github.com/gpu-cli/openapi-to-rust/issues/10
//!
//! A property with a string `const` and no `enum` array should generate a
//! single-variant enum, not a bare `Option<String>`.

#[cfg(test)]
mod tests {
use openapi_to_rust::test_helpers::*;
use serde_json::json;

#[test]
fn const_only_string_property_generates_single_variant_enum() {
let spec = minimal_spec(json!({
"ConstModifier": {
"type": "object",
"properties": {
"someConstant": {
"type": "string",
"const": "TheOnlyValidValue"
}
}
}
}));

let result = test_generation("const_only_property", spec).expect("Generation failed");

assert!(
result.contains("pub enum ConstModifierSomeConstant"),
"expected ConstModifierSomeConstant enum to be generated; got:\n{result}"
);
assert!(
result.contains("#[serde(rename = \"TheOnlyValidValue\")]"),
"expected serde rename for the const value; got:\n{result}"
);
assert!(
result.contains("TheOnlyValidValue,"),
"expected TheOnlyValidValue variant; got:\n{result}"
);

assert!(
result.contains("pub some_constant: Option<ConstModifierSomeConstant>"),
"field should reference the generated enum, not String; got:\n{result}"
);
assert!(
!result.contains("pub some_constant: Option<String>"),
"field should NOT be Option<String>; got:\n{result}"
);
}

#[test]
fn required_const_only_property_is_not_optional() {
let spec = minimal_spec(json!({
"RequiredConst": {
"type": "object",
"properties": {
"kind": {
"type": "string",
"const": "fixed"
}
},
"required": ["kind"]
}
}));

let result = test_generation("const_only_required", spec).expect("Generation failed");

assert!(
result.contains("pub enum RequiredConstKind"),
"expected RequiredConstKind enum; got:\n{result}"
);
assert!(
result.contains("pub kind: RequiredConstKind"),
"required const field should be non-optional; got:\n{result}"
);
}
}
Loading