openapi_code_builder

Last updated:

0 purchases

openapi_code_builder Image
openapi_code_builder Images
Add to Cart

Description:

openapi code builder

Dart OpenApi Code Generator #
openapi_code_builder generates server stubs and client libraries for open api schema yaml files.
This is a build_runner library meant to be included in the
dev_dependencies of your project to allow generating of
dart source files for client and server stubs for
OpenAPI 3.0 schema files (Only yaml is supported right now).
See directory for an example usage.
You can also try out the code generator
right inside your browser: https://hpoul.github.io/openapi_dart/

Real world example #
See the backend for AuthPass which uses auto generated
openapi basically as http server. Yaml file available on github.
Usage #

Update pubspec.yaml:
dependencies:
json_annotation: ^3.0.1
openapi_base: any

dev_dependencies:
openapi_code_builder: any
json_serializable: ^3.3.0
build_runner: ^1.10.0
copied to clipboard

Create your schema file into your lib folder
with the extension .openapi.yaml
Optional: Add the base name to your schema
openapi: 3.0.0
info:
x-dart-name: MyApiName
copied to clipboard

Run the build_runner:
(flutter) pub run build_runner build --delete-conflicting-outputs
copied to clipboard

Implement the server and client. (see below)

Example schema #
openapi: 3.0.0
info:
version: 0.1.0
title: Example API
x-dart-name: TestApi

paths:
/hello/{name}:
parameters:
- name: name
in: path
required: true
schema:
type: string
get:
summary: Say Hello World to {name}
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/HelloResponse'
components:
schemas:
HelloResponse:
properties:
message:
type: string
description: 'The Hello World greeting ;-)'

copied to clipboard
Implement Server #
class TestApiImpl extends TestApi {
@override
Future<HelloNameGetResponse> helloNameGet({String name}) async {
_logger.info('Saying hi to $name');
return HelloNameGetResponse.response200(
HelloResponse(message: 'Hello $name'));
}
}
copied to clipboard
Create a server and bind it to a port #
Future<void> main() async {
PrintAppender.setupLogging();
_logger.fine('Starting Server ...');
final server = OpenApiShelfServer(
TestApiRouter(ApiEndpointProvider.static(TestApiImpl())),
);
server.startServer();
}
copied to clipboard
Implement Client #
Future<void> main() async {
final requestSender = HttpRequestSender();
final client = TestApiClient(
Uri.parse('http://localhost:8000'),
requestSender);
final blubb = await client.helloNameGet(name: 'Blubb');
blubb.map(
on200: (response) => _logger.info('Success: ****${response.body.message}'),
);
_logger.info('Response: $blubb');
requestSender.dispose();
}
copied to clipboard
Try it out #
Run in openapi_dart/packages/openapi_code_builder/example
Server #
dart run bin/example_server.dart
copied to clipboard

Client #
dart run bin/example_client.dart
copied to clipboard

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.