Skip to content

Commit

Permalink
Merge pull request #107 from gibahjoe/upgrade-5.0-start
Browse files Browse the repository at this point in the history
feat: update openapi jar to 7.0
  • Loading branch information
gibahjoe authored Jan 16, 2024
2 parents 23d7594 + 5d6407a commit 9eafc8e
Show file tree
Hide file tree
Showing 25 changed files with 385 additions and 570 deletions.
41 changes: 19 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,15 @@ Annotate a dart class with @Openapi() annotation
```dart
@Openapi(
additionalProperties:
AdditionalProperties(pubName: 'petstore_api', pubAuthor: 'Johnny dep'),
inputSpecFile: 'example/openapi-spec.yaml',
generatorName: Generator.dart,
outputDirectory: 'api/petstore_api')
class Example {}
additionalProperties:
DioProperties(pubName: 'petstore_api', pubAuthor: 'Johnny dep..'),
inputSpec:
RemoteSpec(path: 'https://petstore3.swagger.io/api/v3/openapi.json'),
typeMappings: {'Pet': 'ExamplePet'},
generatorName: Generator.dio,
runSourceGenOnOutput: true,
outputDirectory: 'api/petstore_api',
)
```

Run
Expand All @@ -99,7 +102,8 @@ The api sdk will be generated in the folder specified in the annotation. See exa

## Next Generation

There is some new functionality slated to be added to the generator. This version will have the ability to:
As of version 5.0 of this library, there is some new functionality slated to be added to the generator. This version
will have the ability to:

- cache changes in the OAS spec
- Rerun when there ares difference in the cached copy and current copy
Expand All @@ -118,25 +122,18 @@ New:

```dart
@Openapi(
additionalProperties:
AdditionalProperties(pubName: 'petstore_api', pubAuthor: 'Johnny dep'),
inputSpecFile: 'example/openapi-spec.yaml',
generatorName: Generator.dart,
outputDirectory: 'api/petstore_api',
cachePath: 'some/preferred/directory/cache.json',
useNextGen: true
additionalProperties:
DioProperties(pubName: 'petstore_api', pubAuthor: 'Johnny dep..'),
inputSpec:
RemoteSpec(path: 'https://petstore3.swagger.io/api/v3/openapi.json'),
typeMappings: {'Pet': 'ExamplePet'},
generatorName: Generator.dio,
runSourceGenOnOutput: true,
outputDirectory: 'api/petstore_api',
)
class Example {}
```

**IMPORTANT** With the new changes comes 2 new annotation properties:

- useNextGen (boolean)
- Default: `false`
- cachePath (String)
- Default: `.dart_tool/openapi-generator-cache.json`
- Must be a path to a `json` file.
- Can only be set when `useNextGen` is `true`

## Contributing

Expand Down
2 changes: 1 addition & 1 deletion example/api/petstore_api/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ environment:
sdk: '>=2.15.0 <3.0.0'

dependencies:
dio: '^5.0.0'
dio: '^5.2.0'
one_of: '>=1.5.0 <2.0.0'
one_of_serializer: '>=1.5.0 <2.0.0'
built_value: '>=8.4.0 <9.0.0'
Expand Down
8 changes: 4 additions & 4 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Openapi Generator last run: : 2023-08-20T13:11:18.157121
// Openapi Generator last run: : 2024-01-16T01:49:21.940229
import 'package:flutter/material.dart';
import 'package:openapi_generator_annotations/openapi_generator_annotations.dart';

