openapi_spec_plus
description: A Dart library for parsing and working with OpenAPI Specifications (v2.0, v3.0, and v3.1).
Features
- Supports OpenAPI Specification versions 2.0, 3.0, and 3.1
Getting started
To use this package, you need Dart SDK installed. Then add the package to your pubspec.yaml
:
dependencies:
openapi_spec_plus: ^1.0.0
Run the following command to install the package:
dart pub get
Usage
Here are some simple examples for common use cases. For more examples, see the /example
folder.
Parse OpenAPI 3.1 JSON Spec
import 'dart:convert';
import 'dart:io';
import 'package:openapi_spec_plus/v31.dart' as v31;
void main() {
final file = File('test/fixtures/petstore_v31.json');
final content = file.readAsStringSync();
final spec = v31.OpenAPI.fromJson(jsonDecode(content));
print('API title: ${spec.info.title}');
}
Parse OpenAPI 3.1 YAML Spec
import 'dart:io';
import 'package:openapi_spec_plus/v31.dart' as v31;
import 'package:openapi_spec_plus/src/parser/yaml_utils.dart';
void main() {
final file = File('test/fixtures/openapi-v31.yaml');
final content = file.readAsStringSync();
final orgJson = yamlToMap(content);
final spec = v31.OpenAPI.fromJson(orgJson);
print('API title: ${spec.info.title}');
}
Parse OpenAPI 2.0 JSON Spec
import 'dart:convert';
import 'dart:io';
import 'package:openapi_spec_plus/v20.dart' as v20;
void main() {
final file = File('test/fixtures/petstore_v20.json');
final content = file.readAsStringSync();
final spec = v20.OpenAPI.fromJson(jsonDecode(content));
print('API title: ${spec.info.title}');
}
Parse OpenAPI 3.0 YAML Spec
import 'dart:io';
import 'package:openapi_spec_plus/v30.dart' as v30;
import 'package:openapi_spec_plus/src/parser/yaml_utils.dart';
void main() {
final file = File('test/fixtures/openapi-v30.yaml');
final content = file.readAsStringSync();
final orgJson = yamlToMap(content);
final spec = v30.OpenAPI.fromJson(orgJson);
print('API title: ${spec.info.title}');
}
Creating OpenAPI Spec Instances Manually
import 'package:openapi_spec_plus/v20.dart' as v20;
import 'package:openapi_spec_plus/v30.dart' as v30;
import 'package:openapi_spec_plus/v31.dart' as v31;
void main() {
const specV20 = v20.OpenAPI(
info: v20.Info(title: 'Test API', version: '1.0.0'),
);
const specV30 = v30.OpenAPI(
openapi: '3.0.4',
info: v30.Info(title: 'Test API', version: '1.0.0'),
);
const specV31 = v31.OpenAPI(
openapi: '3.1.0',
info: v31.Info(title: 'Test API', version: '1.0.0'),
);
print(specV20.toJson());
print(specV30.toJson());
print(specV31.toJson());
}
Contributing
Contributions are welcome! Feel free to open issues or submit pull requests on GitHub.
License
MIT License. See the LICENSE file for details.
Libraries
- extra
- v20
- A comprehensive library for working with OpenAPI 2.0 (Swagger) documents.
- v20/contact
- v20/discriminator
- v20/external_docs
- v20/header
- v20/info
- v20/license
- v20/media_type
- v20/openapi_spec
- v20/operation
- v20/parameter
- v20/parser
- v20/path
- v20/request_body
- v20/response
- v20/schema
- v20/security
- v20/server
- v20/tag
- v20/xml
- v30
- A comprehensive library for working with OpenAPI 3.0 (Swagger) documents.
- v30/callback
- v30/components
- v30/contact
- v30/discriminator
- v30/encoding
- v30/example
- v30/external_docs
- v30/header
- v30/info
- v30/license
- v30/link
- v30/media_type
- v30/openapi_spec
- v30/operation
- v30/parameter
- v30/parser
- v30/path
- v30/request_body
- v30/response
- v30/schema
- v30/security
- v30/server
- v30/tag
- v30/xml
- v31
- A comprehensive library for working with OpenAPI 3.1 (Swagger) documents.
- v31/callback
- v31/components
- v31/contact
- v31/discriminator
- v31/encoding
- v31/example
- v31/external_docs
- v31/header
- v31/info
- v31/license
- v31/link
- v31/media_type
- v31/openapi_spec
- v31/operation
- v31/parameter
- v31/parser
- v31/path
- v31/request_body
- v31/response
- v31/schema
- v31/security
- v31/server
- v31/tag
- v31/xml