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

How to define common parameters in OpenAPI spec? #321

Closed
banool opened this issue Jul 18, 2022 · 5 comments
Closed

How to define common parameters in OpenAPI spec? #321

banool opened this issue Jul 18, 2022 · 5 comments
Labels
question Further information is requested Stale

Comments

@banool
Copy link
Contributor

banool commented Jul 18, 2022

Let's say I have code like this:

#[OpenApi]
    #[oai(
        path = "/accounts/:address",
        method = "get",
        operation_id = "get_account",
        tag = "ApiTags::General"
    )]
    async fn get_account(
        &self,
        accept: Accept,
        address: Path<AddressWrapper>,
    ) -> AptosResult<AccountData> {
        let accept_type = AcceptType::try_from(&accept)?;
        let address = address.0.try_into()?;
        let account = Account::new(self.context.clone(), address)?;
        account.account(&accept_type)
    }
}

#[derive(Debug, Clone, Eq, Hash, PartialEq, NewType)]
pub struct AddressWrapper(pub String);

impl TryInto<Address> for AddressWrapper {
    type Error = AptosErrorResponse;

    fn try_into(self) -> Result<Address, Self::Error> {
        parse_param::<Address>(self.0)
    }
}

fn parse_param<T: FromStr>(value: String) -> Result<T, AptosErrorResponse>
where
    T::Err: Display,
{
    T::from_str(&value).map_err(|e| {
        AptosErrorResponse::BadRequest(Json(AptosError::new(
            anyhow::format_err!("Given input was in an invalid format: {}: {}", value, e)
                .to_string(),
        )))
    })
}

The relevant part of the spec that this generates is:

paths:
  "/accounts/{address}":
    get:
      tags:
        - General
      summary: get_account
      description: Return high level information about an account such as its sequence number.
      parameters:
        - name: address
          schema:
            type: string
            description: "Hex-encoded 32 byte Aptos account address. This parameter is case insensitive and it does not matter if you prefix it with `0x` or not. See [here](https://aptos.dev/concepts/basics-accounts) for more details."
          in: path
          required: true
          deprecated: false

As you can see, the resulting spec has the parameter right there. Imagine I had 10 endpoints that took in this address parameter, there would be a lot of repetition. To address this, OpenAPI specs can have a section like this:

components:
  parameters:
    Address:
          schema:
            type: string
            description: "Hex-encoded 32 byte Aptos account address. This parameter is case insensitive and it does not matter if you prefix it with `0x` or not. See [here](https://aptos.dev/concepts/basics-accounts) for more details."
          in: path
          required: true
          deprecated: false

Is it possible to express this with Poem? I've been searching the code but can't find anything that generates this parameters section.

@banool banool added the question Further information is requested label Jul 18, 2022
@sunli829
Copy link
Collaborator

This is not supported yet, but I can add it if you need it.

@banool
Copy link
Contributor Author

banool commented Jul 19, 2022

It's not critical but it would be a nice win, my spec is currently getting very long due to the repeating the same params with the same long descriptions every time.

@sunli829
Copy link
Collaborator

You are right, this is a low priority job.

@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label Aug 18, 2022
@github-actions
Copy link

This issue was closed because it has been stalled for 5 days with no activity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested Stale
Projects
None yet
Development

No branches or pull requests

2 participants