[dart][core] Fix reservedWordsMappings (#12436)

* [core] Fix naming of reservedWordsMappings 

* `GeneratorSettings` used a wrong name (missing `s`) which resulted in config loaded from YAMl files not working

* [dart] Respect reservedWordsMappings when checking for reserved words
This commit is contained in:
Peter Leibiger 2022-05-25 05:56:51 +02:00 committed by GitHub
parent 45cbd5f7a7
commit 111675b89b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 55 additions and 48 deletions

View File

@ -9,3 +9,5 @@ typeMappings:
additionalProperties:
hideGenerationTimestamp: "true"
enumUnknownDefaultCase: "true"
reservedWordsMappings:
class: "classField"

View File

@ -52,7 +52,7 @@ public final class GeneratorSettings implements Serializable {
private final Map<String, String> importMappings;
private final Map<String, String> inlineSchemaNameMappings;
private final Set<String> languageSpecificPrimitives;
private final Map<String, String> reservedWordMappings;
private final Map<String, String> reservedWordsMappings;
private final Map<String, String> serverVariables;
private String gitHost;
@ -268,8 +268,8 @@ public final class GeneratorSettings implements Serializable {
*
* @return the reserved word mappings
*/
public Map<String, String> getReservedWordMappings() {
return reservedWordMappings;
public Map<String, String> getReservedWordsMappings() {
return reservedWordsMappings;
}
@ -361,7 +361,7 @@ public final class GeneratorSettings implements Serializable {
importMappings = Collections.unmodifiableMap(builder.importMappings);
inlineSchemaNameMappings = Collections.unmodifiableMap(builder.inlineSchemaNameMappings);
languageSpecificPrimitives = Collections.unmodifiableSet(builder.languageSpecificPrimitives);
reservedWordMappings = Collections.unmodifiableMap(builder.reservedWordMappings);
reservedWordsMappings = Collections.unmodifiableMap(builder.reservedWordsMappings);
serverVariables = Collections.unmodifiableMap(builder.serverVariables);
gitHost = builder.gitHost;
gitUserId = builder.gitUserId;
@ -432,7 +432,7 @@ public final class GeneratorSettings implements Serializable {
importMappings = Collections.unmodifiableMap(new HashMap<>(0));
inlineSchemaNameMappings = Collections.unmodifiableMap(new HashMap<>(0));
languageSpecificPrimitives = Collections.unmodifiableSet(new HashSet<>(0));
reservedWordMappings = Collections.unmodifiableMap(new HashMap<>(0));
reservedWordsMappings = Collections.unmodifiableMap(new HashMap<>(0));
serverVariables = Collections.unmodifiableMap(new HashMap<>(0));
}
@ -488,8 +488,8 @@ public final class GeneratorSettings implements Serializable {
if (copy.getLanguageSpecificPrimitives() != null) {
builder.languageSpecificPrimitives.addAll(copy.getLanguageSpecificPrimitives());
}
if (copy.getReservedWordMappings() != null) {
builder.reservedWordMappings.putAll(copy.getReservedWordMappings());
if (copy.getReservedWordsMappings() != null) {
builder.reservedWordsMappings.putAll(copy.getReservedWordsMappings());
}
if (copy.getServerVariables() != null) {
builder.serverVariables.putAll(copy.getServerVariables());
@ -526,7 +526,7 @@ public final class GeneratorSettings implements Serializable {
private Map<String, String> importMappings;
private Map<String, String> inlineSchemaNameMappings;
private Set<String> languageSpecificPrimitives;
private Map<String, String> reservedWordMappings;
private Map<String, String> reservedWordsMappings;
private Map<String, String> serverVariables;
private String gitHost;
private String gitUserId;
@ -544,7 +544,7 @@ public final class GeneratorSettings implements Serializable {
importMappings = new HashMap<>();
inlineSchemaNameMappings = new HashMap<>();
languageSpecificPrimitives = new HashSet<>();
reservedWordMappings = new HashMap<>();
reservedWordsMappings = new HashMap<>();
serverVariables = new HashMap<>();
gitHost = DEFAULT_GIT_HOST;
@ -837,28 +837,28 @@ public final class GeneratorSettings implements Serializable {
}
/**
* Sets the {@code reservedWordMappings} and returns a reference to this Builder so that the methods can be chained together.
* Sets the {@code reservedWordsMappings} and returns a reference to this Builder so that the methods can be chained together.
*
* @param reservedWordMappings the {@code reservedWordMappings} to set
* @param reservedWordsMappings the {@code reservedWordsMappings} to set
* @return a reference to this Builder
*/
public Builder withReservedWordMappings(Map<String, String> reservedWordMappings) {
this.reservedWordMappings = reservedWordMappings;
public Builder withReservedWordsMappings(Map<String, String> reservedWordsMappings) {
this.reservedWordsMappings = reservedWordsMappings;
return this;
}
/**
* Sets a single {@code reservedWordMappings} and returns a reference to this Builder so that the methods can be chained together.
* Sets a single {@code reservedWordsMappings} and returns a reference to this Builder so that the methods can be chained together.
*
* @param key A key for some reserved word mapping
* @param value The value of some reserved word mapping
* @return a reference to this Builder
*/
public Builder withReservedWordMapping(String key, String value) {
if (this.reservedWordMappings == null) {
this.reservedWordMappings = new HashMap<>();
if (this.reservedWordsMappings == null) {
this.reservedWordsMappings = new HashMap<>();
}
this.reservedWordMappings.put(key, value);
this.reservedWordsMappings.put(key, value);
return this;
}
@ -966,7 +966,7 @@ public final class GeneratorSettings implements Serializable {
", additionalProperties=" + additionalProperties +
", importMappings=" + importMappings +
", languageSpecificPrimitives=" + languageSpecificPrimitives +
", reservedWordMappings=" + reservedWordMappings +
", reservedWordsMappings=" + reservedWordsMappings +
", gitHost='" + gitHost + '\'' +
", gitUserId='" + gitUserId + '\'' +
", gitRepoId='" + gitRepoId + '\'' +
@ -998,7 +998,7 @@ public final class GeneratorSettings implements Serializable {
Objects.equals(getImportMappings(), that.getImportMappings()) &&
Objects.equals(getInlineSchemaNameMappings(), that.getInlineSchemaNameMappings()) &&
Objects.equals(getLanguageSpecificPrimitives(), that.getLanguageSpecificPrimitives()) &&
Objects.equals(getReservedWordMappings(), that.getReservedWordMappings()) &&
Objects.equals(getReservedWordsMappings(), that.getReservedWordsMappings()) &&
Objects.equals(getGitHost(), that.getGitHost()) &&
Objects.equals(getGitUserId(), that.getGitUserId()) &&
Objects.equals(getGitRepoId(), that.getGitRepoId()) &&
@ -1027,7 +1027,7 @@ public final class GeneratorSettings implements Serializable {
getImportMappings(),
getInlineSchemaNameMappings(),
getLanguageSpecificPrimitives(),
getReservedWordMappings(),
getReservedWordsMappings(),
getGitHost(),
getGitUserId(),
getGitRepoId(),

View File

@ -331,7 +331,7 @@ public class CodeGenMojo extends AbstractMojo {
/**
* A map of reserved names and how they should be escaped
*/
@Parameter(name = "reservedWordsMappings", property = "openapi.generator.maven.plugin.reservedWordMappings")
@Parameter(name = "reservedWordsMappings", property = "openapi.generator.maven.plugin.reservedWordsMappings")
private List<String> reservedWordsMappings;
/**

View File

@ -70,7 +70,7 @@ public class CodegenConfigurator {
private Map<String, String> importMappings = new HashMap<>();
private Map<String, String> inlineSchemaNameMappings = new HashMap<>();
private Set<String> languageSpecificPrimitives = new HashSet<>();
private Map<String, String> reservedWordMappings = new HashMap<>();
private Map<String, String> reservedWordsMappings = new HashMap<>();
private Map<String, String> serverVariables = new HashMap<>();
private String auth;
@ -118,8 +118,8 @@ public class CodegenConfigurator {
if(generatorSettings.getLanguageSpecificPrimitives() != null) {
configurator.languageSpecificPrimitives.addAll(generatorSettings.getLanguageSpecificPrimitives());
}
if(generatorSettings.getReservedWordMappings() != null) {
configurator.reservedWordMappings.putAll(generatorSettings.getReservedWordMappings());
if(generatorSettings.getReservedWordsMappings() != null) {
configurator.reservedWordsMappings.putAll(generatorSettings.getReservedWordsMappings());
}
if(generatorSettings.getServerVariables() != null) {
configurator.serverVariables.putAll(generatorSettings.getServerVariables());
@ -173,7 +173,7 @@ public class CodegenConfigurator {
}
public CodegenConfigurator addAdditionalReservedWordMapping(String key, String value) {
this.reservedWordMappings.put(key, value);
this.reservedWordsMappings.put(key, value);
generatorSettingsBuilder.withReservedWordMapping(key, value);
return this;
}
@ -226,9 +226,9 @@ public class CodegenConfigurator {
return this;
}
public CodegenConfigurator setReservedWordsMappings(Map<String, String> reservedWordMappings) {
this.reservedWordMappings = reservedWordMappings;
generatorSettingsBuilder.withReservedWordMappings(reservedWordMappings);
public CodegenConfigurator setReservedWordsMappings(Map<String, String> reservedWordsMappings) {
this.reservedWordsMappings = reservedWordsMappings;
generatorSettingsBuilder.withReservedWordsMappings(reservedWordsMappings);
return this;
}
@ -628,7 +628,7 @@ public class CodegenConfigurator {
config.importMapping().putAll(generatorSettings.getImportMappings());
config.inlineSchemaNameMapping().putAll(generatorSettings.getInlineSchemaNameMappings());
config.languageSpecificPrimitives().addAll(generatorSettings.getLanguageSpecificPrimitives());
config.reservedWordsMappings().putAll(generatorSettings.getReservedWordMappings());
config.reservedWordsMappings().putAll(generatorSettings.getReservedWordsMappings());
config.additionalProperties().putAll(generatorSettings.getAdditionalProperties());
Map<String, String> serverVariables = generatorSettings.getServerVariables();

View File

@ -146,14 +146,14 @@ public final class CodegenConfiguratorUtils {
}
}
public static void applyReservedWordsMappingsKvpList(List<String> reservedWordMappings, CodegenConfigurator configurator) {
for (String propString : reservedWordMappings) {
public static void applyReservedWordsMappingsKvpList(List<String> reservedWordsMappings, CodegenConfigurator configurator) {
for (String propString : reservedWordsMappings) {
applyReservedWordsMappingsKvp(propString, configurator);
}
}
public static void applyReservedWordsMappingsKvp(String reservedWordMappings, CodegenConfigurator configurator) {
final Map<String, String> map = createMapFromKeyValuePairs(reservedWordMappings);
public static void applyReservedWordsMappingsKvp(String reservedWordsMappings, CodegenConfigurator configurator) {
final Map<String, String> map = createMapFromKeyValuePairs(reservedWordsMappings);
for (Map.Entry<String, String> entry : map.entrySet()) {
configurator.addAdditionalReservedWordMapping(entry.getKey(), entry.getValue());
}

View File

@ -305,13 +305,18 @@ public abstract class AbstractDartCodegen extends DefaultCodegen {
@Override
protected boolean isReservedWord(String word) {
// consider everything as reserved that is either a keyword,
// a default included type, or a type include through some library
return super.isReservedWord(word) || defaultIncludes().contains(word);
// consider everything as reserved that is
// * a keyword
// * a word that has been mapped in the reservedWordsMappings
// * a default included type or a type include through some library
return super.isReservedWord(word) || reservedWordsMappings().containsKey(word) || defaultIncludes().contains(word);
}
@Override
public String escapeReservedWord(String name) {
if (reservedWordsMappings().containsKey(name)) {
return reservedWordsMappings().get(name);
}
return name + "_";
}

View File

@ -8,7 +8,7 @@ import 'package:openapi/api.dart';
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**class_** | **String** | | [optional]
**classField** | **String** | | [optional]
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

View File

@ -9,7 +9,7 @@ import 'package:openapi/api.dart';
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**name** | **int** | | [optional]
**class_** | **String** | | [optional]
**classField** | **String** | | [optional]
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

View File

@ -10,10 +10,10 @@ part 'class_model.g.dart';
/// Model for testing model with \"_class\" property
///
/// Properties:
/// * [class_]
/// * [classField]
abstract class ClassModel implements Built<ClassModel, ClassModelBuilder> {
@BuiltValueField(wireName: r'_class')
String? get class_;
String? get classField;
ClassModel._();
@ -37,10 +37,10 @@ class _$ClassModelSerializer implements StructuredSerializer<ClassModel> {
Iterable<Object?> serialize(Serializers serializers, ClassModel object,
{FullType specifiedType = FullType.unspecified}) {
final result = <Object?>[];
if (object.class_ != null) {
if (object.classField != null) {
result
..add(r'_class')
..add(serializers.serialize(object.class_,
..add(serializers.serialize(object.classField,
specifiedType: const FullType(String)));
}
return result;
@ -61,7 +61,7 @@ class _$ClassModelSerializer implements StructuredSerializer<ClassModel> {
case r'_class':
final valueDes = serializers.deserialize(value,
specifiedType: const FullType(String)) as String;
result.class_ = valueDes;
result.classField = valueDes;
break;
}
}

View File

@ -11,13 +11,13 @@ part 'model200_response.g.dart';
///
/// Properties:
/// * [name]
/// * [class_]
/// * [classField]
abstract class Model200Response implements Built<Model200Response, Model200ResponseBuilder> {
@BuiltValueField(wireName: r'name')
int? get name;
@BuiltValueField(wireName: r'class')
String? get class_;
String? get classField;
Model200Response._();
@ -47,10 +47,10 @@ class _$Model200ResponseSerializer implements StructuredSerializer<Model200Respo
..add(serializers.serialize(object.name,
specifiedType: const FullType(int)));
}
if (object.class_ != null) {
if (object.classField != null) {
result
..add(r'class')
..add(serializers.serialize(object.class_,
..add(serializers.serialize(object.classField,
specifiedType: const FullType(String)));
}
return result;
@ -76,7 +76,7 @@ class _$Model200ResponseSerializer implements StructuredSerializer<Model200Respo
case r'class':
final valueDes = serializers.deserialize(value,
specifiedType: const FullType(String)) as String;
result.class_ = valueDes;
result.classField = valueDes;
break;
}
}