airtasker / spot

Spot is a concise, developer-friendly way to describe your API contract.
Other
555 stars 38 forks source link

Conditional Types for Headers and/or Parameters #1033

Open mattdillon100 opened 3 years ago

mattdillon100 commented 3 years ago

We will be creating our API spec, hopefully created by spot. However (and this is an example), we have headers that can either accept a cookie or authentication information. This is legacy code and not ideal but, regardless, would need to be covered in the specification.

If this is not possible, what workarounds exist. Here is an example of what we need:

@request
    request(
        @headers
            headers: {
            "Accept": "*/*"
            "Host": String,
            {
                "Cookie": String
            } ||
            {
                "Authorisation": String
            }
        }
    ) {}
lfportal commented 3 years ago

We currently don't have support for generating cookie params as defined by the OpenAPI's Parameter Locations. However there is support for optional @headers:

@request
request(
  @headers
  headers:  {
    Accept:  "*/*";
    Host: String;
    Cookie?: String; // optional
    Authorisation?: String; // optional
  }
) {}

Is this sufficient for your use case @wildcard27?

mattdillon100 commented 3 years ago

It could be, thanks @lfportal.

I am wondering if you could elaborate on the security-header part of the specification. Can this be used to globally specify the 2 optional authorisation headers mentioned above?

lfportal commented 3 years ago

It could be, thanks @lfportal.

I am wondering if you could elaborate on the security-header part of the specification. Can this be used to globally specify the 2 optional authorisation headers mentioned above?

Unfortunately @securityHeader currently has limited use. It can only be used to globally specify exactly one non-optional authorisation header:

  @securityHeader
  "security-header-name": String;

I don't think this will fit your use case. There appears to be another open issue to more broadly look at how to handle security for Spot contracts - see https://github.com/airtasker/spot/issues/1004. We can take a stab at designing a broader DSL to support security there.