notAChance
notAChance

Reputation: 1430

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

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?

Upvotes: 4

Views: 3942

Answers (1)

stringy05
stringy05

Reputation: 7067

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 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.

Upvotes: 5

Related Questions