Skip to content

Commit

Permalink
Merge pull request #16 from blazzy/x-client-default
Browse files Browse the repository at this point in the history
x-client-default workaround for unexpected streaming responses
  • Loading branch information
blazzy committed Aug 9, 2024
2 parents 312dcde + 0b5e35a commit 985896c
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 18 deletions.
34 changes: 28 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,37 @@ let images = client.images().image_list_libpod(None).await.unwrap();
## Swagger file modifications

The official swagger file generated by the podman project has a number of
issues and needs to be manually massaged.
issues and needs to be manually massaged. You can see the changes by comparing
[swagger/swagger-v5.1.0.yaml](swagger/swagger-v5.1.0.yaml) against
[swagger/swagger-v5.1.0.modified.yaml](swagger/swagger-v5.1.0.modified.yaml)

### Renamed fields

* `definitions/Mount/properties/Target` renamed to `Destination`

### Missing type info

* `definitions/ListContainer/properties/ExposedPorts` type set to `object`
* `definitions/InspectNetworkSettings/properties/Ports/additionalProperties` type set to `nullable`
* `definitions/InspectPodInfraConfig/properties/PortBindings/additionalProperties` type set to `nullable`
* `definitions/PodRmReport/properties/RemovedCtrs/additionalProperties` type set to `nullable`

More adjustments likely to come.
### Nullable fields

It turns out golang is a bit loosey goosey with nils. The following fields were
set to nullable:

* `definitions/InspectNetworkSettings/properties/Ports/additionalProperties`
* `definitions/InspectPodInfraConfig/properties/PortBindings/additionalProperties`
* `definitions/PodRmReport/properties/RemovedCtrs/additionalProperties`

### Client side defaults

Some requests return extra streaming data with their responses by default. Our
client doesn't support this, so we set up some client side overrides to set the
`quiet` parameters on these requests to true

* `responses//libpod/images/pull/post/parameters`
* `responses//libpod/images/scp/{name}/post/parameters`

More adjustments likely to come as we run into issues and should be documented here

## Changelog

Expand All @@ -99,7 +121,7 @@ More adjustments likely to come.

#### Breaking Changes

* Query and Header parameters are now provided through structs in `params` module
* Query and Header parameters are now provided through structs from the `params` module
* Body parameters are no longer optional.
* Some i32/u32 fields became i16/u16
* API functions no longer have the _api suffix
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use proc_macro2::TokenStream;
use quote::quote;

use crate::lang::rust::{model_type, parameter_to_str, struct_name, to_doc_comment, var_name};
use crate::lang::rust::{
model_type, or_default, parameter_to_str, struct_name, to_doc_comment, var_name,
};
use crate::{error::Error, spec::Spec, tag::Tag};

pub fn api(spec: &Spec, tag: &Tag) -> Result<String, Error> {
Expand Down Expand Up @@ -110,9 +112,10 @@ pub fn operations(spec: &Spec, tag: &Tag) -> Result<Vec<TokenStream>, Error> {
let to_string = parameter_to_str(&quote! { params.#var_name }, param);
quote! { query_pairs.append_pair(#name, #to_string); }
} else {
let or_default = or_default(&param.x_client_default);
let to_string = parameter_to_str(&quote! { #var_name }, param);
quote! {
if let Some(#var_name) = params.#var_name {
if let Some(#var_name) = params.#var_name #or_default {
query_pairs.append_pair(#name, #to_string);
}
}
Expand All @@ -136,8 +139,9 @@ pub fn operations(spec: &Spec, tag: &Tag) -> Result<Vec<TokenStream>, Error> {
if param.required {
quote! { req_builder = req_builder.header(#name, params.#var_name); }
} else {
let or_default = or_default(&param.x_client_default);
quote! {
if let Some(#var_name) = params.#var_name {
if let Some(#var_name) = params.#var_name #or_default {
req_builder = req_builder.header(#name, #var_name);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use proc_macro2::TokenStream;
use quote::quote;

use crate::{error::Error, spec::Spec};
Expand Down
13 changes: 13 additions & 0 deletions openapi-client-gen/src/lang/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::model::Model;
use crate::model::ModelData;
use crate::operation;
use crate::parameter;
use crate::parameter::XClientDefault;
use crate::parse;

/// Format name to a conventional upper camel rust ident for struct and trait names
Expand Down Expand Up @@ -167,3 +168,15 @@ pub fn model_type(model: &Model, models: &BTreeMap<String, Model>) -> Result<Tok
}
})
}

pub fn or_default(default: &Option<XClientDefault>) -> TokenStream {
if let Some(default) = default {
match default {
XClientDefault::String(string) => quote! { .or(Some(#string)) },
XClientDefault::Boolean(bool) => quote! { .or(Some(#bool)) },
XClientDefault::Integer(int) => quote! { .or(Some(#int)) },
}
} else {
TokenStream::new()
}
}
7 changes: 3 additions & 4 deletions openapi-client-gen/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,14 @@ mod tests {

#[test]
fn builds_base_path() {
let spec = Spec::from_yaml_string(
indoc! {r#"
let spec = Spec::from_yaml_string(indoc! {r#"
basePath: /
host: example.com
schemes:
- http
- https
"#}
).unwrap();
"#})
.unwrap();
assert_eq!(spec.base_path, "http://example.com/");
}
}
4 changes: 2 additions & 2 deletions podman-autogen-api/src/apis/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ pub trait Images: HasConfig + Send + Sync {
if let Some(reference) = params.reference {
query_pairs.append_pair("reference", reference);
}
if let Some(quiet) = params.quiet {
if let Some(quiet) = params.quiet.or(Some(true)) {
query_pairs.append_pair("quiet", &quiet.to_string());
}
if let Some(compat_mode) = params.compat_mode {
Expand Down Expand Up @@ -750,7 +750,7 @@ pub trait Images: HasConfig + Send + Sync {
if let Some(destination) = params.destination {
query_pairs.append_pair("destination", destination);
}
if let Some(quiet) = params.quiet {
if let Some(quiet) = params.quiet.or(Some(true)) {
query_pairs.append_pair("quiet", &quiet.to_string());
}
}
Expand Down
2 changes: 2 additions & 0 deletions swagger/swagger-v5.1.0.modified.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15077,6 +15077,7 @@ paths:
in: query
name: quiet
type: boolean
x-client-default: true
- default: false
description: Return the same JSON payload as the Docker-compat endpoint.
in: query
Expand Down Expand Up @@ -15182,6 +15183,7 @@ paths:
in: query
name: quiet
type: boolean
x-client-default: true
produces:
- application/json
responses:
Expand Down
1 change: 0 additions & 1 deletion tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ pub async fn pull_nginx_image(client: &PodmanRestClient) {
.images()
.image_pull_libpod(Some(params::ImagePullLibpod {
reference: Some("docker.io/library/nginx:latest"),
quiet: Some(true),
..Default::default()
}))
.await
Expand Down
1 change: 0 additions & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ async fn it_can_pull_images() {
.images()
.image_pull_libpod(Some(params::ImagePullLibpod {
reference: Some("docker.io/library/nginx:latest"),
quiet: Some(true),
..Default::default()
}))
.await
Expand Down

0 comments on commit 985896c

Please sign in to comment.