Expand All @@ -9,11 +9,11 @@ void main() {
@Openapi(
additionalProperties:
DioProperties(pubName: 'petstore_api', pubAuthor: 'Johnny dep..'),
inputSpecFile: 'openapi-spec.yaml',
inputSpec:
RemoteSpec(path: 'https://petstore3.swagger.io/api/v3/openapi.json'),
typeMappings: {'Pet': 'ExamplePet'},
generatorName: Generator.dio,
runSourceGenOnOutput: true,
useNextGen: true,
outputDirectory: 'api/petstore_api',
)
class MyApp extends StatelessWidget {
Expand Down Expand Up @@ -124,4 +124,4 @@ class _MyHomePageState extends State<MyHomePage> {
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
}
2 changes: 1 addition & 1 deletion openapi-generator-annotations/example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:openapi_generator_annotations/openapi_generator_annotations.dart
@Openapi(
additionalProperties:
AdditionalProperties(pubName: 'petstore_api', pubAuthor: 'Johnny dep'),
inputSpecFile: 'example/openapi-spec.yaml',
inputSpec: InputSpec(path: 'example/openapi-spec.yaml'),
generatorName: Generator.dio,
outputDirectory: 'api/petstore_api',
// useNextGen: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import 'dart:io';
import 'package:crypto/crypto.dart';
import 'package:meta/meta.dart';

/// Config base class
/// Your annotated class must extend this config class
@Deprecated(
'You do not need to extend this anymore (See example). This class would be removed in the next major release.')
abstract class OpenapiGeneratorConfig {}

class Openapi {
/// Additional properties to pass to the compiler (CSV)
///
Expand All @@ -26,12 +20,6 @@ class Openapi {
/// --api-package
final String? apiPackage;

/// relative path or url to spec file
///
/// -i
@Deprecated('To be removed in the next major')
final String inputSpecFile;

/// Provides the access information to the input spec file.
///
/// For use with useNextGen.
Expand All @@ -43,7 +31,7 @@ class Openapi {
/// to be applied to the fetch request when the spec file is in a remote
/// location. There is also special handling for when the spec file lives within
/// AWS.
final InputSpec? inputSpec;
final InputSpec inputSpec;

/// folder containing the template files
///
Expand Down Expand Up @@ -80,12 +68,6 @@ class Openapi {
/// --reserved-words-mappings
final Map<String, String>? reservedWordsMappings;

/// Tells openapi-generator to always run during the build process
/// if set to false (the default), openapi-generator will skip processing if the [outputDirectory] already exists
@Deprecated(
'The generator will always run to determine if there are changes made on the input spec file')
final bool? alwaysRun;

/// if set to true, flutter pub get will be run on the [outputDirectory] after the code has been generated.
/// Defaults to true for backwards compatibility
final bool? fetchDependencies;
Expand Down Expand Up @@ -122,21 +104,6 @@ class Openapi {
/// e.g {'inline_object_2': 'SomethingMapped'}
final Map<String, String>? inlineSchemaNameMappings;

/// Use the next generation of the generator.
///
/// This annotation informs the generator to use the new generator pathway.
/// Enabling this option allows for incremental changes to the [inputSpecFile]
/// to be generated even though the annotation was unchanged.
///
/// Due to some limitations with build_runner and it only running when the
/// asset graph has changed, a generated line get injected at the beginning of
/// the file at the end of each run.
///
/// This will become the default behaviour in the next Major version (v5).
///
/// Default: false
final bool useNextGen;

/// The path where to store the cached copy of the specification.
///
/// For use with [useNextGen].
Expand All @@ -152,8 +119,7 @@ class Openapi {
this.additionalProperties,
this.overwriteExistingFiles,
this.skipSpecValidation = false,
required this.inputSpecFile,
this.inputSpec,
required this.inputSpec,
this.templateDirectory,
required this.generatorName,
this.outputDirectory,
Expand All @@ -165,9 +131,7 @@ class Openapi {
this.apiPackage,
this.fetchDependencies = true,
this.runSourceGenOnOutput = true,
this.alwaysRun = false,
this.cachePath,
this.useNextGen = false,
this.projectPubspecPath,
this.debugLogging = false,
});
Expand All @@ -179,30 +143,18 @@ class Openapi {
/// Includes the option to use the default json or yaml paths.
class InputSpec {
final String path;
final bool defaultYaml;
final bool useYml;

const InputSpec({String? path, this.defaultYaml = true, this.useYml = false})
: path = path ??
'openapi.${defaultYaml ? 'y${useYml ? '' : 'a'}ml' : 'json'}';
const InputSpec({required this.path});

const InputSpec.empty() : this();
const InputSpec.json() : this(path: 'openapi.json');

const InputSpec.emptyJson() : this(defaultYaml: false);
const InputSpec.emptyYml() : this(useYml: true);
const InputSpec.yaml({bool shortExtension = false})
: this(path: 'openapi.y${shortExtension ? '' : 'a'}ml');

Map<String, dynamic> toJsonMap() => {
'path': path,
'defaultYaml': defaultYaml,
'useYml': useYml,
};
Map<String, dynamic> toJsonMap() => {'path': path};

InputSpec.fromMap(Map<String, dynamic> map)
: this(
path: map['path'],
defaultYaml: map['defaultYaml'] == 'true' ? true : false,
useYml: map['useYml'] == 'true' ? true : false,
);
: this(path: map['path']);
}

/// Provides the location for the remote specification.
Expand Down Expand Up @@ -377,10 +329,13 @@ class AdditionalProperties {
/// Allow the 'x-enum-values' extension for enums
final bool? useEnumExtension;

/// With this option enabled, each enum will have a new case, 'unknown_default_open_api',
/// If the server adds new enum cases, that are unknown by an old spec/client,
/// the client will fail to parse the network response.With this option enabled, each enum will have a new case, 'unknown_default_open_api',
/// so that when the server sends an enum case that is not known by the client/spec,
/// they can safely fallback to this case
final bool? enumUnknownDefaultCase;
/// they can safely fallback to this case.
///
/// Default: false
final bool enumUnknownDefaultCase;

/// Flutter wrapper to use (none|flutterw|fvm)
final Wrapper wrapper;
Expand All @@ -404,7 +359,7 @@ class AdditionalProperties {
this.allowUnicodeIdentifiers = false,
this.ensureUniqueParams = true,
this.useEnumExtension = false,
this.enumUnknownDefaultCase = true,
this.enumUnknownDefaultCase = false,
this.prependFormOrBodyParameters = false,
this.pubAuthor,
this.pubAuthorEmail,
Expand All @@ -425,7 +380,7 @@ class AdditionalProperties {
allowUnicodeIdentifiers: map['allowUnicodeIdentifiers'] ?? false,
ensureUniqueParams: map['ensureUniqueParams'] ?? true,
useEnumExtension: map['useEnumExtension'] ?? true,
enumUnknownDefaultCase: map['enumUnknownDefaultCase'] ?? true,
enumUnknownDefaultCase: map['enumUnknownDefaultCase'] ?? false,
prependFormOrBodyParameters:
map['prependFormOrBodyParameters'] ?? false,
pubAuthor: map['pubAuthor'],
Expand Down Expand Up @@ -532,7 +487,7 @@ class DioProperties extends AdditionalProperties {
bool sortModelPropertiesByRequiredFlag = true,
bool sortParamsByRequiredFlag = true,
bool useEnumExtension = true,
bool enumUnknownDefaultCase = true,
bool enumUnknownDefaultCase = false,
String? sourceFolder,
Wrapper wrapper = Wrapper.none})
: super(
Expand Down Expand Up @@ -609,7 +564,7 @@ class DioAltProperties extends AdditionalProperties {
bool sortModelPropertiesByRequiredFlag = true,
bool sortParamsByRequiredFlag = true,
bool useEnumExtension = true,
bool enumUnknownDefaultCase = true,
bool enumUnknownDefaultCase = false,
String? sourceFolder,
Wrapper wrapper = Wrapper.none})
: super(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ void main() {
group('OpenApi', () {
test('defaults', () {
final props = Openapi(
inputSpecFile: InputSpec.empty().path,
inputSpec: InputSpec.empty(),
inputSpec: InputSpec.json(),
generatorName: Generator.dart,
);
expect(props.additionalProperties, isNull);
expect(props.overwriteExistingFiles, isNull);
expect(props.skipSpecValidation, false);
expect(props.inputSpecFile, InputSpec.empty().path);
expect(props.inputSpec!.path, InputSpec.empty().path);
expect(props.inputSpec!.path, InputSpec.json().path);
expect(props.templateDirectory, isNull);
expect(props.generatorName, Generator.dart);
expect(props.outputDirectory, isNull);
Expand All @@ -28,58 +26,44 @@ void main() {
expect(props.apiPackage, isNull);
expect(props.fetchDependencies, true);
expect(props.runSourceGenOnOutput, true);
expect(props.alwaysRun, false);
expect(props.cachePath, isNull);
expect(props.useNextGen, false);
expect(props.projectPubspecPath, isNull);
expect(props.debugLogging, isFalse);
});
group('NextGen', () {
test('Sets cachePath', () {
final api = Openapi(
inputSpecFile: InputSpec.empty().path,
inputSpec: InputSpec.json(),
generatorName: Generator.dart,
cachePath: 'somePath');
expect(api.cachePath, 'somePath');
});
test('Sets useNextGenFlag', () {
final api = Openapi(
inputSpecFile: InputSpec.empty().path,
generatorName: Generator.dart,
useNextGen: true);
expect(api.useNextGen, isTrue);
});
test('Sets projectPubspecPath', () {
final api = Openapi(
inputSpecFile: InputSpec.empty().path,
inputSpec: InputSpec.json(),
generatorName: Generator.dart,
projectPubspecPath: 'test');
expect(api.projectPubspecPath, 'test');
});
test('Set debug logging', () {
final api = Openapi(
inputSpecFile: InputSpec.empty().path,
inputSpec: InputSpec.empty(),
inputSpec: InputSpec.json(),
generatorName: Generator.dart,
debugLogging: true);
expect(api.debugLogging, isTrue);
});
group('InputSpec', () {
group('local spec', () {
test('provides default yaml path', () {
expect(InputSpec.empty().path, 'openapi.yaml');
expect(InputSpec.empty().defaultYaml, isTrue);
expect(InputSpec.empty().useYml, isFalse);
expect(InputSpec.yaml().path, 'openapi.yaml');
expect(InputSpec.yaml(shortExtension: true).path, 'openapi.yml');
});
test('provides default yml path', () {
expect(InputSpec.emptyYml().path, 'openapi.yml');
expect(InputSpec.emptyYml().defaultYaml, isTrue);
expect(InputSpec.emptyYml().useYml, isTrue);
expect(InputSpec.yaml(shortExtension: true).path, 'openapi.yml');
expect(InputSpec.yaml(shortExtension: false).path, 'openapi.yaml');
});
test('provides default json path', () {
expect(InputSpec.emptyJson().path, 'openapi.json');
expect(InputSpec.emptyJson().defaultYaml, isFalse);
expect(InputSpec.emptyJson().useYml, isFalse);
expect(InputSpec.json().path, 'openapi.json');
});
test('uses path', () {
expect(InputSpec(path: 'path').path, 'path');
Expand Down
Binary file modified openapi-generator-cli/lib/openapi-generator.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion openapi-generator-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-cli</artifactId>
<version>6.6.0</version>
<version>7.2.0</version>
</dependency>

<dependency>
Expand Down
4 changes: 2 additions & 2 deletions openapi-generator/example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import 'package:openapi_generator_annotations/openapi_generator_annotations.dart
@Openapi(
additionalProperties:
AdditionalProperties(pubName: 'petstore_api', pubAuthor: 'Johnny dep...'),
inputSpecFile: 'https://google.com',
inputSpec: InputSpec(path: 'openapi-spec.yaml'),
generatorName: Generator.dio,
outputDirectory: 'api/petstore_api',
)
class Example extends OpenapiGeneratorConfig {}
class Example {}
Loading

0 comments on commit 9eafc8e

Please sign in to comment.