Skip to content

Commit

Permalink
feat: add config option to know if schema is using x-nullable extensi…
Browse files Browse the repository at this point in the history
…on (#295)
  • Loading branch information
MohiuddinM authored Jan 27, 2025
1 parent 8167285 commit eaed0ea
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
10 changes: 10 additions & 0 deletions swagger_parser/lib/src/config/swp_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class SWPConfig {
this.enumsParentPrefix = true,
this.skippedParameters = const <String>[],
this.generateValidator = false,
this.useXNullable = false,
});

/// Internal constructor of [SWPConfig]
Expand Down Expand Up @@ -62,6 +63,7 @@ class SWPConfig {
required this.enumsParentPrefix,
required this.skippedParameters,
required this.generateValidator,
required this.useXNullable,
});

/// Creates a [SWPConfig] from [YamlMap].
Expand Down Expand Up @@ -204,6 +206,9 @@ class SWPConfig {
final generateValidator =
yamlMap['generate_validator'] as bool? ?? rootConfig?.generateValidator;

final useXNullable =
yamlMap['use_x_nullable'] as bool? ?? rootConfig?.useXNullable;

// Default config
final dc = SWPConfig(name: name, outputDirectory: outputDirectory);

Expand Down Expand Up @@ -234,6 +239,7 @@ class SWPConfig {
originalHttpResponse: originalHttpResponse ?? dc.originalHttpResponse,
replacementRules: replacementRules ?? dc.replacementRules,
generateValidator: generateValidator ?? dc.generateValidator,
useXNullable: useXNullable ?? dc.useXNullable,
);
}

Expand Down Expand Up @@ -340,6 +346,9 @@ class SWPConfig {
/// Set `true` to generate validator for freezed.
final bool generateValidator;

/// Set `true` if Schema uses x-nullable to indicate nullable fields
final bool useXNullable;

/// Convert [SWPConfig] to [GeneratorConfig]
GeneratorConfig toGeneratorConfig() {
return GeneratorConfig(
Expand Down Expand Up @@ -379,6 +388,7 @@ class SWPConfig {
enumsParentPrefix: enumsParentPrefix,
skippedParameters: skippedParameters,
replacementRules: replacementRules,
useXNullable: useXNullable,
);
}
}
5 changes: 5 additions & 0 deletions swagger_parser/lib/src/parser/config/parser_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ParserConfig {
this.enumsParentPrefix = true,
this.skippedParameters = const <String>[],
this.replacementRules = const [],
this.useXNullable = false,
});

/// Specification file content as [String]
Expand Down Expand Up @@ -49,4 +50,8 @@ class ParserConfig {
/// Optional. Set regex replacement rules for the names of the generated classes/enums.
/// All rules are applied in order.
final List<ReplacementRule> replacementRules;

/// Does the Schema use x-nullable to indicate nullable fields
/// Only used for OpenApi v2
final bool useXNullable;
}
4 changes: 3 additions & 1 deletion swagger_parser/lib/src/parser/parser/open_api_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,9 @@ class OpenApiParser {
propertyValue,
name: propertyName,
additionalName: additionalName,
isRequired: isRequired || !isNullable,
isRequired: (_apiInfo.schemaVersion == OAS.v2 && !config.useXNullable)
? isRequired
: isRequired || !isNullable,
);

var validation = propertyValue;
Expand Down
8 changes: 8 additions & 0 deletions swagger_parser/test/e2e/e2e_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ void main() {
schemaPath: schemaPath,
jsonSerializer: JsonSerializer.freezed,
putClientsInFolder: true,
useXNullable: true,
),
schemaFileName: 'swagger.yaml',
);
Expand All @@ -115,6 +116,7 @@ void main() {
schemaPath: schemaPath,
jsonSerializer: JsonSerializer.freezed,
putClientsInFolder: true,
useXNullable: true,
),
schemaFileName: 'additional_properties_class.2.0.json',
);
Expand All @@ -141,6 +143,7 @@ void main() {
schemaPath: schemaPath,
jsonSerializer: JsonSerializer.freezed,
putClientsInFolder: true,
useXNullable: true,
),
schemaFileName: 'basic_requests.2.0.json',
);
Expand All @@ -167,6 +170,7 @@ void main() {
schemaPath: schemaPath,
jsonSerializer: JsonSerializer.freezed,
putClientsInFolder: true,
useXNullable: true,
),
schemaFileName: 'basic_types_class.2.0.json',
);
Expand Down Expand Up @@ -206,6 +210,7 @@ void main() {
schemaPath: schemaPath,
jsonSerializer: JsonSerializer.freezed,
putClientsInFolder: true,
useXNullable: true,
),
schemaFileName: 'empty_class.2.0.json',
);
Expand Down Expand Up @@ -258,6 +263,7 @@ void main() {
schemaPath: schemaPath,
jsonSerializer: JsonSerializer.freezed,
putClientsInFolder: true,
useXNullable: true,
),
schemaFileName: 'reference_types_class.2.0.json',
);
Expand All @@ -283,6 +289,7 @@ void main() {
outputDirectory: outputDirectory,
schemaPath: schemaPath,
putClientsInFolder: true,
useXNullable: true,
replacementRules: [
ReplacementRule(pattern: RegExp('List'), replacement: 'Lizt'),
ReplacementRule(pattern: RegExp(r'$'), replacement: 'DTO'),
Expand Down Expand Up @@ -317,6 +324,7 @@ void main() {
schemaPath: schemaPath,
jsonSerializer: JsonSerializer.freezed,
putClientsInFolder: true,
useXNullable: true,
),
schemaFileName: 'wrapping_collections.2.0.json',
);
Expand Down

0 comments on commit eaed0ea

Please sign in to comment.