Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(openapi): add support for server variables #962

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions poem-openapi/src/openapi.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
collections::{HashMap, HashSet},
collections::{BTreeMap, HashMap, HashSet},
marker::PhantomData,
};

Expand All @@ -14,7 +14,7 @@ use crate::{
base::UrlQuery,
registry::{
Document, MetaContact, MetaExternalDocument, MetaHeader, MetaInfo, MetaLicense,
MetaOperationParam, MetaParamIn, MetaSchemaRef, MetaServer, Registry,
MetaOperationParam, MetaParamIn, MetaSchemaRef, MetaServer, MetaServerVariable, Registry,
},
types::Type,
OpenApi, Webhook,
Expand All @@ -25,6 +25,7 @@ use crate::{
pub struct ServerObject {
url: String,
description: Option<String>,
variables: BTreeMap<String, MetaServerVariable>,
}

impl<T: Into<String>> From<T> for ServerObject {
Expand All @@ -39,6 +40,7 @@ impl ServerObject {
Self {
url: url.into(),
description: None,
variables: BTreeMap::new(),
}
}

Expand All @@ -50,6 +52,49 @@ impl ServerObject {
..self
}
}

/// Adds a server variable with a limited set of values.
///
/// The variable name must be present in the server URL in curly braces.
#[must_use]
pub fn enum_variable(
mut self,
name: impl Into<String>,
description: impl Into<String>,
default: impl Into<String>,
enum_values: Vec<impl Into<String>>,
) -> Self {
self.variables.insert(
name.into(),
MetaServerVariable {
description: description.into(),
default: default.into(),
enum_values: enum_values.into_iter().map(Into::into).collect(),
},
);
self
}

/// Adds a server variable that can take any value.
///
/// The variable name must be present in the server URL in curly braces.
#[must_use]
pub fn variable(
mut self,
name: impl Into<String>,
description: impl Into<String>,
default: impl Into<String>,
) -> Self {
self.variables.insert(
name.into(),
MetaServerVariable {
description: description.into(),
default: default.into(),
enum_values: Vec::new(),
},
);
self
}
}

/// A contact information for the exposed API.
Expand Down Expand Up @@ -298,6 +343,7 @@ impl<T, W> OpenApiService<T, W> {
self.servers.push(MetaServer {
url: server.url,
description: server.description,
variables: server.variables,
});
self
}
Expand Down
14 changes: 14 additions & 0 deletions poem-openapi/src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,20 @@ pub struct MetaServer {
pub url: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,

#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub variables: BTreeMap<String, MetaServerVariable>,
}

#[derive(Debug, Eq, PartialEq, Serialize, Clone)]
pub struct MetaServerVariable {
pub default: String,

#[serde(skip_serializing_if = "String::is_empty")]
pub description: String,

#[serde(rename = "enum", skip_serializing_if = "Vec::is_empty")]
pub enum_values: Vec<String>,
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
Expand Down
Loading