Skip to content

Commit

Permalink
fix: Add prefix and suffix to discriminator (#16684)
Browse files Browse the repository at this point in the history
Use the existing toModelName method to normalize the discriminator

Co-authored-by: Matthias Gabriel <[email protected]>
  • Loading branch information
MatthiasGabriel and MatthiasGabriel authored Oct 3, 2023
1 parent baf33f0 commit 4075a52
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3585,6 +3585,7 @@ protected CodegenDiscriminator createDiscriminator(String schemaName, Schema sch
.map(p -> (Schema<?>) p.get(discriminatorPropertyName))
.map(Schema::get$ref)
.map(ModelUtils::getSimpleRef)
.map(this::toModelName)
.orElseGet(() -> typeMapping.get("string"));
discriminator.setPropertyType(propertyType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1529,6 +1529,25 @@ public void testComposedSchemaOneOfDiscriminatorMap() {
Assert.assertEquals(cm.discriminator.getMappedModels(), hs);
}

@Test
public void testComposedSchemaOneOfDiscriminatorMapPreAndPostFix() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/oneOfDiscriminator.yaml");
DefaultCodegen codegen = new DefaultCodegen();
codegen.setLegacyDiscriminatorBehavior(false);
codegen.setOpenAPI(openAPI);
codegen.setModelNamePrefix("Transfer");
codegen.setModelNameSuffix("Dto");
String modelName;
Schema sc;
CodegenModel cm;

// ref oneOf models with enum property discriminator
modelName = "FruitOneOfEnumMappingDisc";
sc = openAPI.getComponents().getSchemas().get(modelName);
cm = codegen.fromModel(modelName, sc);
assertThat(cm.discriminator.getPropertyType()).isEqualTo("TransferFruitTypeEnumDto");
}

@Test
public void testComposedSchemaMyPetsOneOfDiscriminatorMap() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/allOf_composition_discriminator.yaml");
Expand Down

0 comments on commit 4075a52

Please sign in to comment.