Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[haskell][server] Include response headers in the API type #13565

Merged
merged 1 commit into from Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -577,6 +577,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