From 124dd413126f62bd847a991bb77b109fc1b84bb5 Mon Sep 17 00:00:00 2001 From: fulin <83196518+xufulin1994@users.noreply.github.com> Date: Mon, 27 Feb 2023 10:28:45 +0800 Subject: [PATCH] Support for optional headers in Reslang (#267) * 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 --- docs/reference.md | 4 ++++ docs/releases.md | 3 +++ models/authorization/testdata/parsed.expected | 2 ++ package.json | 2 +- src/grammar/rest.pegjs | 3 ++- src/main.ts | 2 +- src/swagger/http-headers/index.test.ts | 3 ++- src/swagger/http-headers/index.ts | 2 +- src/treetypes.ts | 1 + 9 files changed, 17 insertions(+), 5 deletions(-) diff --git a/docs/reference.md b/docs/reference.md index 49e14798..8a84775c 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -134,6 +134,7 @@ 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 } ``` @@ -141,6 +142,9 @@ 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: diff --git a/docs/releases.md b/docs/releases.md index 023676d3..2e99652f 100644 --- a/docs/releases.md +++ b/docs/releases.md @@ -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` diff --git a/models/authorization/testdata/parsed.expected b/models/authorization/testdata/parsed.expected index d7c1ba7c..f0a78570 100644 --- a/models/authorization/testdata/parsed.expected +++ b/models/authorization/testdata/parsed.expected @@ -607,6 +607,7 @@ "comment": "for your access token, ya know?", "headerName": "Authorization", "name": "AuthnHeader", + "required": true, "parentShort": "", "parentName": "", "secondary": false, @@ -621,6 +622,7 @@ "comment": "for your org ID, ya know?", "headerName": "LR-Org-ID", "name": "OrgIDHeader", + "required": true, "parentShort": "", "parentName": "", "secondary": false, diff --git a/package.json b/package.json index c5880274..9bff1fcb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@liveramp/reslang", - "version": "6.2.4", + "version": "6.2.5", "main": "index.js", "license": "Apache-2.0", "engines": { diff --git a/src/grammar/rest.pegjs b/src/grammar/rest.pegjs index 029826ce..ad249b76 100644 --- a/src/grammar/rest.pegjs +++ b/src/grammar/rest.pegjs @@ -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} } diff --git a/src/main.ts b/src/main.ts index 16779e87..b0cc2a7d 100755 --- a/src/main.ts +++ b/src/main.ts @@ -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 diff --git a/src/swagger/http-headers/index.test.ts b/src/swagger/http-headers/index.test.ts index d96ec6bd..78f2fefc 100644 --- a/src/swagger/http-headers/index.test.ts +++ b/src/swagger/http-headers/index.test.ts @@ -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[] diff --git a/src/swagger/http-headers/index.ts b/src/swagger/http-headers/index.ts index d4780cb2..55c72076 100644 --- a/src/swagger/http-headers/index.ts +++ b/src/swagger/http-headers/index.ts @@ -78,7 +78,7 @@ function addHeaderParamToSwaggerPath( description: headerDef.comment, in: "header", name: headerDef.headerName, - required: true, + required: headerDef.required, schema: { type: "string" } diff --git a/src/treetypes.ts b/src/treetypes.ts index 5fc9b6b6..e612f2f4 100644 --- a/src/treetypes.ts +++ b/src/treetypes.ts @@ -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 {