Skip to content

Commit

Permalink
generator: HACK for new case of swagger-parser#1961
Browse files Browse the repository at this point in the history
With the 2024.1 models, swagger-parser is now creating additional
duplicated schemas in the graph, suffixed with "_1". This is the same
behavior that was observed in swagger-api/swagger-parser#1961, only this
time it's appearing even with my changes to v2.1.17. Since I'm committed
at this point to a Rust rewrite, implement a hack to allow model
generation to succeed.
  • Loading branch information
AmateurECE committed Aug 15, 2024
1 parent 011f083 commit 87cf956
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,18 @@ public class RedfishCodegen {
typeMappers[2] = new SimpleModelTypeMapper(odataModelIdentifierFactory, new SnakeCaseName("odata_v4"));
typeMappers[3] = new UnversionedModelTypeMapper();

IModelModelMapper[] modelMappers = new IModelModelMapper[1];
IModelModelMapper[] modelMappers = new IModelModelMapper[2];
HashMap<String, String> blacklist = new HashMap<>();
blacklist.put("Resource_Status_1", "Resource_Status");
blacklist.put("Resource_Status_Conditions_inner", "Resource_Condition");
blacklist.put("Resource_Status_1_Conditions_inner", "Resource_Condition");
blacklist.put("Resource_Condition_1", "Resource_Condition");
modelMappers[0] = new BlacklistModelModelMapper(blacklist);

Pattern odataModelPattern = Pattern.compile("odata_v?4_0_[0-9]_");
modelMappers[0] = new NamespaceMapper(odataModelPattern, "odata-v4_");
modelMappers[1] = new NamespaceMapper(odataModelPattern, "odata-v4_");
this.modelResolver = new ModelResolver(typeMappers, modelMappers);

IModelContextFactory[] factories = new IModelContextFactory[6];
factories[0] = new EnumContextFactory();
factories[1] = new FreeFormObjectContextFactory();
Expand All @@ -100,9 +108,11 @@ public class RedfishCodegen {

// These intrusive/low-level policies need to be applied to the set of models as a whole, but should not be
// coupled to context factories.
this.modelGenerationPolicies = new IModelGenerationPolicy[4];
this.modelGenerationPolicies = new IModelGenerationPolicy[6];
this.modelGenerationPolicies[0] = new ModelDeletionPolicy(Pattern.compile(odataModelPattern + "|.*_(EventRecord)?Oem(Actions)?"));
this.modelGenerationPolicies[1] = new ODataPropertyPolicy(new ODataTypeIdentifier(), this.clientMode);
this.modelGenerationPolicies[1] = new ModelDeletionPolicy(Pattern.compile("Resource_(Status|Condition)_1"));
this.modelGenerationPolicies[2] = new ModelDeletionPolicy(Pattern.compile("Resource_Status_Conditions_inner"));
this.modelGenerationPolicies[3] = new ODataPropertyPolicy(new ODataTypeIdentifier(), this.clientMode);
JsonSchemaMapper[] jsonSchemaMappers = new JsonSchemaMapper[2];

Pattern versionParsePattern = Pattern.compile("([0-9]+)_([0-9]+)_([0-9]+)");
Expand All @@ -124,8 +134,8 @@ public class RedfishCodegen {
jsonSchemaMappers[1] = new JsonSchemaMapper(
odataModelIdentifierFactory,
odataJsonSchema.get().file.getFileName().toString());
this.modelGenerationPolicies[2] = new ModelMetadataPolicy(new JsonSchemaIdentifier(jsonSchemaMappers));
this.modelGenerationPolicies[3] = new AdditionalModelAttributesPolicy(
this.modelGenerationPolicies[4] = new ModelMetadataPolicy(new JsonSchemaIdentifier(jsonSchemaMappers));
this.modelGenerationPolicies[5] = new AdditionalModelAttributesPolicy(
Pattern.compile("^(Event|Message)_v[0-9_]+(Event|Message)$"),
CfgAttrExpression.withEqualityPredicate("feature", "\"valuable\"")
.attribute("derive(valuable::Valuable)")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.twardyece.dmtf.model.mapper;

import java.util.HashMap;
import java.util.Optional;

public class BlacklistModelModelMapper implements IModelModelMapper {
HashMap<String, String> blacklist;
public BlacklistModelModelMapper(HashMap<String, String> blacklist) {
this.blacklist = blacklist;
}
@Override
public Optional<String> match(String model) {
if (this.blacklist.containsKey(model)) {
return Optional.of(this.blacklist.get(model));
} else {
return Optional.empty();
}
}
}

0 comments on commit 87cf956

Please sign in to comment.