spot icon indicating copy to clipboard operation
spot copied to clipboard

Conditional Types for Headers and/or Parameters

Open mattdillon100 opened this issue 5 years ago • 3 comments

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
            }
        }
    ) {}

mattdillon100 avatar Sep 25 '20 13:09 mattdillon100

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?

lfportal avatar Sep 25 '20 23:09 lfportal

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?

mattdillon100 avatar Oct 20 '20 02:10 mattdillon100

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.

lfportal avatar Oct 20 '20 09:10 lfportal