OpenApi/Swagger - What is the difference between Client and Server?

我与影子孤独终老i 提交于 2019-12-24 03:28:13

问题


I've built a RESTful service exposing an endpoint using OpenApi 3.0 using yaml

When I build this, it auto-generates a client directory and a server directory.

I am successfully leveraging the server API as an endpoint which consumes requests.

However the client API is generated from exactly the same yaml...accepting the same argument type and returning the same type as the server.

My understanding was a client produces requests to an external service, while a server consumes requests from an external service.

If this is correct, why does OpenApi define Client and Server identically, and provide no means to configure them individually?


回答1:


You are correct:

My understanding was a client produces requests to an external service, while a server consumes requests from an external service.

The are basically an example implementation of your spec.

They are both built from the same OpenAPI contract, and the client can call the server and it will work as they agree on the various APIs and the data schemas described in the spec.

The client is a library you could use in the application that consumes the API, in that you can use it as is make requests to the API described by the OpenAPI definition.

The server is simply an example server application that provides the HTTP interface described in the the OpenAPI.

Neither are super useful by themselves as the client just calls the APIs in the right way and the server will simply expose the endpoint and do things like serialise/deserialise any request or response data. The implementation of the API is left up to you..

Typically the useful bits I use from generated specs are the model classes, as this helps ensure that my application code (whether a client or a server) is based on the correct schema.



来源:https://stackoverflow.com/questions/58652317/openapi-swagger-what-is-the-difference-between-client-and-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!