Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
Signed-off-by: Jess Frazelle <github@jessfraz.com>
  • Loading branch information
jessfraz committed Aug 12, 2024
1 parent 0f4fc14 commit 2ba0c64
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
36 changes: 34 additions & 2 deletions openapitor/src/types/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ pub fn generate_example_rust_from_schema(
quote!(chrono::Utc::now())
}
openapiv3::VariantOrUnknownOrEmpty::Item(openapiv3::StringFormat::Date) => {
quote!(chrono::Utc::now().date().naive_utc())
quote!(chrono::Utc::now().date_naive())
}
openapiv3::VariantOrUnknownOrEmpty::Item(openapiv3::StringFormat::Password) => {
quote!("some-password".to_string())
Expand Down Expand Up @@ -379,7 +379,7 @@ pub fn generate_example_rust_from_schema(
quote!(chrono::Utc::now().time())
}
"date" => {
quote!(chrono::Utc::now().date().naive_utc())
quote!(chrono::Utc::now().date_naive())
}
"date-time" => quote!(chrono::Utc::now()),
"partial-date-time" => {
Expand Down Expand Up @@ -943,6 +943,38 @@ pub fn generate_example_rust_from_schema(
},
in_crate,
);
} else if !any.one_of.is_empty() {
let one_of = openapiv3::Schema {
schema_data: schema.schema_data.clone(),
schema_kind: openapiv3::SchemaKind::OneOf {
one_of: any.one_of.clone(),
},
};
return generate_example_rust_from_schema(type_space, name, &one_of, in_crate);
} else if !any.all_of.is_empty() {
let all_of = openapiv3::Schema {
schema_data: schema.schema_data.clone(),
schema_kind: openapiv3::SchemaKind::AllOf {
all_of: any.all_of.clone(),
},
};
return generate_example_rust_from_schema(type_space, name, &all_of, in_crate);
} else if let Some(typ) = &any.typ {
let string_schema = openapiv3::Schema {
schema_data: schema.schema_data.clone(),
schema_kind: openapiv3::SchemaKind::Type(openapiv3::Type::String(
openapiv3::StringType {
format: openapiv3::VariantOrUnknownOrEmpty::Unknown(typ.to_string()),
..Default::default()
},
)),
};
return generate_example_rust_from_schema(
type_space,
name,
&string_schema,
in_crate,
);
}

quote!(serde_json::Value::String("some-string".to_string()))
Expand Down
22 changes: 21 additions & 1 deletion openapitor/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,15 @@ impl TypeSpace {
return self.render_one_of(name, &any.one_of, data);
} else if !any.all_of.is_empty() {
return self.render_all_of(name, &any.all_of, data);
} else if let Some(typ) = &any.typ {
return self.render_string_type(
name,
&openapiv3::StringType {
format: openapiv3::VariantOrUnknownOrEmpty::Unknown(typ.to_string()),
..Default::default()
},
data,
);
}

// This is a serde_json::Value.
Expand Down Expand Up @@ -1511,6 +1520,17 @@ pub fn get_type_name_for_schema(
},
};
return get_type_name_for_schema(name, &all_of, spec, in_crate);
} else if let Some(typ) = &any.typ {
let string_schema = openapiv3::Schema {
schema_data: schema.schema_data.clone(),
schema_kind: openapiv3::SchemaKind::Type(openapiv3::Type::String(
openapiv3::StringType {
format: openapiv3::VariantOrUnknownOrEmpty::Unknown(typ.to_string()),
..Default::default()
},
)),
};
return get_type_name_for_schema(name, &string_schema, spec, in_crate);
}
log::warn!("got any schema kind `{}`: {:?}", name, any);
quote!(serde_json::Value)
Expand Down Expand Up @@ -1593,7 +1613,7 @@ fn get_type_name_for_string(
"money-usd" => quote!(bigdecimal::BigDecimal),
"id" => quote!(String),
f => {
anyhow::bail!("XXX unknown string format {}", f)
anyhow::bail!("XXX unknown string format {} for {}", f, name)
}
},
};
Expand Down

0 comments on commit 2ba0c64

Please sign in to comment.