Skip to content

Commit

Permalink
fix: support of one of composition schema
Browse files Browse the repository at this point in the history
  • Loading branch information
cpaulve-1A committed Jan 26, 2024
1 parent 869491a commit a3b6650
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -489,10 +489,19 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
}

// exclude the classname for revivers
List<String> filteredSubTypes = new ArrayList<String>();
List<AbstractMap.SimpleEntry<String, String>> filteredSubTypes = new ArrayList<AbstractMap.SimpleEntry<String, String>>();
for (String subType : sanitizedAllowableValues) {
if (!StringUtils.equals(model.classname, subType)) {
filteredSubTypes.add(subType);
String mappedModelName = subType;
if (model.discriminator.getMappedModels() != null) {
for (CodegenDiscriminator.MappedModel mappedModel : new ArrayList<CodegenDiscriminator.MappedModel>(model.discriminator.getMappedModels())) {
if (subType.equals(mappedModel.getMappingName())) {
mappedModelName = mappedModel.getModelName();
break;
}
}
}
filteredSubTypes.add(new AbstractMap.SimpleEntry(subType, mappedModelName));
}
}

Expand Down Expand Up @@ -546,50 +555,16 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
*/
@Override
public ModelsMap postProcessModels(ModelsMap objs) {
List<Map<String, String>> importsMap = objs.getImports();
for (Map _map : importsMap) {
String _import = (String) _map.get("import");
_map.put("import", _import.substring(_import.lastIndexOf(".") + 1));
}
List<ModelMap> models = objs.getModels();

// Store additional imports from vendor extensions
if (importsMap != null) {
List<String> alreadyImported = new ArrayList();
List<ModelMap> models = objs.getModels();
for (ModelMap modelMap : models) {
CodegenModel model = modelMap.getModel();
if (model.isEnum) {
nonObjectModels.add(model.name);
}
if (model.name.endsWith("_allOf")) {
model.vendorExtensions.put("x-intermediate-class", model.getDiscriminator() == null ? "composition" : "inheritance");
}
boolean containsExtensions = false;
ArrayList<List<CodegenProperty>> group = new ArrayList<List<CodegenProperty>>();
group.add(model.allVars);
group.add(model.vars);
group.add(model.requiredVars);
group.add(model.optionalVars);
for (List<CodegenProperty> container : group) {
for (CodegenProperty prop : container) {
Map<String, Object> vendorExtensions = prop.vendorExtensions;
if (vendorExtensions.containsKey("x-field-type")) {
containsExtensions = true;
String fieldType = (String) vendorExtensions.get("x-field-type");
if (!this.languageSpecificPrimitives.contains(fieldType) && !alreadyImported.contains(fieldType)) {
alreadyImported.add(fieldType);
HashMap addImport = new HashMap();
addImport.put("import", vendorExtensions.get("x-field-type"));
importsMap.add(addImport);
}
}
}

// We store on the model's vendor extension a parameter saying our vars have vendor extensions (hence, a dictionary)
if (containsExtensions == true) {
model.vendorExtensions.put("requireDictionary", Boolean.TRUE);
}
}
// Set additional properties and check non model objects
for (ModelMap modelMap : models) {
CodegenModel model = modelMap.getModel();
if (model.isEnum) {
nonObjectModels.add(toModelName(model.name));
}
if (model.name.endsWith("_allOf")) {
model.vendorExtensions.put("x-intermediate-class", model.getDiscriminator() == null ? "composition" : "inheritance");
}
}

Expand All @@ -611,7 +586,65 @@ protected void addImport(CodegenModel m, String type) {
}

/**
* This post-processing is to avoid the use of revivers from non-object models (e.g. enums in definition)
* Post process the import of a ModelsMap, make sure to include the field and the subtypes with their revivers
* @param objs
* @return
*/
private ModelsMap postProcessImports(ModelsMap objs) {
List<ModelMap> models = objs.getModels();
// Store additional imports from vendor extensions
List<Map<String, String>> importsMap = objs.getImports();
List<String> alreadyImported = new ArrayList();
for (Map _map : importsMap) {
String _import = (String) _map.get("import");
_map.put("import", _import.substring(_import.lastIndexOf(".") + 1));
alreadyImported.add(_import.substring(_import.lastIndexOf(".") + 1));
}
for (ModelMap modelMap : models) {
CodegenModel model = modelMap.getModel();
boolean containsExtensions = false;
ArrayList<List<CodegenProperty>> group = new ArrayList<List<CodegenProperty>>();
group.add(model.allVars);
group.add(model.vars);
group.add(model.requiredVars);
group.add(model.optionalVars);
for (List<CodegenProperty> container : group) {
for (CodegenProperty prop : container) {
Map<String, Object> vendorExtensions = prop.vendorExtensions;
List<String> importKeys = new ArrayList<String>();
if (vendorExtensions.containsKey("x-field-type")) {
containsExtensions = true;
importKeys.add((String) vendorExtensions.get("x-field-type"));
}
Map<String, Object> vendors = model.vendorExtensions;
if (vendors != null && vendors.containsKey("x-discriminator-subtypes")) {
for (AbstractMap.SimpleEntry<String, String> entry : (List<AbstractMap.SimpleEntry<String, String>>) vendors.get("x-discriminator-subtypes")) {
importKeys.add(entry.getValue());
}
}
for (String importModel : importKeys) {
if (!this.languageSpecificPrimitives.contains(importModel) && !alreadyImported.contains(importModel)) {
alreadyImported.add(importModel);
HashMap<String, String> addImport = new HashMap();
addImport.put("import", importModel);
importsMap.add(addImport);
}
}
}

// We store on the model's vendor extension a parameter saying our vars have vendor extensions (hence, a dictionary)
if (containsExtensions == true) {
model.vendorExtensions.put("requireDictionary", Boolean.TRUE);
}
}
}

return objs;
}

/**
* This post-processing is to avoid the use of revivers from non-object models (e.g. enums in definition),
* to remove intermediate classes with no use in the project and to adjust the imports
*/
@Override
public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs) {
Expand Down Expand Up @@ -680,6 +713,9 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
return entry;
}).collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue()));
}
for (Map.Entry<String, ModelsMap> entry : objs.entrySet()) {
entry.setValue(this.postProcessImports(entry.getValue()));
}
return objs;
}

