Skip to content

Commit 267fff7

Browse files
committed
Add sample per PR request
1 parent 99cb8d1 commit 267fff7

15 files changed

Lines changed: 1388 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
generatorName: rust-axum
2+
outputDir: samples/server/petstore/rust-axum/output/rust-axum-array-params-test
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/rust/rust-axum-array-params-test.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/rust-axum
5+
generateAliasAsModel: true
6+
additionalProperties:
7+
hideGenerationTimestamp: "true"
8+
packageName: rust-axum-array-params-test
9+
globalProperties:
10+
skipFormModel: false
11+
enablePostProcessFile: true
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
openapi: 3.0.1
2+
info:
3+
title: Test to check that array query params are rendered correctly.
4+
version: 0.0.1
5+
paths:
6+
/endpoint:
7+
get:
8+
description: Some endpoint.
9+
parameters:
10+
- name: numbers
11+
in: query
12+
description: Some numbers.
13+
schema:
14+
type: array
15+
items:
16+
type: number
17+
- name: multiplier
18+
in: query
19+
description: Multipler for sum.
20+
schema:
21+
type: number
22+
responses:
23+
'200':
24+
description: OK.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target
2+
Cargo.lock
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.gitignore
2+
.openapi-generator-ignore
3+
Cargo.toml
4+
README.md
5+
src/apis/default.rs
6+
src/apis/mod.rs
7+
src/header.rs
8+
src/lib.rs
9+
src/models.rs
10+
src/server/mod.rs
11+
src/types.rs
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.13.0-SNAPSHOT
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
[package]
2+
name = "rust-axum-array-params-test"
3+
version = "0.0.1"
4+
authors = ["OpenAPI Generator team and contributors"]
5+
description = "No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)"
6+
edition = "2021"
7+
8+
[features]
9+
default = ["server"]
10+
server = []
11+
conversion = [
12+
"frunk",
13+
"frunk_derives",
14+
"frunk_core",
15+
"frunk-enum-core",
16+
"frunk-enum-derive",
17+
]
18+
19+
[dependencies]
20+
async-trait = "0.1"
21+
axum = { version = "0.8", features = ["multipart"] }
22+
axum-extra = { version = "0.10", features = ["cookie", "query"] }
23+
base64 = "0.22"
24+
bytes = "1"
25+
chrono = { version = "0.4", features = ["serde"] }
26+
frunk = { version = "0.4", optional = true }
27+
frunk-enum-core = { version = "0.3", optional = true }
28+
frunk-enum-derive = { version = "0.3", optional = true }
29+
frunk_core = { version = "0.4", optional = true }
30+
frunk_derives = { version = "0.4", optional = true }
31+
http = "1"
32+
lazy_static = "1"
33+
regex = "1"
34+
serde = { version = "1", features = ["derive"] }
35+
serde_html_form = "0.2"
36+
serde_json = { version = "1", features = ["raw_value"] }
37+
tokio = { version = "1", default-features = false, features = [
38+
"signal",
39+
"rt-multi-thread",
40+
] }
41+
tracing = { version = "0.1", features = ["attributes"] }
42+
uuid = { version = "1", features = ["serde"] }
43+
validator = { version = "0.20", features = ["derive"] }
44+
45+
[dev-dependencies]
46+
tracing-subscriber = "0.3"
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Rust API for rust-axum-array-params-test
2+
3+
No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
4+
5+
## Overview
6+
7+
This server was generated by the [openapi-generator]
8+
(https://openapi-generator.tech) project. By using the
9+
[OpenAPI-Spec](https://github.com/OAI/OpenAPI-Specification) from a remote
10+
server, you can easily generate a server stub.
11+
12+
To see how to make this your own, look here: [README]((https://openapi-generator.tech))
13+
14+
- API version: 0.0.1
15+
- Generator version: 7.13.0-SNAPSHOT
16+
17+
18+
19+
This autogenerated project defines an API crate `rust-axum-array-params-test` which contains:
20+
* An `Api` trait defining the API in Rust.
21+
* Data types representing the underlying data model.
22+
* Axum router which accepts HTTP requests and invokes the appropriate `Api` method for each operation.
23+
* Request validations (path, query, body params) are included.
24+
25+
## Using the generated library
26+
27+
The generated library has a few optional features that can be activated through Cargo.
28+
29+
* `server`
30+
* This defaults to enabled and creates the basic skeleton of a server implementation based on Axum.
31+
* To create the server stack you'll need to provide an implementation of the API trait to provide the server function.
32+
* `conversions`
33+
* This defaults to disabled and creates extra derives on models to allow "transmogrification" between objects of structurally similar types.
34+
35+
See https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section for how to use features in your `Cargo.toml`.
36+
37+
### Example
38+
39+
```rust
40+
struct ServerImpl {
41+
// database: sea_orm::DbConn,
42+
}
43+
44+
#[allow(unused_variables)]
45+
#[async_trait]
46+
impl rust_axum_array_params_test::apis::default::Api for ServerImpl {
47+
// API implementation goes here
48+
}
49+
50+
impl rust_axum_array_params_test::apis::ErrorHandler for ServerImpl {}
51+
52+
pub async fn start_server(addr: &str) {
53+
// initialize tracing
54+
tracing_subscriber::fmt::init();
55+
56+
// Init Axum router
57+
let app = rust_axum_array_params_test::server::new(Arc::new(ServerImpl));
58+
59+
// Add layers to the router
60+
let app = app.layer(...);
61+
62+
// Run the server with graceful shutdown
63+
let listener = TcpListener::bind(addr).await.unwrap();
64+
axum::serve(listener, app)
65+
.with_graceful_shutdown(shutdown_signal())
66+
.await
67+
.unwrap();
68+
}
69+
70+
async fn shutdown_signal() {
71+
let ctrl_c = async {
72+
signal::ctrl_c()
73+
.await
74+
.expect("failed to install Ctrl+C handler");
75+
};
76+
77+
#[cfg(unix)]
78+
let terminate = async {
79+
signal::unix::signal(signal::unix::SignalKind::terminate())
80+
.expect("failed to install signal handler")
81+
.recv()
82+
.await;
83+
};
84+
85+
#[cfg(not(unix))]
86+
let terminate = std::future::pending::<()>();
87+
88+
tokio::select! {
89+
_ = ctrl_c => {},
90+
_ = terminate => {},
91+
}
92+
}
93+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use async_trait::async_trait;
2+
use axum::extract::*;
3+
use axum_extra::extract::{CookieJar, Host};
4+
use bytes::Bytes;
5+
use http::Method;
6+
use serde::{Deserialize, Serialize};
7+
8+
use crate::{models, types::*};
9+
10+
#[derive(Debug, PartialEq, Serialize, Deserialize)]
11+
#[must_use]
12+
#[allow(clippy::large_enum_variant)]
13+
pub enum EndpointGetResponse {
14+
/// OK.
15+
Status200_OK,
16+
}
17+
18+
/// Default
19+
#[async_trait]
20+
#[allow(clippy::ptr_arg)]
21+
pub trait Default<E: std::fmt::Debug + Send + Sync + 'static = ()>: super::ErrorHandler<E> {
22+
/// EndpointGet - GET /endpoint
23+
async fn endpoint_get(
24+
&self,
25+
method: &Method,
26+
host: &Host,
27+
cookies: &CookieJar,
28+
query_params: &models::EndpointGetQueryParams,
29+
) -> Result<EndpointGetResponse, E>;
30+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
pub mod default;
2+
3+
// Error handler for unhandled errors.
4+
#[async_trait::async_trait]
5+
pub trait ErrorHandler<E: std::fmt::Debug + Send + Sync + 'static = ()> {
6+
#[allow(unused_variables)]
7+
#[tracing::instrument(skip_all)]
8+
async fn handle_error(
9+
&self,
10+
method: &::http::Method,
11+
host: &axum_extra::extract::Host,
12+
cookies: &axum_extra::extract::CookieJar,
13+
error: E,
14+
) -> Result<axum::response::Response, http::StatusCode> {
15+
tracing::error!("Unhandled error: {:?}", error);
16+
axum::response::Response::builder()
17+
.status(http::StatusCode::INTERNAL_SERVER_ERROR)
18+
.body(axum::body::Body::empty())
19+
.map_err(|_| http::StatusCode::INTERNAL_SERVER_ERROR)
20+
}
21+
}

0 commit comments

Comments
 (0)