openapi_code_builder 0.1.0

  • Readme
  • Changelog
  • Installing
  • new50

Dart OpenApi Code Genenrator #

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/

Flutter Screenshot

Usage #

  1. 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
    
  2. Create your schema file into your lib folder with the extension .openapi.yaml
  3. Optional: Add the base name to your schema
    openapi: 3.0.0
    info:
      x-dart-name: MyApiName
    
  4. Run the build_runner:
    (flutter) pub run build_runner build --delete-conflicting-outputs
    
  5. 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 ;-)'

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'));
  }
}

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();
}

Implement Client #

  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();

Try it out #

Run in openapi_dart/packages/openapi_code_builder/example

Server #

dart run bin/example_server.dart

Client #

dart run bin/example_client.dart

0.1.0 #

  • Initial version

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  openapi_code_builder: ^0.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:openapi_code_builder/openapi_code_builder.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
1
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Jul 14, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15

Analysis suggestions

Package not compatible with runtime flutter-web on Web

Because:

  • package:openapi_code_builder/openapi_code_builder.dart that imports:
  • package:openapi_code_builder/src/openapi_code_builder.dart that imports:
  • package:dart_style/dart_style.dart that imports:
  • package:dart_style/src/exceptions.dart that imports:
  • package:analyzer/error/error.dart that imports:
  • package:analyzer/src/generated/source.dart that imports:
  • package:analyzer/src/generated/source_io.dart that imports:
  • package:analyzer/src/generated/java_io.dart that imports:
  • dart:io

Package not compatible with runtime js

Because:

  • package:openapi_code_builder/openapi_code_builder.dart that imports:
  • package:openapi_code_builder/src/openapi_code_builder.dart that imports:
  • package:dart_style/dart_style.dart that imports:
  • package:dart_style/src/exceptions.dart that imports:
  • package:analyzer/error/error.dart that imports:
  • package:analyzer/src/generated/source.dart that imports:
  • package:analyzer/src/generated/source_io.dart that imports:
  • package:analyzer/src/generated/java_io.dart that imports:
  • dart:io

Maintenance suggestions

Maintain an example.

None of the files in the package's example/ directory matches known example patterns.

Common filename patterns include main.dart, example.dart, and openapi_code_builder.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.8.0 <3.0.0
build ^1.3.0 1.3.0
code_builder ^3.3.0 3.4.0
dart_style ^1.3.6 1.3.6
json_annotation ^3.0.1 3.0.1
logging ^0.11.4 0.11.4
meta ^1.1.8 1.2.2 1.3.0-nullsafety
open_api ^2.0.1 2.0.1
openapi_base ^0.1.0 0.1.2
path ^1.7.0 1.7.0
quiver ^2.1.3 2.1.3
recase ^3.0.0 3.0.0
source_gen ^0.9.5 0.9.6
yaml ^2.2.1 2.2.1
Transitive dependencies
_fe_analyzer_shared 5.0.0
analyzer 0.39.13
args 1.6.0
async 2.4.2
built_collection 4.3.2
built_value 7.1.0
charcode 1.1.3
clock 1.0.1
codable 1.0.0
collection 1.14.13 1.15.0-nullsafety
convert 2.1.1
crypto 2.1.5
csslib 0.16.1
dio 3.0.9
fixnum 0.10.11
glob 1.2.0
html 0.14.0+3
http 0.12.1
http_parser 3.1.4
intl 0.16.1
io 0.3.4
js 0.6.2
logging_appenders 0.4.2+3
matcher 0.12.9
node_interop 1.1.1
node_io 1.1.1
package_config 1.9.3
pub_semver 1.4.4
rxdart 0.24.1
shelf 0.7.7
shelf_cookie 1.1.1
source_span 1.7.0
stack_trace 1.9.5
stream_channel 2.0.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.2.0 1.3.0-nullsafety
uri 0.11.3+1
utf 0.9.0+5
watcher 0.9.7+15
Dev dependencies
pedantic ^1.9.0 1.9.2