Skip to content
New issue

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

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

Already on GitHub? # to your account

[Rust Server] Support types with additional properties #3666

Merged
merged 2 commits into from
Sep 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,12 @@ public CodegenModel fromModel(String name, Schema model) {
additionalProperties.put("usesXmlNamespaces", true);
}

Schema additionalProperties = ModelUtils.getAdditionalProperties(model);

if (additionalProperties != null) {
mdl.additionalPropertiesType = getSchemaType(additionalProperties);
}

return mdl;
}

Expand Down Expand Up @@ -1123,20 +1129,34 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
// 'null'. This ensures that we treat this model as a struct
// with multiple parameters.
cm.dataType = null;
} else if (cm.dataType != null) {
if (cm.dataType.equals("map")) {
// We don't yet support `additionalProperties`. We ignore
// the `additionalProperties` type ('map') and warn the
// user. This will produce code that compiles, but won't
// feature the `additionalProperties`.
} else if (cm.dataType != null && cm.dataType.equals("map")) {
if (!cm.allVars.isEmpty() || cm.additionalPropertiesType == null) {
// We don't yet support `additionalProperties` that also have
// properties. If we see variables, we ignore the
// `additionalProperties` type ('map') and warn the user. This
// will produce code that compiles, but won't feature the
// `additionalProperties` - but that's likely more useful to
// the user than the alternative.
LOGGER.warn("Ignoring additionalProperties (see https://github.com/OpenAPITools/openapi-generator/issues/318) alongside defined properties");
cm.dataType = null;
LOGGER.warn("Ignoring unsupported additionalProperties (see https://github.com/OpenAPITools/openapi-generator/issues/318)");
} else {
// We need to hack about with single-parameter models to
// get them recognised correctly.
cm.isAlias = false;
cm.dataType = typeMapping.get(cm.dataType);
}
else
{
String type;

if (typeMapping.containsKey(cm.additionalPropertiesType)) {
type = typeMapping.get(cm.additionalPropertiesType);
} else {
type = toModelName(cm.additionalPropertiesType);
}

cm.dataType = "HashMap<String, " + type + ">";
}
} else if (cm.dataType != null) {
// We need to hack about with single-parameter models to
// get them recognised correctly.
cm.isAlias = false;
cm.dataType = typeMapping.get(cm.dataType);
}

cm.vendorExtensions.put("isString", "String".equals(cm.dataType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,19 @@ impl ::std::str::FromStr for {{{classname}}} {
}
}
}
{{/isEnum}}{{^isEnum}}{{#dataType}}{{! newtype}}#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
{{/isEnum}}
{{^isEnum}}
{{#dataType}}
{{#isMapModel}}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
{{/isMapModel}}
{{^isMapModel}}
#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
{{/isMapModel}}
#[cfg_attr(feature = "conversion", derive(LabelledGeneric))]
{{#xmlName}}#[serde(rename = "{{{xmlName}}}")]{{/xmlName}}
{{#xmlName}}
#[serde(rename = "{{{xmlName}}}")]
{{/xmlName}}
pub struct {{{classname}}}({{{dataType}}});

impl ::std::convert::From<{{{dataType}}}> for {{{classname}}} {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ impl DuplicateXmlObject {
/// Test a model containing a UUID
#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
#[cfg_attr(feature = "conversion", derive(LabelledGeneric))]

pub struct UuidObject(uuid::Uuid);

impl ::std::convert::From<uuid::Uuid> for UuidObject {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,6 @@ impl Order {

#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
#[cfg_attr(feature = "conversion", derive(LabelledGeneric))]

pub struct OuterBoolean(bool);

impl ::std::convert::From<bool> for OuterBoolean {
Expand Down Expand Up @@ -1190,7 +1189,6 @@ impl OuterEnum {

#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
#[cfg_attr(feature = "conversion", derive(LabelledGeneric))]

pub struct OuterNumber(f64);

impl ::std::convert::From<f64> for OuterNumber {
Expand Down Expand Up @@ -1231,7 +1229,6 @@ impl OuterNumber {

#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
#[cfg_attr(feature = "conversion", derive(LabelledGeneric))]

pub struct OuterString(String);

impl ::std::convert::From<String> for OuterString {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,35 @@ impl ANullableContainer {
/// An additionalPropertiesObject
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "conversion", derive(LabelledGeneric))]
pub struct AdditionalPropertiesObject {
pub struct AdditionalPropertiesObject(HashMap<String, String>);

impl ::std::convert::From<HashMap<String, String>> for AdditionalPropertiesObject {
fn from(x: HashMap<String, String>) -> Self {
AdditionalPropertiesObject(x)
}
}

impl AdditionalPropertiesObject {
pub fn new() -> AdditionalPropertiesObject {
AdditionalPropertiesObject {
}

impl ::std::convert::From<AdditionalPropertiesObject> for HashMap<String, String> {
fn from(x: AdditionalPropertiesObject) -> Self {
x.0
}
}

impl ::std::ops::Deref for AdditionalPropertiesObject {
type Target = HashMap<String, String>;
fn deref(&self) -> &HashMap<String, String> {
&self.0
}
}

impl ::std::ops::DerefMut for AdditionalPropertiesObject {
fn deref_mut(&mut self) -> &mut HashMap<String, String> {
&mut self.0
}
}



#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "conversion", derive(LabelledGeneric))]
Expand Down