Skip to content

Commit

Permalink
feat: Return resource id on successful creation from Location header (
Browse files Browse the repository at this point in the history
#119)

* return resource id on creation from location header

* convert String to TypeString

* get_id_from_location_header now returns TypeString and doesn't use unwrap()

* updated openapi.rs to generate resource id extractor

* updated generated code using openapi.rs

* review: less transformations, shorter names, comment, gen update, tests for features added

---------

Co-authored-by: Alexander Korolev <[email protected]>
  • Loading branch information
Star3Lord and kilork authored Apr 30, 2024
1 parent 8ed6fa7 commit 4067d78
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 164 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ jobs:
run: cargo build
- name: Run unit tests
run: cargo test
- name: Build with features
run: cargo build --features=rc,schemars
- name: Run unit tests with features
run: cargo test --features=rc,schemars
- name: Run integration tests
run: |
export KEYCLOAK_VERSION=`cargo metadata --no-deps --format-version 1 | jq '.packages[0].version | split(".") | map(tonumber) | .[:-1] + [.[2] / 100] | map(floor) | join(".")' | tr -d '"'`
docker run -p 8080:8080 --name keycloak -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=password -e KC_FEATURES=admin-api quay.io/keycloak/keycloak:${KEYCLOAK_VERSION} start-dev &
sleep 40
cargo run --example=adduser
cargo run --example=adduser --features=rc,schemars
31 changes: 23 additions & 8 deletions examples/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,23 @@ mod openapi {

method_name = (method_name + &method_string).to_snake_case();

let mut result_type = self.responses.to_rust_return_type_and_parse_calls();

let mut result_type_value = result_type
.as_ref()
.map(|rt| rt.value.as_ref())
.unwrap_or("()");

// post method with empty body may return id extracted from location header
let to_id = if matches!(method, Method::Post) && result_type_value == "()" {
result_type_value = "Option<TypeString>";
true
} else {
false
};

let (method_string_lc, comments) =
self.comments(&parameters, method_string, path, &path_snake_case);
self.comments(&parameters, method_string, path, &path_snake_case, to_id);

let mut output = vec![];

Expand Down Expand Up @@ -362,13 +377,6 @@ mod openapi {

output.extend(parameters_of_method);

let mut result_type = self.responses.to_rust_return_type_and_parse_calls();

let mut result_type_value = result_type
.as_ref()
.map(|rt| rt.value.as_ref())
.unwrap_or("()");

let desc = Toml::desc::<_, _, String>(path, &method_string_lc, None);
if let Some(desc) = desc.as_ref() {
let from_type = desc.from_type.as_str();
Expand Down Expand Up @@ -458,6 +466,9 @@ mod openapi {
" Ok(error_check(response).await?.{body}().await{}?)",
convert.as_deref().unwrap_or_default()
));
} else if to_id {
output.push(" let response = builder.send().await?;".into());
output.push(" error_check(response).await.map(to_id)".into());
} else {
output.push(" let response = builder.send().await?;".into());
output.push(" error_check(response).await?;".into());
Expand All @@ -480,6 +491,7 @@ mod openapi {
method_string: String,
path: &str,
path_snake_case: &String,
to_id: bool,
) -> (String, Vec<String>) {
let mut comments: Vec<Vec<Cow<str>>> = vec![];

Expand Down Expand Up @@ -515,6 +527,9 @@ mod openapi {
.collect(),
);
}
if to_id {
comments.push(vec!["Returns id of created resource".into()]);
}
if let [tag] = self.tags.as_deref().unwrap_or_else(|| &[]) {
comments.push(vec![format!("Resource: `{tag}`").into()]);
}
Expand Down
Loading

0 comments on commit 4067d78

Please sign in to comment.