Expand Down Expand Up @@ -722,7 +758,7 @@ private CodegenModel stripIntermediateModel(CodegenModel model, List<String> int
);
}
CodegenComposedSchemas composedSchema = model.getComposedSchemas();
if (composedSchema != null) {
if (composedSchema != null && composedSchema.getAllOf() != null) {
List<CodegenProperty> filteredAllOf = composedSchema.getAllOf().stream()
.filter(allOfProp -> intermediateModels.indexOf(allOfProp.baseType) == -1)
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,14 +439,14 @@ public RemoveUnusedImports() {
String importedClass = litr.next();
int count =0;
//Looking if the import is used in the fragment
Matcher m2 = Pattern.compile("^(?!(?:\\s*import\\s?\\{))(?:.*\\W)?"+ importedClass + "[^\\w\\/]",
Matcher m2 = Pattern.compile("^(?!(\\s*import\\s?\\{)|(\\s*\\*)|(/\\*))(?:.*[^\\w'\"])?"+ importedClass + "[^\\w\\/]",
Pattern
.MULTILINE)
.matcher
(fragment);
if (!m2.find()) {
//Import unused found, removing it
fragment = Pattern.compile("(.*import\\s\\{(?:.*\\W)?)" + importedClass +",?\\s*(\\W.*)")
fragment = Pattern.compile("(.*import\\s\\{(?:.*\\W)?)" + importedClass +"\\s*,?\\s*(\\W.*)")
.matcher(fragment)
.replaceAll("$1$2");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,51 +30,58 @@ import { {{import}} } from '../{{#kebabCase}}{{import}}{{/kebabCase}}';
* Generated by OpenApi generator to handle the inheritance of inline object
*
* @internal Please do not use this class but its child (class name stripped of AllOf)
* eg. instead of referring to DataModelAllOf, please refer to DataModel
* e.g. instead of referring to DataModelAllOf, please refer to DataModel
*/
{{/vendorExtensions.x-intermediate-class}}
{{^isEnum}}
{{^isEnum}}
{{^oneOf}}
export interface {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{
{{#vars}}
{{#description}}
{{#vars}}
{{#description}}
/** {{{description}}} */
{{/description}}
{{^description}}
{{#isArray}}
{{/description}}
{{^description}}
{{#isArray}}
/** List of {{#plurialize}}{{#noArrayInType}}{{{dataType}}}{{/noArrayInType}}{{/plurialize}} */
{{/isArray}}
{{^isArray}}
{{^isPrimitiveType}}
{{#datatypeWithEnum}}
{{/isArray}}
{{^isArray}}
{{^isPrimitiveType}}
{{#datatypeWithEnum}}
/** @see {{datatypeWithEnum}} */
{{/datatypeWithEnum}}
{{/isPrimitiveType}}
{{#isEnum}}
{{#datatypeWithEnum}}
{{/datatypeWithEnum}}
{{/isPrimitiveType}}
{{#isEnum}}
{{#datatypeWithEnum}}
/** @see {{datatypeWithEnum}} */
{{/datatypeWithEnum}}
{{/isEnum}}
{{/isArray}}
{{/description}}
{{/datatypeWithEnum}}
{{/isEnum}}
{{/isArray}}
{{/description}}
{{#readOnly}} readonly {{/readOnly}}{{#propertyDeclaration}}{{baseName}}{{/propertyDeclaration}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}};
{{/vars}}
{{#vars}}
{{#vendorExtensions}}
{{#x-field-name}}
{{/vars}}
{{#vars}}
{{#vendorExtensions}}
{{#x-field-name}}
/** Dictionary extraction for {{baseName}} */
{{#readOnly}}readonly {{/readOnly}}{{x-field-name}}{{^required}}?{{/required}}: {{x-field-type}};
{{/x-field-name}}
{{/vendorExtensions}}
{{/vars}}
{{#vars}}
{{#vendorExtensions}}
{{#x-map-name}}
{{/x-field-name}}
{{/vendorExtensions}}
{{/vars}}
{{#vars}}
{{#vendorExtensions}}
{{#x-map-name}}
/** Dictionary extraction for {{baseName}} */
{{#readOnly}}readonly {{/readOnly}}{{x-map-name}}{{^required}}?{{/required}}: { [key: string]: {{x-field-type}} | undefined };
{{/x-map-name}}
{{/vendorExtensions}}
{{/vars}}
{{/x-map-name}}
{{/vendorExtensions}}
{{/vars}}
}
{{/oneOf}}
{{#oneOf}}
{{#-first}}
export type {{classname}} = {{#oneOf}}{{.}}{{^-last}} | {{/-last}}{{/oneOf}};
{{/-first}}
{{/oneOf}}

{{#hasEnums}}
{{#allVars}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ import { revive{{import}} } from '../{{#kebabCase}}{{import}}{{/kebabCase}}';
{{#models}}
{{#model}}
{{^isEnum}}
{{#vendorExtensions}}
{{#x-discriminator-subtypes}}
import { revive{{.}} } from '../{{#kebabCase}}{{.}}{{/kebabCase}}';
import { {{.}} } from '../{{#kebabCase}}{{.}}{{/kebabCase}}';
{{/x-discriminator-subtypes}}
{{/vendorExtensions}}
import { utils, reviveArray, reviveMap, reviveDictionarizedArray } from '@ama-sdk/core';
{{/isEnum}}
{{/model}}
Expand All @@ -30,28 +24,33 @@ import { utils, reviveArray, reviveMap, reviveDictionarizedArray } from '@ama-sd
{{^isEnum}}
{{^isEnumRef}}
export function revive{{classname}}<T extends {{classname}} = {{classname}}>(data: undefined, dictionaries?: any): undefined;
export function revive{{classname}}(data: {{classname}}, dictionaries?: any): {{classname}} {{#discriminator}}{{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{.}}{{/x-discriminator-subtypes}}{{/vendorExtensions}}{{/discriminator}};
export function revive{{classname}}(data: any, dictionaries?: any): {{classname}} {{#discriminator}}{{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{.}}{{/x-discriminator-subtypes}}{{/vendorExtensions}}{{/discriminator}} | undefined;
export function revive{{classname}}<T extends {{classname}}>(data: T, dictionaries?: any): T {{#discriminator}}{{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{.}}{{/x-discriminator-subtypes}}{{/vendorExtensions}}{{/discriminator}};
export function revive{{classname}}<T extends {{classname}}>(data: any, dictionaries?: any): T {{#discriminator}}{{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{.}}{{/x-discriminator-subtypes}}{{/vendorExtensions}}{{/discriminator}} | undefined;
export function revive{{classname}}<T extends {{classname}} = {{classname}}>(data: any, dictionaries?: any): T {{#discriminator}}{{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{.}}{{/x-discriminator-subtypes}}{{/vendorExtensions}}{{/discriminator}} | undefined {
export function revive{{classname}}(data: {{classname}}, dictionaries?: any): {{classname}} {{#discriminator}}{{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{value}}{{/x-discriminator-subtypes}}{{/vendorExtensions}}{{/discriminator}};
export function revive{{classname}}(data: any, dictionaries?: any): {{classname}} {{#discriminator}}{{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{value}}{{/x-discriminator-subtypes}}{{/vendorExtensions}}{{/discriminator}} | undefined;
export function revive{{classname}}<T extends {{classname}}>(data: T, dictionaries?: any): T {{#discriminator}}{{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{value}}{{/x-discriminator-subtypes}}{{/vendorExtensions}}{{/discriminator}};
export function revive{{classname}}<T extends {{classname}}>(data: any, dictionaries?: any): T {{#discriminator}}{{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{value}}{{/x-discriminator-subtypes}}{{/vendorExtensions}}{{/discriminator}} | undefined;
export function revive{{classname}}<T extends {{classname}} = {{classname}}>(data: any, dictionaries?: any): T {{#discriminator}}{{#vendorExtensions}}{{#x-discriminator-subtypes}}| {{value}}{{/x-discriminator-subtypes}}{{/vendorExtensions}}{{/discriminator}} | undefined {
if (!data) { return ; }
{{#noEmptyLines}}
{{#discriminator}}
{{#discriminator}}
{{#vendorExtensions}}
{{#x-discriminator-subtypes}}
{{#-first}}
// Handle discriminator
if (data.{{discriminatorName}}) {
// Hack while waiting for DAPI fix
{{#vendorExtensions}}
{{#x-discriminator-subtypes}}
if (data.{{discriminatorName}}[0].toUpperCase() + data.{{discriminatorName}}.slice(1) === '{{.}}') {
return revive{{.}}(data, dictionaries);
{{#vendorExtensions}}
{{#x-discriminator-subtypes}}
if (data.{{discriminatorName}}[0].toUpperCase() + data.{{discriminatorName}}.slice(1) === '{{key}}') {
return revive{{value}}(data, dictionaries);
}
{{/x-discriminator-subtypes}}
{{/vendorExtensions}}
{{/x-discriminator-subtypes}}
{{/vendorExtensions}}
}
{{/discriminator}}
{{#vendorExtensions}}
{{#requireDictionary}}
{{/-first}}
{{/x-discriminator-subtypes}}
{{/vendorExtensions}}
{{/discriminator}}
{{#vendorExtensions}}
{{#requireDictionary}}
if (dictionaries) {
{{#allVars}}
{{#vendorExtensions}}
Expand Down Expand Up @@ -154,13 +153,12 @@ export function revive{{classname}}<T extends {{classname}} = {{classname}}>(dat
{{/isPrimitiveType}}
{{/allVars}}
{{/trimComma}}
{{/noEmptyLines}}

return data as T;
}

{{/isEnumRef}}
{{/isEnum}}
{{/noEmptyLines}}
{{/isEnumRef}}
{{/isEnum}}
{{#isEnum}}
/** As the definition is not an object, no reviver is needed */
{{/isEnum}}
Expand Down

0 comments on commit a3b6650

Please sign in to comment.