diff --git a/modules/openapi-generator/src/main/resources/erlang-server/api.mustache b/modules/openapi-generator/src/main/resources/erlang-server/api.mustache index 65e0944809b..39a11125996 100644 --- a/modules/openapi-generator/src/main/resources/erlang-server/api.mustache +++ b/modules/openapi-generator/src/main/resources/erlang-server/api.mustache @@ -261,7 +261,7 @@ validate(Rule = {pattern, Pattern}, Name, Value, _ValidatorState) -> end; validate(Rule = schema, Name, Value, ValidatorState) -> - Definition = list_to_binary("#/definitions/" ++ {{packageName}}_utils:to_list(Name)), + Definition = list_to_binary("#/components/schemas/" ++ {{packageName}}_utils:to_list(Name)), try _ = validate_with_schema(Value, Definition, ValidatorState), ok @@ -300,7 +300,7 @@ validation_error(ViolatedRule, Name, Info) -> {Value :: any(), Req :: cowboy_req:req()} | {error, Reason :: any(), Req :: cowboy_req:req()}. get_value(body, _Name, Req0) -> - {ok, Body, Req} = cowboy_req:body(Req0), + {ok, Body, Req} = cowboy_req:read_body(Req0), case prepare_body(Body) of {error, Reason} -> {error, Reason, Req}; @@ -308,19 +308,18 @@ get_value(body, _Name, Req0) -> {Value, Req} end; -get_value(qs_val, Name, Req0) -> - {QS, Req} = cowboy_req:qs_vals(Req0), +get_value(qs_val, Name, Req) -> + QS = cowboy_req:parse_qs(Req), Value = {{packageName}}_utils:get_opt({{packageName}}_utils:to_qs(Name), QS), {Value, Req}; -get_value(header, Name, Req0) -> - {Headers, Req} = cowboy_req:headers(Req0), - Value = {{packageName}}_utils:get_opt({{packageName}}_utils:to_header(Name), Headers), +get_value(header, Name, Req) -> + Headers = cowboy_req:headers(Req), + Value = maps:get({{packageName}}_utils:to_header(Name), Headers, undefined), {Value, Req}; -get_value(binding, Name, Req0) -> - {Bindings, Req} = cowboy_req:bindings(Req0), - Value = {{packageName}}_utils:get_opt({{packageName}}_utils:to_binding(Name), Bindings), +get_value(binding, Name, Req) -> + Value = cowboy_req:binding({{packageName}}_utils:to_binding(Name), Req), {Value, Req}. prepare_body(Body) -> diff --git a/modules/openapi-generator/src/main/resources/erlang-server/auth.mustache b/modules/openapi-generator/src/main/resources/erlang-server/auth.mustache index 2d3a3d17825..c651c1c80e3 100644 --- a/modules/openapi-generator/src/main/resources/erlang-server/auth.mustache +++ b/modules/openapi-generator/src/main/resources/erlang-server/auth.mustache @@ -36,18 +36,19 @@ authorize_api_key(LogicHandler, OperationID, From, KeyParam, Req0) -> end end. -get_api_key(header, KeyParam, Req0) -> - {Headers, Req} = cowboy_req:headers(Req0), +get_api_key(header, KeyParam, Req) -> + Headers = cowboy_req:headers(Req), { - openapi_utils:get_opt( - {{packageName}}_utils:to_header(KeyParam), - Headers + maps:get( + openapi_utils:to_header(KeyParam), + Headers, + undefined ), Req }; -get_api_key(qs_val, KeyParam, Req0) -> - {QS, Req} = cowboy_req:qs_vals(Req0), +get_api_key(qs_val, KeyParam, Req) -> + QS = cowboy_req:parse_qs(Req), { {{packageName}}_utils:get_opt(KeyParam, QS), Req}. diff --git a/modules/openapi-generator/src/main/resources/erlang-server/default_logic_handler.mustache b/modules/openapi-generator/src/main/resources/erlang-server/default_logic_handler.mustache index 3be26911c74..1b39ed1d75a 100644 --- a/modules/openapi-generator/src/main/resources/erlang-server/default_logic_handler.mustache +++ b/modules/openapi-generator/src/main/resources/erlang-server/default_logic_handler.mustache @@ -22,11 +22,11 @@ authorize_api_key(_, _) -> {true, #{}}. Req :: cowboy_req:req(), Context :: #{} ) -> - {Status :: cowboy:http_status(), Headers :: cowboy:http_headers(), Body :: #{}}. + {Status :: cowboy:http_status(), Headers :: cowboy:http_headers(), Body :: jsx:json_term()}. handle_request(OperationID, Req, Context) -> error_logger:error_msg( "Got not implemented request to process: ~p~n", [{OperationID, Req, Context}] ), - {501, [], #{}}. + {501, #{}, #{}}. diff --git a/modules/openapi-generator/src/main/resources/erlang-server/handler.mustache b/modules/openapi-generator/src/main/resources/erlang-server/handler.mustache index 9796d860f93..7117dc31ae9 100644 --- a/modules/openapi-generator/src/main/resources/erlang-server/handler.mustache +++ b/modules/openapi-generator/src/main/resources/erlang-server/handler.mustache @@ -3,8 +3,7 @@ %% Cowboy REST callbacks -export([allowed_methods/2]). --export([init/3]). --export([rest_init/2]). +-export([init/2]). -export([allow_missing_post/2]). -export([content_types_accepted/2]). -export([content_types_provided/2]). @@ -27,17 +26,11 @@ -type state() :: state(). --spec init(TransportName :: atom(), Req :: cowboy_req:req(), Opts :: {{packageName}}_router:init_opts()) -> - {upgrade, protocol, cowboy_rest, Req :: cowboy_req:req(), Opts :: {{packageName}}_router:init_opts()}. +-spec init(Req :: cowboy_req:req(), Opts :: {{packageName}}_router:init_opts()) -> + {cowboy_rest, Req :: cowboy_req:req(), State :: state()}. -init(_Transport, Req, Opts) -> - {upgrade, protocol, cowboy_rest, Req, Opts}. - --spec rest_init(Req :: cowboy_req:req(), Opts :: {{packageName}}_router:init_opts()) -> - {ok, Req :: cowboy_req:req(), State :: state()}. - -rest_init(Req0, {Operations, LogicHandler, ValidatorState}) -> - {Method, Req} = cowboy_req:method(Req0), +init(Req, {Operations, LogicHandler, ValidatorState}) -> + Method = cowboy_req:method(Req), OperationID = maps:get(Method, Operations, undefined), error_logger:info_msg("Attempt to process operation: ~p", [OperationID]), @@ -47,7 +40,7 @@ rest_init(Req0, {Operations, LogicHandler, ValidatorState}) -> logic_handler = LogicHandler, validator_state = ValidatorState }, - {ok, Req, State}. + {cowboy_rest, Req, State}. -spec allowed_methods(Req :: cowboy_req:req(), State :: state()) -> {Value :: [binary()], Req :: cowboy_req:req(), State :: state()}. @@ -192,7 +185,6 @@ valid_entity_length(Req, State) -> {true, Req, State}. %%%% - -type result_ok() :: { ok, {Status :: cowboy:http_status(), Headers :: cowboy:http_headers(), Body :: iodata()} @@ -200,7 +192,7 @@ valid_entity_length(Req, State) -> -type result_error() :: {error, Reason :: any()}. --type processed_response() :: {halt, cowboy_req:req(), state()}. +-type processed_response() :: {stop, cowboy_req:req(), state()}. -spec process_response(result_ok() | result_error(), cowboy_req:req(), state()) -> processed_response(). @@ -208,24 +200,23 @@ valid_entity_length(Req, State) -> process_response(Response, Req0, State = #state{operation_id = OperationID}) -> case Response of {ok, {Code, Headers, Body}} -> - {ok, Req} = cowboy_req:reply(Code, Headers, Body, Req0), - {halt, Req, State}; + Req = cowboy_req:reply(Code, Headers, Body, Req0), + {stop, Req, State}; {error, Message} -> error_logger:error_msg("Unable to process request for ~p: ~p", [OperationID, Message]), - {ok, Req} = cowboy_req:reply(400, Req0), - {halt, Req, State} + Req = cowboy_req:reply(400, Req0), + {stop, Req, State} end. --spec handle_request_json(cowboy_req:req(), state()) -> {halt, cowboy_req:req(), state()}. +-spec handle_request_json(cowboy_req:req(), state()) -> {cowboy_req:resp_body(), cowboy_req:req(), state()}. handle_request_json( Req0, State = #state{ operation_id = OperationID, logic_handler = LogicHandler, - validator_state = ValidatorState, - context = Context + validator_state = ValidatorState } ) -> case {{packageName}}_api:populate_request(OperationID, Req0, ValidatorState) of @@ -233,8 +224,8 @@ handle_request_json( {Code, Headers, Body} = {{packageName}}_logic_handler:handle_request( LogicHandler, OperationID, - Populated, - Context + Req1, + Populated ), _ = {{packageName}}_api:validate_response( OperationID, diff --git a/modules/openapi-generator/src/main/resources/erlang-server/logic_handler.mustache b/modules/openapi-generator/src/main/resources/erlang-server/logic_handler.mustache index 8baf9b81c20..509ed3107b7 100644 --- a/modules/openapi-generator/src/main/resources/erlang-server/logic_handler.mustache +++ b/modules/openapi-generator/src/main/resources/erlang-server/logic_handler.mustache @@ -22,13 +22,13 @@ {{/authMethods}} --callback handle_request(OperationID :: {{packageName}}_api:operation_id(), Request :: any(), Context :: context()) -> +-callback handle_request(OperationID :: {{packageName}}_api:operation_id(), cowboy_req:req(), Context :: context()) -> handler_response(). -spec handle_request( Handler :: atom(), OperationID :: {{packageName}}_api:operation_id(), - Request :: any(), + Request :: cowboy_req:req(), Context :: context() ) -> handler_response(). diff --git a/modules/openapi-generator/src/main/resources/erlang-server/rebar.config.mustache b/modules/openapi-generator/src/main/resources/erlang-server/rebar.config.mustache index 1d01cefa83b..7ddca0f3027 100644 --- a/modules/openapi-generator/src/main/resources/erlang-server/rebar.config.mustache +++ b/modules/openapi-generator/src/main/resources/erlang-server/rebar.config.mustache @@ -1,4 +1,6 @@ {deps, [ - {jsx, {git, "https://github.com/talentdeficit/jsx.git", {tag, "2.8.2"}}}, - {jesse, {git, "https://github.com/for-GET/jesse.git", {tag, "1.4.0"}}} + {cowboy, {git, "https://github.com/ninenines/cowboy.git", {tag, "2.5.0"}}}, + {rfc3339, {git, "https://github.com/talentdeficit/rfc3339.git", {tag, "master"}}}, + {jsx, {git, "https://github.com/talentdeficit/jsx.git", {tag, "2.9.0"}}}, + {jesse, {git, "https://github.com/for-GET/jesse.git", {tag, "1.5.2"}}} ]}. diff --git a/modules/openapi-generator/src/main/resources/erlang-server/server.mustache b/modules/openapi-generator/src/main/resources/erlang-server/server.mustache index 348a473e9e3..204a8fd8659 100644 --- a/modules/openapi-generator/src/main/resources/erlang-server/server.mustache +++ b/modules/openapi-generator/src/main/resources/erlang-server/server.mustache @@ -1,30 +1,32 @@ -module({{packageName}}_server). --define(DEFAULT_ACCEPTORS_POOLSIZE, 100). -define(DEFAULT_LOGIC_HANDLER, {{packageName}}_default_logic_handler). --export([child_spec/2]). +-export([start/2]). --spec child_spec( ID :: any(), #{ +-spec start( ID :: any(), #{ ip => inet:ip_address(), port => inet:port_number(), logic_handler => module(), net_opts => [] -}) -> supervisor:child_spec(). +}) -> {ok, pid()} | {error, any()}. -child_spec(ID, #{ +start(ID, #{ ip := IP , port := Port, net_opts := NetOpts } = Params) -> - AcceptorsPool = ?DEFAULT_ACCEPTORS_POOLSIZE, {Transport, TransportOpts} = get_socket_transport(IP, Port, NetOpts), LogicHandler = maps:get(logic_handler, Params, ?DEFAULT_LOGIC_HANDLER), ExtraOpts = maps:get(cowboy_extra_opts, Params, []), CowboyOpts = get_cowboy_config(LogicHandler, ExtraOpts), - ranch:child_spec({?MODULE, ID}, AcceptorsPool, - Transport, TransportOpts, cowboy_protocol, CowboyOpts). + case Transport of + ssl -> + cowboy:start_tls(ID, TransportOpts, CowboyOpts); + tcp -> + cowboy:start_clear(ID, TransportOpts, CowboyOpts) + end. get_socket_transport(IP, Port, Options) -> Opts = [ @@ -33,9 +35,9 @@ get_socket_transport(IP, Port, Options) -> ], case {{packageName}}_utils:get_opt(ssl, Options) of SslOpts = [_|_] -> - {ranch_ssl, Opts ++ SslOpts}; + {ssl, Opts ++ SslOpts}; undefined -> - {ranch_tcp, Opts} + {tcp, Opts} end. get_cowboy_config(LogicHandler, ExtraOpts) -> @@ -56,10 +58,10 @@ get_cowboy_config(LogicHandler, [{Key, Value}| Rest], Opts) -> get_default_dispatch(LogicHandler) -> Paths = {{packageName}}_router:get_paths(LogicHandler), - {dispatch, cowboy_router:compile(Paths)}. + #{dispatch => cowboy_router:compile(Paths)}. get_default_opts(LogicHandler) -> - [{env, [get_default_dispatch(LogicHandler)]}]. + #{env => get_default_dispatch(LogicHandler)}. store_key(Key, Value, Opts) -> lists:keystore(Key, 1, Opts, {Key, Value}). diff --git a/pom.xml b/pom.xml index 2599c49f578..9fbb2523e85 100644 --- a/pom.xml +++ b/pom.xml @@ -1223,8 +1223,8 @@ samples/client/petstore/elixir samples/client/petstore/erlang-client - + + samples/server/petstore/erlang-server samples/server/petstore/jaxrs/jersey2 samples/server/petstore/jaxrs/jersey2-useTags samples/server/petstore/spring-mvc diff --git a/samples/server/petstore/erlang-server/.openapi-generator/VERSION b/samples/server/petstore/erlang-server/.openapi-generator/VERSION index a6527129083..e24c1f857e0 100644 --- a/samples/server/petstore/erlang-server/.openapi-generator/VERSION +++ b/samples/server/petstore/erlang-server/.openapi-generator/VERSION @@ -1 +1 @@ -3.3.2-SNAPSHOT \ No newline at end of file +3.3.3-SNAPSHOT \ No newline at end of file diff --git a/samples/server/petstore/erlang-server/priv/swagger.json b/samples/server/petstore/erlang-server/priv/swagger.json deleted file mode 100644 index 9f7abc12961..00000000000 --- a/samples/server/petstore/erlang-server/priv/swagger.json +++ /dev/null @@ -1,831 +0,0 @@ -{ - "swagger" : "2.0", - "info" : { - "description" : "This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters.", - "version" : "1.0.0", - "title" : "Swagger Petstore", - "termsOfService" : "http://swagger.io/terms/", - "contact" : { - "email" : "apiteam@swagger.io" - }, - "license" : { - "name" : "Apache-2.0", - "url" : "http://www.apache.org/licenses/LICENSE-2.0.html" - } - }, - "host" : "petstore.swagger.io", - "basePath" : "/v2", - "tags" : [ { - "name" : "pet", - "description" : "Everything about your Pets", - "externalDocs" : { - "description" : "Find out more", - "url" : "http://swagger.io" - } - }, { - "name" : "store", - "description" : "Access to Petstore orders" - }, { - "name" : "user", - "description" : "Operations about user", - "externalDocs" : { - "description" : "Find out more about our store", - "url" : "http://swagger.io" - } - } ], - "schemes" : [ "http" ], - "paths" : { - "/pet" : { - "post" : { - "tags" : [ "pet" ], - "summary" : "Add a new pet to the store", - "description" : "", - "operationId" : "addPet", - "consumes" : [ "application/json", "application/xml" ], - "produces" : [ "application/xml", "application/json" ], - "parameters" : [ { - "in" : "body", - "name" : "body", - "description" : "Pet object that needs to be added to the store", - "required" : true, - "schema" : { - "$ref" : "#/definitions/Pet" - } - } ], - "responses" : { - "405" : { - "description" : "Invalid input" - } - }, - "security" : [ { - "petstore_auth" : [ "write:pets", "read:pets" ] - } ] - }, - "put" : { - "tags" : [ "pet" ], - "summary" : "Update an existing pet", - "description" : "", - "operationId" : "updatePet", - "consumes" : [ "application/json", "application/xml" ], - "produces" : [ "application/xml", "application/json" ], - "parameters" : [ { - "in" : "body", - "name" : "body", - "description" : "Pet object that needs to be added to the store", - "required" : true, - "schema" : { - "$ref" : "#/definitions/Pet" - } - } ], - "responses" : { - "400" : { - "description" : "Invalid ID supplied" - }, - "404" : { - "description" : "Pet not found" - }, - "405" : { - "description" : "Validation exception" - } - }, - "security" : [ { - "petstore_auth" : [ "write:pets", "read:pets" ] - } ] - } - }, - "/pet/findByStatus" : { - "get" : { - "tags" : [ "pet" ], - "summary" : "Finds Pets by status", - "description" : "Multiple status values can be provided with comma separated strings", - "operationId" : "findPetsByStatus", - "produces" : [ "application/xml", "application/json" ], - "parameters" : [ { - "name" : "status", - "in" : "query", - "description" : "Status values that need to be considered for filter", - "required" : true, - "type" : "array", - "items" : { - "type" : "string", - "enum" : [ "available", "pending", "sold" ], - "default" : "available" - }, - "collectionFormat" : "csv" - } ], - "responses" : { - "200" : { - "description" : "successful operation", - "schema" : { - "type" : "array", - "items" : { - "$ref" : "#/definitions/Pet" - } - } - }, - "400" : { - "description" : "Invalid status value" - } - }, - "security" : [ { - "petstore_auth" : [ "write:pets", "read:pets" ] - } ] - } - }, - "/pet/findByTags" : { - "get" : { - "tags" : [ "pet" ], - "summary" : "Finds Pets by tags", - "description" : "Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.", - "operationId" : "findPetsByTags", - "produces" : [ "application/xml", "application/json" ], - "parameters" : [ { - "name" : "tags", - "in" : "query", - "description" : "Tags to filter by", - "required" : true, - "type" : "array", - "items" : { - "type" : "string" - }, - "collectionFormat" : "csv" - } ], - "responses" : { - "200" : { - "description" : "successful operation", - "schema" : { - "type" : "array", - "items" : { - "$ref" : "#/definitions/Pet" - } - } - }, - "400" : { - "description" : "Invalid tag value" - } - }, - "security" : [ { - "petstore_auth" : [ "write:pets", "read:pets" ] - } ], - "deprecated" : true - } - }, - "/pet/{petId}" : { - "get" : { - "tags" : [ "pet" ], - "summary" : "Find pet by ID", - "description" : "Returns a single pet", - "operationId" : "getPetById", - "produces" : [ "application/xml", "application/json" ], - "parameters" : [ { - "name" : "petId", - "in" : "path", - "description" : "ID of pet to return", - "required" : true, - "type" : "integer", - "format" : "int64" - } ], - "responses" : { - "200" : { - "description" : "successful operation", - "schema" : { - "$ref" : "#/definitions/Pet" - } - }, - "400" : { - "description" : "Invalid ID supplied" - }, - "404" : { - "description" : "Pet not found" - } - }, - "security" : [ { - "api_key" : [ ] - } ] - }, - "post" : { - "tags" : [ "pet" ], - "summary" : "Updates a pet in the store with form data", - "description" : "", - "operationId" : "updatePetWithForm", - "consumes" : [ "application/x-www-form-urlencoded" ], - "produces" : [ "application/xml", "application/json" ], - "parameters" : [ { - "name" : "petId", - "in" : "path", - "description" : "ID of pet that needs to be updated", - "required" : true, - "type" : "integer", - "format" : "int64" - }, { - "name" : "name", - "in" : "formData", - "description" : "Updated name of the pet", - "required" : false, - "type" : "string" - }, { - "name" : "status", - "in" : "formData", - "description" : "Updated status of the pet", - "required" : false, - "type" : "string" - } ], - "responses" : { - "405" : { - "description" : "Invalid input" - } - }, - "security" : [ { - "petstore_auth" : [ "write:pets", "read:pets" ] - } ] - }, - "delete" : { - "tags" : [ "pet" ], - "summary" : "Deletes a pet", - "description" : "", - "operationId" : "deletePet", - "produces" : [ "application/xml", "application/json" ], - "parameters" : [ { - "name" : "api_key", - "in" : "header", - "required" : false, - "type" : "string" - }, { - "name" : "petId", - "in" : "path", - "description" : "Pet id to delete", - "required" : true, - "type" : "integer", - "format" : "int64" - } ], - "responses" : { - "400" : { - "description" : "Invalid pet value" - } - }, - "security" : [ { - "petstore_auth" : [ "write:pets", "read:pets" ] - } ] - } - }, - "/pet/{petId}/uploadImage" : { - "post" : { - "tags" : [ "pet" ], - "summary" : "uploads an image", - "description" : "", - "operationId" : "uploadFile", - "consumes" : [ "multipart/form-data" ], - "produces" : [ "application/json" ], - "parameters" : [ { - "name" : "petId", - "in" : "path", - "description" : "ID of pet to update", - "required" : true, - "type" : "integer", - "format" : "int64" - }, { - "name" : "additionalMetadata", - "in" : "formData", - "description" : "Additional data to pass to server", - "required" : false, - "type" : "string" - }, { - "name" : "file", - "in" : "formData", - "description" : "file to upload", - "required" : false, - "type" : "file" - } ], - "responses" : { - "200" : { - "description" : "successful operation", - "schema" : { - "$ref" : "#/definitions/ApiResponse" - } - } - }, - "security" : [ { - "petstore_auth" : [ "write:pets", "read:pets" ] - } ] - } - }, - "/store/inventory" : { - "get" : { - "tags" : [ "store" ], - "summary" : "Returns pet inventories by status", - "description" : "Returns a map of status codes to quantities", - "operationId" : "getInventory", - "produces" : [ "application/json" ], - "parameters" : [ ], - "responses" : { - "200" : { - "description" : "successful operation", - "schema" : { - "type" : "object", - "additionalProperties" : { - "type" : "integer", - "format" : "int32" - } - } - } - }, - "security" : [ { - "api_key" : [ ] - } ] - } - }, - "/store/order" : { - "post" : { - "tags" : [ "store" ], - "summary" : "Place an order for a pet", - "description" : "", - "operationId" : "placeOrder", - "produces" : [ "application/xml", "application/json" ], - "parameters" : [ { - "in" : "body", - "name" : "body", - "description" : "order placed for purchasing the pet", - "required" : true, - "schema" : { - "$ref" : "#/definitions/Order" - } - } ], - "responses" : { - "200" : { - "description" : "successful operation", - "schema" : { - "$ref" : "#/definitions/Order" - } - }, - "400" : { - "description" : "Invalid Order" - } - } - } - }, - "/store/order/{orderId}" : { - "get" : { - "tags" : [ "store" ], - "summary" : "Find purchase order by ID", - "description" : "For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions", - "operationId" : "getOrderById", - "produces" : [ "application/xml", "application/json" ], - "parameters" : [ { - "name" : "orderId", - "in" : "path", - "description" : "ID of pet that needs to be fetched", - "required" : true, - "type" : "integer", - "maximum" : 5, - "minimum" : 1, - "format" : "int64" - } ], - "responses" : { - "200" : { - "description" : "successful operation", - "schema" : { - "$ref" : "#/definitions/Order" - } - }, - "400" : { - "description" : "Invalid ID supplied" - }, - "404" : { - "description" : "Order not found" - } - } - }, - "delete" : { - "tags" : [ "store" ], - "summary" : "Delete purchase order by ID", - "description" : "For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors", - "operationId" : "deleteOrder", - "produces" : [ "application/xml", "application/json" ], - "parameters" : [ { - "name" : "orderId", - "in" : "path", - "description" : "ID of the order that needs to be deleted", - "required" : true, - "type" : "string" - } ], - "responses" : { - "400" : { - "description" : "Invalid ID supplied" - }, - "404" : { - "description" : "Order not found" - } - } - } - }, - "/user" : { - "post" : { - "tags" : [ "user" ], - "summary" : "Create user", - "description" : "This can only be done by the logged in user.", - "operationId" : "createUser", - "produces" : [ "application/xml", "application/json" ], - "parameters" : [ { - "in" : "body", - "name" : "body", - "description" : "Created user object", - "required" : true, - "schema" : { - "$ref" : "#/definitions/User" - } - } ], - "responses" : { - "default" : { - "description" : "successful operation" - } - } - } - }, - "/user/createWithArray" : { - "post" : { - "tags" : [ "user" ], - "summary" : "Creates list of users with given input array", - "description" : "", - "operationId" : "createUsersWithArrayInput", - "produces" : [ "application/xml", "application/json" ], - "parameters" : [ { - "in" : "body", - "name" : "body", - "description" : "List of user object", - "required" : true, - "schema" : { - "type" : "array", - "items" : { - "$ref" : "#/definitions/User" - } - } - } ], - "responses" : { - "default" : { - "description" : "successful operation" - } - } - } - }, - "/user/createWithList" : { - "post" : { - "tags" : [ "user" ], - "summary" : "Creates list of users with given input array", - "description" : "", - "operationId" : "createUsersWithListInput", - "produces" : [ "application/xml", "application/json" ], - "parameters" : [ { - "in" : "body", - "name" : "body", - "description" : "List of user object", - "required" : true, - "schema" : { - "type" : "array", - "items" : { - "$ref" : "#/definitions/User" - } - } - } ], - "responses" : { - "default" : { - "description" : "successful operation" - } - } - } - }, - "/user/login" : { - "get" : { - "tags" : [ "user" ], - "summary" : "Logs user into the system", - "description" : "", - "operationId" : "loginUser", - "produces" : [ "application/xml", "application/json" ], - "parameters" : [ { - "name" : "username", - "in" : "query", - "description" : "The user name for login", - "required" : true, - "type" : "string" - }, { - "name" : "password", - "in" : "query", - "description" : "The password for login in clear text", - "required" : true, - "type" : "string" - } ], - "responses" : { - "200" : { - "description" : "successful operation", - "schema" : { - "type" : "string" - }, - "headers" : { - "X-Rate-Limit" : { - "type" : "integer", - "format" : "int32", - "description" : "calls per hour allowed by the user" - }, - "X-Expires-After" : { - "type" : "string", - "format" : "date-time", - "description" : "date in UTC when toekn expires" - } - } - }, - "400" : { - "description" : "Invalid username/password supplied" - } - } - } - }, - "/user/logout" : { - "get" : { - "tags" : [ "user" ], - "summary" : "Logs out current logged in user session", - "description" : "", - "operationId" : "logoutUser", - "produces" : [ "application/xml", "application/json" ], - "parameters" : [ ], - "responses" : { - "default" : { - "description" : "successful operation" - } - } - } - }, - "/user/{username}" : { - "get" : { - "tags" : [ "user" ], - "summary" : "Get user by user name", - "description" : "", - "operationId" : "getUserByName", - "produces" : [ "application/xml", "application/json" ], - "parameters" : [ { - "name" : "username", - "in" : "path", - "description" : "The name that needs to be fetched. Use user1 for testing. ", - "required" : true, - "type" : "string" - } ], - "responses" : { - "200" : { - "description" : "successful operation", - "schema" : { - "$ref" : "#/definitions/User" - } - }, - "400" : { - "description" : "Invalid username supplied" - }, - "404" : { - "description" : "User not found" - } - } - }, - "put" : { - "tags" : [ "user" ], - "summary" : "Updated user", - "description" : "This can only be done by the logged in user.", - "operationId" : "updateUser", - "produces" : [ "application/xml", "application/json" ], - "parameters" : [ { - "name" : "username", - "in" : "path", - "description" : "name that need to be deleted", - "required" : true, - "type" : "string" - }, { - "in" : "body", - "name" : "body", - "description" : "Updated user object", - "required" : true, - "schema" : { - "$ref" : "#/definitions/User" - } - } ], - "responses" : { - "400" : { - "description" : "Invalid user supplied" - }, - "404" : { - "description" : "User not found" - } - } - }, - "delete" : { - "tags" : [ "user" ], - "summary" : "Delete user", - "description" : "This can only be done by the logged in user.", - "operationId" : "deleteUser", - "produces" : [ "application/xml", "application/json" ], - "parameters" : [ { - "name" : "username", - "in" : "path", - "description" : "The name that needs to be deleted", - "required" : true, - "type" : "string" - } ], - "responses" : { - "400" : { - "description" : "Invalid username supplied" - }, - "404" : { - "description" : "User not found" - } - } - } - } - }, - "securityDefinitions" : { - "petstore_auth" : { - "type" : "oauth2", - "authorizationUrl" : "http://petstore.swagger.io/api/oauth/dialog", - "flow" : "implicit", - "scopes" : { - "write:pets" : "modify pets in your account", - "read:pets" : "read your pets" - } - }, - "api_key" : { - "type" : "apiKey", - "name" : "api_key", - "in" : "header" - } - }, - "definitions" : { - "Order" : { - "type" : "object", - "properties" : { - "id" : { - "type" : "integer", - "format" : "int64" - }, - "petId" : { - "type" : "integer", - "format" : "int64" - }, - "quantity" : { - "type" : "integer", - "format" : "int32" - }, - "shipDate" : { - "type" : "string", - "format" : "date-time" - }, - "status" : { - "type" : "string", - "description" : "Order Status", - "enum" : [ "placed", "approved", "delivered" ] - }, - "complete" : { - "type" : "boolean", - "default" : false - } - }, - "title" : "Pet Order", - "description" : "An order for a pets from the pet store", - "xml" : { - "name" : "Order" - } - }, - "Category" : { - "type" : "object", - "properties" : { - "id" : { - "type" : "integer", - "format" : "int64" - }, - "name" : { - "type" : "string" - } - }, - "title" : "Pet category", - "description" : "A category for a pet", - "xml" : { - "name" : "Category" - } - }, - "User" : { - "type" : "object", - "properties" : { - "id" : { - "type" : "integer", - "format" : "int64" - }, - "username" : { - "type" : "string" - }, - "firstName" : { - "type" : "string" - }, - "lastName" : { - "type" : "string" - }, - "email" : { - "type" : "string" - }, - "password" : { - "type" : "string" - }, - "phone" : { - "type" : "string" - }, - "userStatus" : { - "type" : "integer", - "format" : "int32", - "description" : "User Status" - } - }, - "title" : "a User", - "description" : "A User who is purchasing from the pet store", - "xml" : { - "name" : "User" - } - }, - "Tag" : { - "type" : "object", - "properties" : { - "id" : { - "type" : "integer", - "format" : "int64" - }, - "name" : { - "type" : "string" - } - }, - "title" : "Pet Tag", - "description" : "A tag for a pet", - "xml" : { - "name" : "Tag" - } - }, - "Pet" : { - "type" : "object", - "required" : [ "name", "photoUrls" ], - "properties" : { - "id" : { - "type" : "integer", - "format" : "int64" - }, - "category" : { - "$ref" : "#/definitions/Category" - }, - "name" : { - "type" : "string", - "example" : "doggie" - }, - "photoUrls" : { - "type" : "array", - "xml" : { - "name" : "photoUrl", - "wrapped" : true - }, - "items" : { - "type" : "string" - } - }, - "tags" : { - "type" : "array", - "xml" : { - "name" : "tag", - "wrapped" : true - }, - "items" : { - "$ref" : "#/definitions/Tag" - } - }, - "status" : { - "type" : "string", - "description" : "pet status in the store", - "enum" : [ "available", "pending", "sold" ] - } - }, - "title" : "a Pet", - "description" : "A pet for sale in the pet store", - "xml" : { - "name" : "Pet" - } - }, - "ApiResponse" : { - "type" : "object", - "properties" : { - "code" : { - "type" : "integer", - "format" : "int32" - }, - "type" : { - "type" : "string" - }, - "message" : { - "type" : "string" - } - }, - "title" : "An uploaded response", - "description" : "Describes the result of uploading an image resource" - } - }, - "externalDocs" : { - "description" : "Find out more about Swagger", - "url" : "http://swagger.io" - } -} diff --git a/samples/server/petstore/erlang-server/rebar.config b/samples/server/petstore/erlang-server/rebar.config index 1d01cefa83b..7ddca0f3027 100644 --- a/samples/server/petstore/erlang-server/rebar.config +++ b/samples/server/petstore/erlang-server/rebar.config @@ -1,4 +1,6 @@ {deps, [ - {jsx, {git, "https://github.com/talentdeficit/jsx.git", {tag, "2.8.2"}}}, - {jesse, {git, "https://github.com/for-GET/jesse.git", {tag, "1.4.0"}}} + {cowboy, {git, "https://github.com/ninenines/cowboy.git", {tag, "2.5.0"}}}, + {rfc3339, {git, "https://github.com/talentdeficit/rfc3339.git", {tag, "master"}}}, + {jsx, {git, "https://github.com/talentdeficit/jsx.git", {tag, "2.9.0"}}}, + {jesse, {git, "https://github.com/for-GET/jesse.git", {tag, "1.5.2"}}} ]}. diff --git a/samples/server/petstore/erlang-server/src/openapi_api.erl b/samples/server/petstore/erlang-server/src/openapi_api.erl index 3353e6669c7..ba4b3a0e7af 100644 --- a/samples/server/petstore/erlang-server/src/openapi_api.erl +++ b/samples/server/petstore/erlang-server/src/openapi_api.erl @@ -649,7 +649,7 @@ validate(Rule = {pattern, Pattern}, Name, Value, _ValidatorState) -> end; validate(Rule = schema, Name, Value, ValidatorState) -> - Definition = list_to_binary("#/definitions/" ++ openapi_utils:to_list(Name)), + Definition = list_to_binary("#/components/schemas/" ++ openapi_utils:to_list(Name)), try _ = validate_with_schema(Value, Definition, ValidatorState), ok @@ -688,7 +688,7 @@ validation_error(ViolatedRule, Name, Info) -> {Value :: any(), Req :: cowboy_req:req()} | {error, Reason :: any(), Req :: cowboy_req:req()}. get_value(body, _Name, Req0) -> - {ok, Body, Req} = cowboy_req:body(Req0), + {ok, Body, Req} = cowboy_req:read_body(Req0), case prepare_body(Body) of {error, Reason} -> {error, Reason, Req}; @@ -696,19 +696,18 @@ get_value(body, _Name, Req0) -> {Value, Req} end; -get_value(qs_val, Name, Req0) -> - {QS, Req} = cowboy_req:qs_vals(Req0), +get_value(qs_val, Name, Req) -> + QS = cowboy_req:parse_qs(Req), Value = openapi_utils:get_opt(openapi_utils:to_qs(Name), QS), {Value, Req}; -get_value(header, Name, Req0) -> - {Headers, Req} = cowboy_req:headers(Req0), - Value = openapi_utils:get_opt(openapi_utils:to_header(Name), Headers), +get_value(header, Name, Req) -> + Headers = cowboy_req:headers(Req), + Value = maps:get(openapi_utils:to_header(Name), Headers, undefined), {Value, Req}; -get_value(binding, Name, Req0) -> - {Bindings, Req} = cowboy_req:bindings(Req0), - Value = openapi_utils:get_opt(openapi_utils:to_binding(Name), Bindings), +get_value(binding, Name, Req) -> + Value = cowboy_req:binding(openapi_utils:to_binding(Name), Req), {Value, Req}. prepare_body(Body) -> diff --git a/samples/server/petstore/erlang-server/src/openapi_auth.erl b/samples/server/petstore/erlang-server/src/openapi_auth.erl index 5d9c20f73a1..06f1f163953 100644 --- a/samples/server/petstore/erlang-server/src/openapi_auth.erl +++ b/samples/server/petstore/erlang-server/src/openapi_auth.erl @@ -32,18 +32,19 @@ authorize_api_key(LogicHandler, OperationID, From, KeyParam, Req0) -> end end. -get_api_key(header, KeyParam, Req0) -> - {Headers, Req} = cowboy_req:headers(Req0), +get_api_key(header, KeyParam, Req) -> + Headers = cowboy_req:headers(Req), { - openapi_utils:get_opt( + maps:get( openapi_utils:to_header(KeyParam), - Headers + Headers, + undefined ), Req }; -get_api_key(qs_val, KeyParam, Req0) -> - {QS, Req} = cowboy_req:qs_vals(Req0), +get_api_key(qs_val, KeyParam, Req) -> + QS = cowboy_req:parse_qs(Req), { openapi_utils:get_opt(KeyParam, QS), Req}. diff --git a/samples/server/petstore/erlang-server/src/openapi_default_logic_handler.erl b/samples/server/petstore/erlang-server/src/openapi_default_logic_handler.erl index 8c45e87675e..da6e79a74eb 100644 --- a/samples/server/petstore/erlang-server/src/openapi_default_logic_handler.erl +++ b/samples/server/petstore/erlang-server/src/openapi_default_logic_handler.erl @@ -14,11 +14,11 @@ authorize_api_key(_, _) -> {true, #{}}. Req :: cowboy_req:req(), Context :: #{} ) -> - {Status :: cowboy:http_status(), Headers :: cowboy:http_headers(), Body :: #{}}. + {Status :: cowboy:http_status(), Headers :: cowboy:http_headers(), Body :: jsx:json_term()}. handle_request(OperationID, Req, Context) -> error_logger:error_msg( "Got not implemented request to process: ~p~n", [{OperationID, Req, Context}] ), - {501, [], #{}}. + {501, #{}, #{}}. diff --git a/samples/server/petstore/erlang-server/src/openapi_logic_handler.erl b/samples/server/petstore/erlang-server/src/openapi_logic_handler.erl index 812944721b1..2c05ce24836 100644 --- a/samples/server/petstore/erlang-server/src/openapi_logic_handler.erl +++ b/samples/server/petstore/erlang-server/src/openapi_logic_handler.erl @@ -18,13 +18,13 @@ Result :: boolean() | {boolean(), context()}. --callback handle_request(OperationID :: openapi_api:operation_id(), Request :: any(), Context :: context()) -> +-callback handle_request(OperationID :: openapi_api:operation_id(), cowboy_req:req(), Context :: context()) -> handler_response(). -spec handle_request( Handler :: atom(), OperationID :: openapi_api:operation_id(), - Request :: any(), + Request :: cowboy_req:req(), Context :: context() ) -> handler_response(). diff --git a/samples/server/petstore/erlang-server/src/openapi_pet_handler.erl b/samples/server/petstore/erlang-server/src/openapi_pet_handler.erl index b30c53efdea..e63d95fa30b 100644 --- a/samples/server/petstore/erlang-server/src/openapi_pet_handler.erl +++ b/samples/server/petstore/erlang-server/src/openapi_pet_handler.erl @@ -3,8 +3,7 @@ %% Cowboy REST callbacks -export([allowed_methods/2]). --export([init/3]). --export([rest_init/2]). +-export([init/2]). -export([allow_missing_post/2]). -export([content_types_accepted/2]). -export([content_types_provided/2]). @@ -27,17 +26,11 @@ -type state() :: state(). --spec init(TransportName :: atom(), Req :: cowboy_req:req(), Opts :: openapi_router:init_opts()) -> - {upgrade, protocol, cowboy_rest, Req :: cowboy_req:req(), Opts :: openapi_router:init_opts()}. +-spec init(Req :: cowboy_req:req(), Opts :: openapi_router:init_opts()) -> + {cowboy_rest, Req :: cowboy_req:req(), State :: state()}. -init(_Transport, Req, Opts) -> - {upgrade, protocol, cowboy_rest, Req, Opts}. - --spec rest_init(Req :: cowboy_req:req(), Opts :: openapi_router:init_opts()) -> - {ok, Req :: cowboy_req:req(), State :: state()}. - -rest_init(Req0, {Operations, LogicHandler, ValidatorState}) -> - {Method, Req} = cowboy_req:method(Req0), +init(Req, {Operations, LogicHandler, ValidatorState}) -> + Method = cowboy_req:method(Req), OperationID = maps:get(Method, Operations, undefined), error_logger:info_msg("Attempt to process operation: ~p", [OperationID]), @@ -47,7 +40,7 @@ rest_init(Req0, {Operations, LogicHandler, ValidatorState}) -> logic_handler = LogicHandler, validator_state = ValidatorState }, - {ok, Req, State}. + {cowboy_rest, Req, State}. -spec allowed_methods(Req :: cowboy_req:req(), State :: state()) -> {Value :: [binary()], Req :: cowboy_req:req(), State :: state()}. @@ -423,7 +416,6 @@ valid_entity_length(Req, State) -> {true, Req, State}. %%%% - -type result_ok() :: { ok, {Status :: cowboy:http_status(), Headers :: cowboy:http_headers(), Body :: iodata()} @@ -431,7 +423,7 @@ valid_entity_length(Req, State) -> -type result_error() :: {error, Reason :: any()}. --type processed_response() :: {halt, cowboy_req:req(), state()}. +-type processed_response() :: {stop, cowboy_req:req(), state()}. -spec process_response(result_ok() | result_error(), cowboy_req:req(), state()) -> processed_response(). @@ -439,24 +431,23 @@ valid_entity_length(Req, State) -> process_response(Response, Req0, State = #state{operation_id = OperationID}) -> case Response of {ok, {Code, Headers, Body}} -> - {ok, Req} = cowboy_req:reply(Code, Headers, Body, Req0), - {halt, Req, State}; + Req = cowboy_req:reply(Code, Headers, Body, Req0), + {stop, Req, State}; {error, Message} -> error_logger:error_msg("Unable to process request for ~p: ~p", [OperationID, Message]), - {ok, Req} = cowboy_req:reply(400, Req0), - {halt, Req, State} + Req = cowboy_req:reply(400, Req0), + {stop, Req, State} end. --spec handle_request_json(cowboy_req:req(), state()) -> {halt, cowboy_req:req(), state()}. +-spec handle_request_json(cowboy_req:req(), state()) -> {cowboy_req:resp_body(), cowboy_req:req(), state()}. handle_request_json( Req0, State = #state{ operation_id = OperationID, logic_handler = LogicHandler, - validator_state = ValidatorState, - context = Context + validator_state = ValidatorState } ) -> case openapi_api:populate_request(OperationID, Req0, ValidatorState) of @@ -464,8 +455,8 @@ handle_request_json( {Code, Headers, Body} = openapi_logic_handler:handle_request( LogicHandler, OperationID, - Populated, - Context + Req1, + Populated ), _ = openapi_api:validate_response( OperationID, diff --git a/samples/server/petstore/erlang-server/src/openapi_server.erl b/samples/server/petstore/erlang-server/src/openapi_server.erl index f00f02cb077..45f6d1a39d5 100644 --- a/samples/server/petstore/erlang-server/src/openapi_server.erl +++ b/samples/server/petstore/erlang-server/src/openapi_server.erl @@ -1,30 +1,32 @@ -module(openapi_server). --define(DEFAULT_ACCEPTORS_POOLSIZE, 100). -define(DEFAULT_LOGIC_HANDLER, openapi_default_logic_handler). --export([child_spec/2]). +-export([start/2]). --spec child_spec( ID :: any(), #{ +-spec start( ID :: any(), #{ ip => inet:ip_address(), port => inet:port_number(), logic_handler => module(), net_opts => [] -}) -> supervisor:child_spec(). +}) -> {ok, pid()} | {error, any()}. -child_spec(ID, #{ +start(ID, #{ ip := IP , port := Port, net_opts := NetOpts } = Params) -> - AcceptorsPool = ?DEFAULT_ACCEPTORS_POOLSIZE, {Transport, TransportOpts} = get_socket_transport(IP, Port, NetOpts), LogicHandler = maps:get(logic_handler, Params, ?DEFAULT_LOGIC_HANDLER), ExtraOpts = maps:get(cowboy_extra_opts, Params, []), CowboyOpts = get_cowboy_config(LogicHandler, ExtraOpts), - ranch:child_spec({?MODULE, ID}, AcceptorsPool, - Transport, TransportOpts, cowboy_protocol, CowboyOpts). + case Transport of + ssl -> + cowboy:start_tls(ID, TransportOpts, CowboyOpts); + tcp -> + cowboy:start_clear(ID, TransportOpts, CowboyOpts) + end. get_socket_transport(IP, Port, Options) -> Opts = [ @@ -33,9 +35,9 @@ get_socket_transport(IP, Port, Options) -> ], case openapi_utils:get_opt(ssl, Options) of SslOpts = [_|_] -> - {ranch_ssl, Opts ++ SslOpts}; + {ssl, Opts ++ SslOpts}; undefined -> - {ranch_tcp, Opts} + {tcp, Opts} end. get_cowboy_config(LogicHandler, ExtraOpts) -> @@ -56,10 +58,10 @@ get_cowboy_config(LogicHandler, [{Key, Value}| Rest], Opts) -> get_default_dispatch(LogicHandler) -> Paths = openapi_router:get_paths(LogicHandler), - {dispatch, cowboy_router:compile(Paths)}. + #{dispatch => cowboy_router:compile(Paths)}. get_default_opts(LogicHandler) -> - [{env, [get_default_dispatch(LogicHandler)]}]. + #{env => get_default_dispatch(LogicHandler)}. store_key(Key, Value, Opts) -> lists:keystore(Key, 1, Opts, {Key, Value}). diff --git a/samples/server/petstore/erlang-server/src/openapi_store_handler.erl b/samples/server/petstore/erlang-server/src/openapi_store_handler.erl index 8b1ed1bf5cc..eb262f1c216 100644 --- a/samples/server/petstore/erlang-server/src/openapi_store_handler.erl +++ b/samples/server/petstore/erlang-server/src/openapi_store_handler.erl @@ -3,8 +3,7 @@ %% Cowboy REST callbacks -export([allowed_methods/2]). --export([init/3]). --export([rest_init/2]). +-export([init/2]). -export([allow_missing_post/2]). -export([content_types_accepted/2]). -export([content_types_provided/2]). @@ -27,17 +26,11 @@ -type state() :: state(). --spec init(TransportName :: atom(), Req :: cowboy_req:req(), Opts :: openapi_router:init_opts()) -> - {upgrade, protocol, cowboy_rest, Req :: cowboy_req:req(), Opts :: openapi_router:init_opts()}. +-spec init(Req :: cowboy_req:req(), Opts :: openapi_router:init_opts()) -> + {cowboy_rest, Req :: cowboy_req:req(), State :: state()}. -init(_Transport, Req, Opts) -> - {upgrade, protocol, cowboy_rest, Req, Opts}. - --spec rest_init(Req :: cowboy_req:req(), Opts :: openapi_router:init_opts()) -> - {ok, Req :: cowboy_req:req(), State :: state()}. - -rest_init(Req0, {Operations, LogicHandler, ValidatorState}) -> - {Method, Req} = cowboy_req:method(Req0), +init(Req, {Operations, LogicHandler, ValidatorState}) -> + Method = cowboy_req:method(Req), OperationID = maps:get(Method, Operations, undefined), error_logger:info_msg("Attempt to process operation: ~p", [OperationID]), @@ -47,7 +40,7 @@ rest_init(Req0, {Operations, LogicHandler, ValidatorState}) -> logic_handler = LogicHandler, validator_state = ValidatorState }, - {ok, Req, State}. + {cowboy_rest, Req, State}. -spec allowed_methods(Req :: cowboy_req:req(), State :: state()) -> {Value :: [binary()], Req :: cowboy_req:req(), State :: state()}. @@ -218,7 +211,6 @@ valid_entity_length(Req, State) -> {true, Req, State}. %%%% - -type result_ok() :: { ok, {Status :: cowboy:http_status(), Headers :: cowboy:http_headers(), Body :: iodata()} @@ -226,7 +218,7 @@ valid_entity_length(Req, State) -> -type result_error() :: {error, Reason :: any()}. --type processed_response() :: {halt, cowboy_req:req(), state()}. +-type processed_response() :: {stop, cowboy_req:req(), state()}. -spec process_response(result_ok() | result_error(), cowboy_req:req(), state()) -> processed_response(). @@ -234,24 +226,23 @@ valid_entity_length(Req, State) -> process_response(Response, Req0, State = #state{operation_id = OperationID}) -> case Response of {ok, {Code, Headers, Body}} -> - {ok, Req} = cowboy_req:reply(Code, Headers, Body, Req0), - {halt, Req, State}; + Req = cowboy_req:reply(Code, Headers, Body, Req0), + {stop, Req, State}; {error, Message} -> error_logger:error_msg("Unable to process request for ~p: ~p", [OperationID, Message]), - {ok, Req} = cowboy_req:reply(400, Req0), - {halt, Req, State} + Req = cowboy_req:reply(400, Req0), + {stop, Req, State} end. --spec handle_request_json(cowboy_req:req(), state()) -> {halt, cowboy_req:req(), state()}. +-spec handle_request_json(cowboy_req:req(), state()) -> {cowboy_req:resp_body(), cowboy_req:req(), state()}. handle_request_json( Req0, State = #state{ operation_id = OperationID, logic_handler = LogicHandler, - validator_state = ValidatorState, - context = Context + validator_state = ValidatorState } ) -> case openapi_api:populate_request(OperationID, Req0, ValidatorState) of @@ -259,8 +250,8 @@ handle_request_json( {Code, Headers, Body} = openapi_logic_handler:handle_request( LogicHandler, OperationID, - Populated, - Context + Req1, + Populated ), _ = openapi_api:validate_response( OperationID, diff --git a/samples/server/petstore/erlang-server/src/openapi_user_handler.erl b/samples/server/petstore/erlang-server/src/openapi_user_handler.erl index db0e900bf2a..c45a2a22651 100644 --- a/samples/server/petstore/erlang-server/src/openapi_user_handler.erl +++ b/samples/server/petstore/erlang-server/src/openapi_user_handler.erl @@ -3,8 +3,7 @@ %% Cowboy REST callbacks -export([allowed_methods/2]). --export([init/3]). --export([rest_init/2]). +-export([init/2]). -export([allow_missing_post/2]). -export([content_types_accepted/2]). -export([content_types_provided/2]). @@ -27,17 +26,11 @@ -type state() :: state(). --spec init(TransportName :: atom(), Req :: cowboy_req:req(), Opts :: openapi_router:init_opts()) -> - {upgrade, protocol, cowboy_rest, Req :: cowboy_req:req(), Opts :: openapi_router:init_opts()}. +-spec init(Req :: cowboy_req:req(), Opts :: openapi_router:init_opts()) -> + {cowboy_rest, Req :: cowboy_req:req(), State :: state()}. -init(_Transport, Req, Opts) -> - {upgrade, protocol, cowboy_rest, Req, Opts}. - --spec rest_init(Req :: cowboy_req:req(), Opts :: openapi_router:init_opts()) -> - {ok, Req :: cowboy_req:req(), State :: state()}. - -rest_init(Req0, {Operations, LogicHandler, ValidatorState}) -> - {Method, Req} = cowboy_req:method(Req0), +init(Req, {Operations, LogicHandler, ValidatorState}) -> + Method = cowboy_req:method(Req), OperationID = maps:get(Method, Operations, undefined), error_logger:info_msg("Attempt to process operation: ~p", [OperationID]), @@ -47,7 +40,7 @@ rest_init(Req0, {Operations, LogicHandler, ValidatorState}) -> logic_handler = LogicHandler, validator_state = ValidatorState }, - {ok, Req, State}. + {cowboy_rest, Req, State}. -spec allowed_methods(Req :: cowboy_req:req(), State :: state()) -> {Value :: [binary()], Req :: cowboy_req:req(), State :: state()}. @@ -271,7 +264,6 @@ valid_entity_length(Req, State) -> {true, Req, State}. %%%% - -type result_ok() :: { ok, {Status :: cowboy:http_status(), Headers :: cowboy:http_headers(), Body :: iodata()} @@ -279,7 +271,7 @@ valid_entity_length(Req, State) -> -type result_error() :: {error, Reason :: any()}. --type processed_response() :: {halt, cowboy_req:req(), state()}. +-type processed_response() :: {stop, cowboy_req:req(), state()}. -spec process_response(result_ok() | result_error(), cowboy_req:req(), state()) -> processed_response(). @@ -287,24 +279,23 @@ valid_entity_length(Req, State) -> process_response(Response, Req0, State = #state{operation_id = OperationID}) -> case Response of {ok, {Code, Headers, Body}} -> - {ok, Req} = cowboy_req:reply(Code, Headers, Body, Req0), - {halt, Req, State}; + Req = cowboy_req:reply(Code, Headers, Body, Req0), + {stop, Req, State}; {error, Message} -> error_logger:error_msg("Unable to process request for ~p: ~p", [OperationID, Message]), - {ok, Req} = cowboy_req:reply(400, Req0), - {halt, Req, State} + Req = cowboy_req:reply(400, Req0), + {stop, Req, State} end. --spec handle_request_json(cowboy_req:req(), state()) -> {halt, cowboy_req:req(), state()}. +-spec handle_request_json(cowboy_req:req(), state()) -> {cowboy_req:resp_body(), cowboy_req:req(), state()}. handle_request_json( Req0, State = #state{ operation_id = OperationID, logic_handler = LogicHandler, - validator_state = ValidatorState, - context = Context + validator_state = ValidatorState } ) -> case openapi_api:populate_request(OperationID, Req0, ValidatorState) of @@ -312,8 +303,8 @@ handle_request_json( {Code, Headers, Body} = openapi_logic_handler:handle_request( LogicHandler, OperationID, - Populated, - Context + Req1, + Populated ), _ = openapi_api:validate_response( OperationID,