Skip to content

Commit

Permalink
Include response headers in the API type (#13565)
Browse files Browse the repository at this point in the history
  • Loading branch information
7omb committed Dec 6, 2022
1 parent d06ab43 commit f2321a6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Expand Up @@ -578,6 +578,25 @@ public CodegenOperation fromOperation(String resourcePath, String httpMethod, Op
returnType = "(" + returnType + ")";
}

List<CodegenProperty> headers = new ArrayList<>();
for (CodegenResponse r : op.responses) {
headers.addAll(r.headers);
}
if (!headers.isEmpty()) {
List<String> headerContents = new ArrayList<>();
for (CodegenProperty h : headers) {
// Because headers is a Map multiple Set-Cookie headers are currently not possible. If someone
// uses the workaround with null bytes, remove them and add add each header to the list:
// https://github.com/OAI/OpenAPI-Specification/issues/1237#issuecomment-906603675
String headerName = h.baseName.replaceAll("\0", "");
String headerType = h.dataType;
headerContents.add("Header \"" + headerName + "\" " + headerType);
}
String headerContent = String.join(", ", headerContents);

returnType = "(Headers '[" + headerContent + "] " + returnType + ")";
}

String code = "200";
for (CodegenResponse r : op.responses) {
if (r.code.matches("2[0-9][0-9]")) {
Expand Down
Expand Up @@ -163,7 +163,7 @@ type OpenAPIPetstoreAPI
:<|> "user" :> "createWithList" :> ReqBody '[JSON] [User] :> Verb 'POST 200 '[JSON] NoContent -- 'createUsersWithListInput' route
:<|> "user" :> Capture "username" Text :> Verb 'DELETE 200 '[JSON] NoContent -- 'deleteUser' route
:<|> "user" :> Capture "username" Text :> Verb 'GET 200 '[JSON] User -- 'getUserByName' route
:<|> "user" :> "login" :> QueryParam "username" Text :> QueryParam "password" Text :> Verb 'GET 200 '[JSON] Text -- 'loginUser' route
:<|> "user" :> "login" :> QueryParam "username" Text :> QueryParam "password" Text :> Verb 'GET 200 '[JSON] (Headers '[Header "X-Rate-Limit" Int, Header "X-Expires-After" UTCTime] Text) -- 'loginUser' route
:<|> "user" :> "logout" :> Verb 'GET 200 '[JSON] NoContent -- 'logoutUser' route
:<|> "user" :> Capture "username" Text :> ReqBody '[JSON] User :> Verb 'PUT 200 '[JSON] NoContent -- 'updateUser' route
:<|> Raw
Expand Down Expand Up @@ -203,7 +203,7 @@ data OpenAPIPetstoreBackend a m = OpenAPIPetstoreBackend
, createUsersWithListInput :: [User] -> m NoContent{- ^ -}
, deleteUser :: Text -> m NoContent{- ^ This can only be done by the logged in user. -}
, getUserByName :: Text -> m User{- ^ -}
, loginUser :: Maybe Text -> Maybe Text -> m Text{- ^ -}
, loginUser :: Maybe Text -> Maybe Text -> m (Headers '[Header "X-Rate-Limit" Int, Header "X-Expires-After" UTCTime] Text){- ^ -}
, logoutUser :: m NoContent{- ^ -}
, updateUser :: Text -> User -> m NoContent{- ^ This can only be done by the logged in user. -}
}
Expand Down

0 comments on commit f2321a6

Please sign in to comment.