Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[csharp][generichost] Fix oneof anyof serialization #15873

Merged
merged 13 commits into from
Jul 1, 2023
Merged
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions .github/workflows/samples-dotnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
paths:
- 'samples/client/petstore/csharp/**net6.0**/'
- 'samples/client/petstore/csharp/OpenAPIClient-generichost-netcore**/'
- 'samples/client/petstore/csharp/OpenAPIClient-generichost-manual-tests/**'
- 'samples/server/petstore/aspnetcore-6.0/**'
- 'samples/server/petstore/aspnetcore-6.0-pocoModels/**'
- 'samples/server/petstore/aspnetcore-6.0-useSwashBuckle/**'
Expand All @@ -13,6 +14,7 @@ on:
paths:
- 'samples/client/petstore/csharp/**net6.0**/'
- 'samples/client/petstore/csharp/OpenAPIClient-generichost-netcore**/'
- 'samples/client/petstore/csharp/OpenAPIClient-generichost-manual-tests/**'
- 'samples/server/petstore/aspnetcore-6.0/**'
- 'samples/server/petstore/aspnetcore-6.0-pocoModels/**'
- 'samples/server/petstore/aspnetcore-6.0-project4Models/**'
Expand All @@ -33,6 +35,7 @@ jobs:
- samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-allOf
- samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-oneOf
- samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-anyOf
- samples/client/petstore/csharp/OpenAPIClient-generichost-manual-tests
- samples/server/petstore/aspnetcore-6.0
- samples/server/petstore/aspnetcore-6.0-pocoModels
- samples/server/petstore/aspnetcore-6.0-project4Models
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.openapitools.codegen;

import java.util.TreeSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.*;

/**
* This class encapsulates the OpenAPI discriminator construct, as specified at
Expand Down Expand Up @@ -114,6 +111,8 @@ public static class MappedModel implements Comparable<MappedModel>{
// is converted to a sanitized, internal representation within codegen.
private String modelName;

private CodegenModel model;

public MappedModel(String mappingName, String modelName) {
this.mappingName = mappingName;
this.modelName = modelName;
Expand Down Expand Up @@ -147,6 +146,10 @@ public void setModelName(String modelName) {
this.modelName = modelName;
}

public CodegenModel getModel() { return model; }

public void setModel(CodegenModel model) { this.model = model; }

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
for (Map.Entry<String, ModelsMap> entry : objs.entrySet()) {
CodegenModel model = ModelUtils.getModelByName(entry.getKey(), objs);

// add the model to the discriminator's mapping so templates have access to more than just the string to string mapping
if (model.discriminator != null && model.discriminator.getMappedModels() != null) {
devhl-labs marked this conversation as resolved.
Show resolved Hide resolved
for (CodegenDiscriminator.MappedModel mappedModel : model.discriminator.getMappedModels()) {
CodegenModel mappedCodegenModel = ModelUtils.getModelByName(mappedModel.getModelName(), objs);
mappedModel.setModel(mappedCodegenModel);
}
}

for (CodegenProperty property : model.allVars){
property.isNew = codegenPropertyIsNew(model, property);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,36 @@ public ModelsMap postProcessModels(ModelsMap objs) {
for (ModelMap mo : objs.getModels()) {
CodegenModel cm = mo.getModel();

if (cm.getComposedSchemas() != null) {
List<CodegenProperty> oneOf = cm.getComposedSchemas().getOneOf();
if (oneOf != null) {
Set<String> dataTypeSet = new HashSet<>();
for (CodegenProperty oneOfProperty : oneOf) {
if (dataTypeSet.contains(oneOfProperty.dataType)) {
// add "x-duplicated-data-type" to indicate if the dataType already occurs before
// in other sub-schemas of allOf/anyOf/oneOf
oneOfProperty.vendorExtensions.putIfAbsent("x-composed-data-type", true);
} else {
dataTypeSet.add(oneOfProperty.dataType);
}
}
}

List<CodegenProperty> anyOf = cm.getComposedSchemas().getAnyOf();
if (anyOf != null) {
Set<String> dataTypeSet = new HashSet<>();
for (CodegenProperty anyOfProperty : anyOf) {
if (dataTypeSet.contains(anyOfProperty.dataType)) {
// add "x-duplicated-data-type" to indicate if the dataType already occurs before
// in other sub-schemas of allOf/anyOf/oneOf
anyOfProperty.vendorExtensions.putIfAbsent("x-composed-data-type", true);
} else {
dataTypeSet.add(anyOfProperty.dataType);
}
}
}
}

if (cm.isEnum && !cm.vendorExtensions.containsKey(this.zeroBasedEnumVendorExtension)) {
if (Boolean.TRUE.equals(this.zeroBasedEnums)) {
cm.vendorExtensions.put(this.zeroBasedEnumVendorExtension, true);
Expand Down Expand Up @@ -487,6 +517,17 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
Map<String, CodegenModel> enumRefs = new HashMap<>();
for (Map.Entry<String, ModelsMap> entry : processed.entrySet()) {
CodegenModel model = ModelUtils.getModelByName(entry.getKey(), processed);

// if we don't call setHasDiscriminatorWithNonEmptyMapping then hasDiscriminatorWithNonEmptyMapping will be false, and we need it in the JsonConverter
// the checks on oneOf and anyOf must be there or else hasDiscriminatorWithNonEmptyMapping will be true for GrandparentAnimal.
// GrandparentAnimal has a discriminator, but no oneOf nor anyOf
// modules\openapi-generator\src\test\resources\3_0\csharp\petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml
model.setHasDiscriminatorWithNonEmptyMapping(
((model.anyOf != null && model.anyOf.size() > 0) || (model.anyOf != null &&model.oneOf.size() > 0)) &&
model.discriminator != null &&
model.discriminator.getMappedModels() != null &&
model.discriminator.getMappedModels().size() > 0);

if (model.isEnum) {
enumRefs.put(model.getClassname(), model);
}
Expand Down
Loading
Loading