1
0
Fork 0
This commit is contained in:
Cyborus 2024-01-29 22:13:20 -05:00
parent 4ccdce0395
commit 2b1e6a8aae
No known key found for this signature in database
3 changed files with 31 additions and 27 deletions

View file

@ -106,7 +106,9 @@ fn method_docs(op: &Operation) -> eyre::Result<String> {
MaybeRef::Ref { _ref } => eyre::bail!("pipis"), MaybeRef::Ref { _ref } => eyre::bail!("pipis"),
}; };
match param._in { match param._in {
ParameterIn::Path { param: _ } | ParameterIn::Body { schema: _ } | ParameterIn::FormData { param: _ } => { ParameterIn::Path { param: _ }
| ParameterIn::Body { schema: _ }
| ParameterIn::FormData { param: _ } => {
write!(&mut out, "/// - `{}`", param.name)?; write!(&mut out, "/// - `{}`", param.name)?;
if let Some(description) = &param.description { if let Some(description) = &param.description {
write!(&mut out, ": {}", description)?; write!(&mut out, ": {}", description)?;
@ -153,7 +155,7 @@ fn fn_args_from_op(spec: &OpenApiV2, op: &Operation) -> eyre::Result<String> {
args.push_str(&type_name); args.push_str(&type_name);
} }
ParameterIn::Query { param: _ } => has_query = true, ParameterIn::Query { param: _ } => has_query = true,
ParameterIn::Header { param: _ }=> (), // has_headers = true, ParameterIn::Header { param: _ } => (), // has_headers = true,
ParameterIn::Body { schema } => { ParameterIn::Body { schema } => {
let ty = crate::schema_ref_type_name(spec, schema)?; let ty = crate::schema_ref_type_name(spec, schema)?;
args.push_str(", "); args.push_str(", ");
@ -178,7 +180,12 @@ fn fn_args_from_op(spec: &OpenApiV2, op: &Operation) -> eyre::Result<String> {
} }
pub fn param_type(param: &NonBodyParameter, owned: bool) -> eyre::Result<String> { pub fn param_type(param: &NonBodyParameter, owned: bool) -> eyre::Result<String> {
param_type_inner(&param._type, param.format.as_deref(), param.items.as_ref(), owned) param_type_inner(
&param._type,
param.format.as_deref(),
param.items.as_ref(),
owned,
)
} }
fn param_type_inner( fn param_type_inner(
@ -324,8 +331,8 @@ fn create_method_request(
let name = crate::sanitize_ident(&param.name); let name = crate::sanitize_ident(&param.name);
match &param._in { match &param._in {
ParameterIn::Path { param: _ } => (/* do nothing */), ParameterIn::Path { param: _ } => (/* do nothing */),
ParameterIn::Query { param: _ } => has_query = true, ParameterIn::Query { param: _ } => has_query = true,
ParameterIn::Header { param: _ } => (), // _has_headers = true, ParameterIn::Header { param: _ } => (), // _has_headers = true,
ParameterIn::Body { schema: _ } => { ParameterIn::Body { schema: _ } => {
if !body_method.is_empty() { if !body_method.is_empty() {
eyre::bail!("cannot have more than one body parameter"); eyre::bail!("cannot have more than one body parameter");
@ -336,7 +343,7 @@ fn create_method_request(
body_method = format!(".json(&{name})"); body_method = format!(".json(&{name})");
} }
} }
ParameterIn::FormData { param: _ } => { ParameterIn::FormData { param: _ } => {
if !body_method.is_empty() { if !body_method.is_empty() {
eyre::bail!("cannot have more than one body parameter"); eyre::bail!("cannot have more than one body parameter");
} }
@ -361,10 +368,11 @@ fn create_method_request(
fn param_is_string(spec: &OpenApiV2, param: &Parameter) -> eyre::Result<bool> { fn param_is_string(spec: &OpenApiV2, param: &Parameter) -> eyre::Result<bool> {
match &param._in { match &param._in {
ParameterIn::Body { schema } => { ParameterIn::Body { schema } => crate::schema_is_string(spec, schema),
crate::schema_is_string(spec, schema) ParameterIn::Path { param }
} | ParameterIn::Query { param }
ParameterIn::Path { param } | ParameterIn::Query { param } | ParameterIn::Header { param } | ParameterIn::FormData { param } => { | ParameterIn::Header { param }
| ParameterIn::FormData { param } => {
let is_str = match param._type { let is_str = match param._type {
ParameterType::String => true, ParameterType::String => true,
_ => false, _ => false,
@ -403,10 +411,7 @@ fn sanitize_path_arg(mut path: &str) -> eyre::Result<String> {
Ok(out) Ok(out)
} }
fn create_method_response( fn create_method_response(spec: &OpenApiV2, op: &Operation) -> eyre::Result<String> {
spec: &OpenApiV2,
op: &Operation,
) -> eyre::Result<String> {
let mut has_empty = false; let mut has_empty = false;
let mut only_empty = true; let mut only_empty = true;
for (code, res) in &op.responses.http_codes { for (code, res) in &op.responses.http_codes {

View file

@ -248,19 +248,18 @@ impl ParameterIn {
fn validate(&self) -> eyre::Result<()> { fn validate(&self) -> eyre::Result<()> {
match self { match self {
ParameterIn::Path { param } => { ParameterIn::Path { param } => {
eyre::ensure!( eyre::ensure!(param.required, "path parameters must be required");
param.required,
"path parameters must be required"
);
param.validate().wrap_err("path param") param.validate().wrap_err("path param")
}, }
ParameterIn::Query { param } => param.validate().wrap_err("query param"), ParameterIn::Query { param } => param.validate().wrap_err("query param"),
ParameterIn::Header { param } => param.validate().wrap_err("header param"), ParameterIn::Header { param } => param.validate().wrap_err("header param"),
ParameterIn::Body { schema } => if let MaybeRef::Value { value } = schema { ParameterIn::Body { schema } => {
if let MaybeRef::Value { value } = schema {
value.validate().wrap_err("body param") value.validate().wrap_err("body param")
} else { } else {
Ok(()) Ok(())
}, }
}
ParameterIn::FormData { param } => param.validate().wrap_err("form param"), ParameterIn::FormData { param } => param.validate().wrap_err("form param"),
} }
} }

View file

@ -97,9 +97,7 @@ fn create_struct_docs(schema: &Schema) -> eyre::Result<String> {
Ok(doc) Ok(doc)
} }
pub fn create_query_structs_for_path( pub fn create_query_structs_for_path(item: &PathItem) -> eyre::Result<String> {
item: &PathItem,
) -> eyre::Result<String> {
let mut s = String::new(); let mut s = String::new();
if let Some(op) = &item.get { if let Some(op) = &item.get {
s.push_str(&create_query_struct(op).wrap_err("GET")?); s.push_str(&create_query_struct(op).wrap_err("GET")?);
@ -149,7 +147,7 @@ fn create_query_struct(op: &Operation) -> eyre::Result<String> {
MaybeRef::Value { value } => value, MaybeRef::Value { value } => value,
MaybeRef::Ref { _ref } => eyre::bail!("todo: add deref parameters"), MaybeRef::Ref { _ref } => eyre::bail!("todo: add deref parameters"),
}; };
if let ParameterIn::Query { param: query_param} = &param._in { if let ParameterIn::Query { param: query_param } = &param._in {
let ty = crate::methods::param_type(query_param, true)?; let ty = crate::methods::param_type(query_param, true)?;
let field_name = crate::sanitize_ident(&param.name); let field_name = crate::sanitize_ident(&param.name);
fields.push_str("pub "); fields.push_str("pub ");
@ -199,7 +197,9 @@ fn create_query_struct(op: &Operation) -> eyre::Result<String> {
)?; )?;
} }
ParameterType::Array => { ParameterType::Array => {
let format = query_param.collection_format.unwrap_or(CollectionFormat::Csv); let format = query_param
.collection_format
.unwrap_or(CollectionFormat::Csv);
let item = query_param let item = query_param
.items .items
.as_ref() .as_ref()