Skip to content

Commit

Permalink
Support for optional headers in Reslang (#267)
Browse files Browse the repository at this point in the history
* supporting optional http headers

* feat: Support for optional headers in Reslang

* feat: update httpheader test

* feat: update doc

* feat: update reference.md

* feat: update reference.md

* feat: update reference.md

---------

Co-authored-by: Yanan Xu <yanan.xu@liveramp.com>
  • Loading branch information
xufulin1994 and yanan-xu committed Feb 27, 2023
1 parent f5bfead commit 124dd41
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 5 deletions.
4 changes: 4 additions & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,17 @@ In order to require a specific HTTP header on requests, you must first add an
"The standard header for passing bearer tokens. e.g. `Bearer $ACCESS_TOKEN`"
http-header AuthHeader {
name: Authorization
required: true //it's optional and will give true if not provided
}
```

In this example, `AuthHeader` is the name of the `http-header` definition,
`"The standard header...` is the description that will appear with the header
parameter in the generated Swagger, and `Authorization` is the header name.

The `required` attribute indicates if the header is required when calling the API.
And the attribute itself is optional, the default value is `true`.

Now, the `AuthHeader` header can be referenced in the `/request-headers`
section in a resource, subresource, or action definition. For example:

Expand Down
3 changes: 3 additions & 0 deletions docs/releases.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 6.2.5 2/23/2023
- Add `required` attribute to httpHeader. Support optional HTTP request headers.

## 6.2.4 12/30/2021

- Include flag `--oneof-polymorphism` to translate `union` reslang definitions into swagger `oneOf` field instead of `allOf`
Expand Down
2 changes: 2 additions & 0 deletions models/authorization/testdata/parsed.expected
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@
"comment": "for your access token, ya know?",
"headerName": "Authorization",
"name": "AuthnHeader",
"required": true,
"parentShort": "",
"parentName": "",
"secondary": false,
Expand All @@ -621,6 +622,7 @@
"comment": "for your org ID, ya know?",
"headerName": "LR-Org-ID",
"name": "OrgIDHeader",
"required": true,
"parentShort": "",
"parentName": "",
"secondary": false,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@liveramp/reslang",
"version": "6.2.4",
"version": "6.2.5",
"main": "index.js",
"license": "Apache-2.0",
"engines": {
Expand Down
3 changes: 2 additions & 1 deletion src/grammar/rest.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ bool = b: ("true" / "false") {

httpHeader = _ comment:description? _ type:("http-header") _ name:name _ "{" _
"name:" _ headerName:[a-zA-Z0-9\-]+ _
required:("required:" _ bool _)?
"}" _ ";"? _ {
return {category: "definition", kind: type, "type": type, parents: [], "short": name, "comment": comment, "headerName": headerName.join("")}
return {category: "definition", kind: type, "type": type, parents: [], "short": name, "comment": comment, "headerName": headerName.join(""), "required": required?required[2]:true}
}
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import JsonSchemaGen from "./genjsonschema"
const RULES = "rules.json"
const LOCAL_RULES = lpath.join(__dirname, "library", RULES)

export const VERSION = "v6.2.4"
export const VERSION = "v6.2.5"

// parse the cmd line
const args = yargs
Expand Down
3 changes: 2 additions & 1 deletion src/swagger/http-headers/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ const sampleReslangDefinitions = [
kind: "http-header",
name: "MyCoolHeaderDef",
headerName: "MyCoolHeader",
comment: "This is the header you use for cool things"
comment: "This is the header you use for cool things",
required: true,
} as IHTTPHeader
] as AnyKind[]

Expand Down
2 changes: 1 addition & 1 deletion src/swagger/http-headers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function addHeaderParamToSwaggerPath(
description: headerDef.comment,
in: "header",
name: headerDef.headerName,
required: true,
required: headerDef.required,
schema: {
type: "string"
}
Expand Down
1 change: 1 addition & 0 deletions src/treetypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export interface IResourceLike extends IDefinition {
export interface IHTTPHeader extends IDefinition {
kind: "http-header"
headerName: string
required: boolean
}

export interface IEnum extends IDefinition {
Expand Down

0 comments on commit 124dd41

Please sign in to comment.