aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xrust/codegen_openapi2.sh76
1 files changed, 23 insertions, 53 deletions
diff --git a/rust/codegen_openapi2.sh b/rust/codegen_openapi2.sh
index db338dcb..f93322ff 100755
--- a/rust/codegen_openapi2.sh
+++ b/rust/codegen_openapi2.sh
@@ -5,67 +5,37 @@ set -o pipefail
echo "Running openapi-generator..."
OUTPUT=`pwd`/fatcat-openapi
+mkdir -p $OUTPUT
cat ../fatcat-openapi2.yml | grep -v "TAGLINE$" | perl -0777 -pe "s/<\!-- STARTLONGDESCRIPTION -->.*<\!-- ENDLONGDESCRIPTION -->//s" > $OUTPUT/api.yaml
-docker run \
- -v $OUTPUT:/tmp/swagger/ \
- openapitools/openapi-generator-cli:v4.1.2 \
+
+export OPENAPI_GENERATOR_VERSION=5.0.0-SNAPSHOT
+./openapi-generator-cli.sh \
generate \
--generator-name rust-server \
- --input-spec /tmp/swagger/api.yaml \
- --output /tmp/swagger/ \
- --package-name=fatcat-openapi
-
-sudo chown `whoami`:`whoami` -R fatcat-openapi
-#git checkout fatcat-openapi/Cargo.toml
-
-echo "Patching..."
-
-# Hacks to fix crate naming
-sed -i 's/fatcat::/fatcat_openapi::/g' fatcat-openapi/examples/client.rs fatcat-openapi/examples/server.rs fatcat-openapi/examples/server_lib/server.rs
-sed -i 's/extern crate fatcat;/extern crate fatcat_openapi;/g' fatcat-openapi/examples/client.rs fatcat-openapi/examples/server.rs fatcat-openapi/examples/server_lib/server.rs
-
-# Squash many warnings ("error: unused doc comment")
-sed -i 's/\/\/\/ Create Mime objects/\/\/ Create Mime objects/g' fatcat-openapi/src/mimetypes.rs
-
-# 2019 edition crate imports
-sed -i 's/use models;/use crate::models;/g' fatcat-openapi/src/client/*.rs fatcat-openapi/src/server/*.rs fatcat-openapi/src/models.rs
-sed -i 's/use mimetypes;/use crate::mimetypes;/g' fatcat-openapi/src/client/*.rs fatcat-openapi/src/server/*.rs
-sed -i 's/use {/use crate::{/g' fatcat-openapi/src/client/*.rs fatcat-openapi/src/server/*.rs
-
-# weird broken auth example
-sed -i 's/chain.link_before(AllowAllMiddleware/\/\/chain.link_before(AllowAllMiddleware/g' fatcat-openapi/examples/server.rs
+ --input-spec $OUTPUT/api.yaml \
+ --output $OUTPUT \
+ --package-name=fatcat-openapi \
+ --generate-alias-as-model
-# Hack to handle "extra" JSON fields
-sed -i 's/, object/, serde_json::Value/g' fatcat-openapi/src/models.rs
-sed -i 's/extern crate uuid;/extern crate serde_json;\nextern crate uuid;/g' fatcat-openapi/src/models.rs
+cd fatcat-openapi
-# Hack to fix "release_date" as Date, not DateTime
-sed -i 's/release_date: Option<chrono::DateTime<chrono::Utc>>/release_date: Option<chrono::NaiveDate>/g' fatcat-openapi/src/models.rs
-sed -i 's/withdrawn_date: Option<chrono::DateTime<chrono::Utc>>/withdrawn_date: Option<chrono::NaiveDate>/g' fatcat-openapi/src/models.rs
+echo "Running cargo-fmt (first time)..."
+cargo fmt
-# Hack to optionally log unknown request fields (not actually needed)
-#sed -i 's/\(response.headers.set(Warning(format!("Ignoring unknown fields in body: {:?}", unused_elements)));\)/\1 warn!("unknown fields in request body: {:?}", unused_elements);/g' fatcat-openapi/src/server.rs
+echo "Patching..."
-# Hack to require that optional params parse correctly (boolean, integer, datetime)
-# If we reformat, this this should basically go from, eg:
-# .and_then(|x| x.parse::<i64>()
-# .ok());
-# To:
-# .and_then(|x| Some(x.parse::<i64>()))
-# .map_or_else(|| Ok(None), |x| x.map(|v| Some(v)))
-# .map_err(|x| Response::with((status::InternalServerError, "unparsable query parameter (expected integer)".to_string())))?;
-sed -i 's/.and_then(|x| x.parse::<i64>().ok());$/.and_then(|x| Some(x.parse::<i64>())).map_or_else(|| Ok(None), |x| x.map(|v| Some(v))).map_err(|x| Response::with((status::BadRequest, "unparsable query parameter (expected integer)".to_string())))?;/g' fatcat-openapi/src/server/*.rs
-sed -i 's/.and_then(|x| x.parse::<bool>().ok());$/.and_then(|x| Some(x.to_lowercase().parse::<bool>())).map_or_else(|| Ok(None), |x| x.map(|v| Some(v))).map_err(|x| Response::with((status::BadRequest, "unparsable query parameter (expected boolean)".to_string())))?;/g' fatcat-openapi/src/server/*.rs
-sed -i 's/.and_then(|x| x.parse::<chrono::DateTime<chrono::Utc>>().ok());$/.and_then(|x| Some(x.parse::<chrono::DateTime<chrono::Utc>>())).map_or_else(|| Ok(None), |x| x.map(|v| Some(v))).map_err(|x| Response::with((status::BadRequest, "unparsable query parameter (expected UTC datetime in ISO\/RFC format)".to_string())))?;/g' fatcat-openapi/src/server/*.rs
+# Hack to fix "release_date" (and "withdrawn_date") as Date, not DateTime
+sed -i 's/_date: Option<chrono::DateTime<chrono::Utc>>/_date: Option<chrono::NaiveDate>/g' src/models.rs
+sed -i 's/_date: Vec<chrono::DateTime<chrono::Utc>>/_date: Vec<chrono::NaiveDate>/g' src/models.rs
+perl -0777 -pi -e 's/_date\.push\(\n\s+chrono::DateTime::<chrono::Utc>::from_str/_date\.push\(chrono::NaiveDate::from_str/gs' src/models.rs
# unnecessary duplicate copies of API spec
-rm fatcat-openapi/api.yaml
-rm -rf fatcat-openapi/api/
+rm api.yaml
+rm -rf api/
-cd fatcat-openapi
-
-echo "Running cargo-fix (slow)..."
-cargo fix --allow-dirty
-
-echo "Running cargo-fmt..."
+echo "Running cargo-fmt (final time)..."
cargo fmt
+
+cp Cargo.toml Cargo.toml.new
+git checkout Cargo.toml
+echo "Make sure to review/merge fatcat-openapi/Cargo.toml by hand (compare with fatcat-openapi/Cargo.toml.new)"