Skip to content

Commit

Permalink
Fixed 2.0 schemas; possible freeze present
Browse files Browse the repository at this point in the history
  • Loading branch information
Victoria-Casasampere-BeeTheData committed Dec 16, 2024
1 parent 1984c1c commit 4bca943
Show file tree
Hide file tree
Showing 13 changed files with 6,938 additions and 4,418 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1083,8 +1083,8 @@ public String toString() {
sb.append(", items='").append(items).append('\'');
sb.append(", additionalProperties='").append(additionalProperties).append('\'');
sb.append(", isModel='").append(isModel).append('\'');
sb.append(", isNull='").append(isNull);
sb.append(", hasValidation='").append(hasValidation);
sb.append(", isNull='").append(isNull).append('\'');
sb.append(", hasValidation='").append(hasValidation).append('\'');
sb.append(", getAdditionalPropertiesIsAnyType=").append(getAdditionalPropertiesIsAnyType());
sb.append(", getHasDiscriminatorWithNonEmptyMapping=").append(hasDiscriminatorWithNonEmptyMapping);
sb.append(", getIsAnyType=").append(getIsAnyType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.RequestBody;
Expand All @@ -28,6 +29,7 @@
import io.swagger.v3.oas.models.tags.Tag;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.commonmark.node.Code;
import org.openapitools.codegen.*;
import org.openapitools.codegen.meta.GeneratorMetadata;
import org.openapitools.codegen.meta.Stability;
Expand Down Expand Up @@ -594,12 +596,28 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
return op;
}

@Override
public OperationsMap postProcessOperationsWithModels(final OperationsMap operationsMap, List<ModelMap> allModels) {
HashMap<String, List<String>> oneOfMapDiscriminator = new HashMap<>();

for (ModelMap mo : allModels) {
CodegenModel cm = mo.getModel();

CodegenComposedSchemas cs = cm.getComposedSchemas();
if (cs != null) {
List<CodegenProperty> csOneOf = cs.getOneOf();

if (csOneOf != null) {
for (CodegenProperty model : csOneOf) {
String[] modelParts = model.dataType.replace("<", "Of").replace(">", "").split("::");
model.datatypeWithEnum = camelize(modelParts[modelParts.length - 1]);
}

cs.setOneOf(csOneOf);
cm.setComposedSchemas(cs);
}
}

if (cm.discriminator != null) {
for (String model : cm.oneOf) {
List<String> discriminators = oneOfMapDiscriminator.getOrDefault(model, new ArrayList<>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,26 +576,32 @@ impl PartialEq for {{{classname}}} {
{{/anyOf.size}}
{{#oneOf.size}}
#[derive(Debug, Clone, PartialEq, serde::Deserialize)]
{{#discriminator}}
#[derive(Debug, Clone, PartialEq, serde::Deserialize)]
#[serde(tag = "{{{propertyBaseName}}}")]
{{/discriminator}}
{{^discriminator}}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
{{/discriminator}}
#[allow(non_camel_case_types)]
pub enum {{{classname}}} {
{{#oneOf}}
{{{.}}}(Box<{{{.}}}>),
{{/oneOf}}
{{#composedSchemas}}
{{#oneOf}}
{{{datatypeWithEnum}}}(Box<{{{dataType}}}>),
{{/oneOf}}
{{/composedSchemas}}
}
impl validator::Validate for {{{classname}}}
{
fn validate(&self) -> std::result::Result<(), validator::ValidationErrors> {
match self {
{{#composedSchemas}}
{{#oneOf}}
Self::{{{.}}}(x) => x.validate(),
Self::{{{datatypeWithEnum}}}(x) => x.validate(),
{{/oneOf}}
{{/composedSchemas}}
}
}
}
Expand All @@ -605,38 +611,27 @@ impl serde::Serialize for {{{classname}}} {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: serde::Serializer {
match self {
{{#composedSchemas}}
{{#oneOf}}
Self::{{{.}}}(x) => {{{.}}}::serialize(x, serializer),
Self::{{{datatypeWithEnum}}}(x) => x.serialize(serializer),
{{/oneOf}}
{{/composedSchemas}}
}
}
}
{{#oneOf}}
impl {{{.}}} {
fn __name_for_{{{propertyBaseName}}}() -> String {
String::from("{{{.}}}")
}
fn __serialize_{{{propertyBaseName}}}<S>(_: &String, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
s.serialize_str(&Self::__name_for_{{{propertyBaseName}}}())
}
}
{{/oneOf}}
{{/discriminator}}
{{#composedSchemas}}
{{#oneOf}}
impl From<{{{.}}}> for {{{classname}}} {
fn from(value: {{{.}}}) -> Self {
Self::{{{.}}}(Box::new(value))
impl From<{{{dataType}}}> for {{{classname}}} {
fn from(value: {{{dataType}}}) -> Self {
Self::{{{datatypeWithEnum}}}(Box::new(value))
}
}
{{/oneOf}}
{{/composedSchemas}}
/// Converts Query Parameters representation (style=form, explode=false) to a {{{classname}}} value
/// as specified in https://swagger.io/docs/specification/serialization/
Expand All @@ -659,13 +654,13 @@ pub struct {{{classname}}} {
{{#description}}
/// {{{.}}}
{{/description}}
{{#isEnum}}
/// Note: inline enums are not fully supported by openapi-generator
{{/isEnum}}
{{#isDiscriminator}}
#[serde(default = "{{{classname}}}::__name_for_{{{name}}}")]
#[serde(serialize_with = "{{{classname}}}::__serialize_{{{name}}}")]
{{/isDiscriminator}}
{{#isEnum}}
/// Note: inline enums are not fully supported by openapi-generator
{{/isEnum}}
#[serde(rename = "{{{baseName}}}")]
{{#hasValidation}}
#[validate(
Expand Down Expand Up @@ -733,6 +728,25 @@ pub struct {{{classname}}} {
{{/vars}}
}
{{#vars}}
{{#isDiscriminator}}
impl {{{classname}}} {
fn __name_for_{{{name}}}() -> String {
String::from("{{{classname}}}")
}
fn __serialize_{{{name}}}<S>(_: &String, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
s.serialize_str(&Self::__name_for_{{{name}}}())
}
}
{{/isDiscriminator}}
{{/vars}}
{{#vars}}
{{#hasValidation}}
{{#pattern}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
unused_extern_crates,
non_camel_case_types,
unused_imports,
unused_attributes
unused_attributes,
)]
#![allow(
clippy::derive_partial_eq_without_eq,
Expand All @@ -20,9 +20,9 @@ pub const API_VERSION: &str = "1.0.7";
#[cfg(feature = "server")]
pub mod server;

pub mod apis;
pub mod models;
pub mod types;
pub mod apis;

#[cfg(feature = "server")]
pub(crate) mod header;
Loading

0 comments on commit 4bca943

Please sign in to comment.