Skip to content

Commit

Permalink
[cpp-ue4] Handled nullable by setting the required tag, effectively m…
Browse files Browse the repository at this point in the history
…aking nullables optionals (even if they may be required in the spec). This is because there is no such concept as a value being nullable in C++, and this generator deals with values, not pointers. (OpenAPITools#18168)

Co-authored-by: Samuel Kahn <[email protected]>
  • Loading branch information
2 people authored and zapodot committed Mar 21, 2024
1 parent 9a3e234 commit d7db3c0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
import io.swagger.v3.oas.models.media.Schema;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.CodegenResponse;
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.meta.GeneratorMetadata;
import org.openapitools.codegen.meta.Stability;
Expand Down Expand Up @@ -246,6 +250,34 @@ public void setOptionalProjectFileFlag(boolean flag) {
this.optionalProjectFileFlag = flag;
}

// override to post-process any model properties
@Override
@SuppressWarnings("unused")
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
super.postProcessModelProperty(model, property);
// Nullable will be handled as optional
property.required = !property.notRequiredOrIsNullable();
}

// override to post-process any response
@Override
@SuppressWarnings("unused")
public void postProcessResponseWithProperty(CodegenResponse response, CodegenProperty property) {
super.postProcessResponseWithProperty(response, property);
// Nullable will be handled as optional
property.required = !property.notRequiredOrIsNullable();
}

// override to post-process any parameters
@Override
@SuppressWarnings("unused")
public void postProcessParameter(CodegenParameter parameter) {
super.postProcessParameter(parameter);
// Nullable will be handled as optional
parameter.required = !parameter.notRequiredOrIsNullable();
}


/**
* Configures the type of generator.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,18 +448,21 @@ inline bool TryGetJsonValue(const TSharedPtr<FJsonObject>& JsonObject, const FSt
template<typename T>
inline bool TryGetJsonValue(const TSharedPtr<FJsonObject>& JsonObject, const FString& Key, TOptional<T>& OptionalValue)
{
if(JsonObject->HasField(Key))
const TSharedPtr<FJsonValue> JsonValue = JsonObject->TryGetField(Key);
if (JsonValue.IsValid() && !JsonValue->IsNull())
{
T Value;
if (TryGetJsonValue(JsonObject, Key, Value))
if (TryGetJsonValue(JsonValue, Value))
{
OptionalValue = Value;
return true;
}
else
return false;
}
return true; // Absence of optional value is not a parsing error
// Absence of optional value is not a parsing error.
// Nullable is handled like optional.
return true;
}

{{#cppNamespaceDeclarations}}
Expand Down

0 comments on commit d7db3c0

Please sign in to comment.