Skip to content

Commit

Permalink
Bugfixes/12952 fix newtonsoft using (#15515)
Browse files Browse the repository at this point in the history
* Fix #12952: Handle useNewtonsoft parameter correctly
Next commit will have all modified samples.

* Fix #12952: Add generated samples

* Fix #12952: Add missing files
  • Loading branch information
JFCote committed May 16, 2023
1 parent fbf42de commit edad90e
Show file tree
Hide file tree
Showing 123 changed files with 5,299 additions and 89 deletions.
9 changes: 9 additions & 0 deletions bin/configs/aspnetcore-6.0-NewtonsoftFalse.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
generatorName: aspnetcore
outputDir: samples/server/petstore/aspnetcore-6.0-useNewtonsoft
inputSpec: modules/openapi-generator/src/test/resources/3_0/aspnetcore/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/aspnetcore/3.0
additionalProperties:
packageGuid: '{3C799344-F285-4669-8FD5-7ED9B795D5C5}'
aspnetCoreVersion: "6.0"
userSecretsGuid: 'cb87e868-8646-48ef-9bb6-344b537d0d37'
useNewtonsoft: false
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,10 @@ public void processOpts() {

if (useSeparateModelProject)
{
supportingFiles.add(new SupportingFile("typeConverter.mustache", sourceFolder + File.separator + modelPackage + File.separator + "Converters", "CustomEnumConverter.cs"));
String separateModelSeparator = sourceFolder + File.separator + modelPackage ;
supportingFiles.add(new SupportingFile("gitignore", separateModelSeparator, ".gitignore"));
supportingFiles.add(new SupportingFile("typeConverter.mustache", separateModelSeparator + File.separator + "Converters", "CustomEnumConverter.cs"));
supportingFiles.add(new SupportingFile("ModelsProject.csproj.mustache", separateModelSeparator, modelPackage + ".csproj"));
} else {
supportingFiles.add(new SupportingFile("typeConverter.mustache", packageFolder + File.separator + "Converters", "CustomEnumConverter.cs"));
}
Expand All @@ -419,11 +422,6 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("OpenApi" + File.separator + "TypeExtensions.mustache", packageFolder + File.separator + "OpenApi", "TypeExtensions.cs"));
}

if (useSeparateModelProject)
{
supportingFiles.add(new SupportingFile("ModelsProject.csproj.mustache", sourceFolder + File.separator + modelPackage, modelPackage + ".csproj"));
}

supportingFiles.add(new SupportingFile("Project.csproj.mustache", packageFolder, packageName + ".csproj"));
if (!isLibrary) {
supportingFiles.add(new SupportingFile("Dockerfile.mustache", packageFolder, "Dockerfile"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ using Microsoft.OpenApi.Models;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
{{/useNewtonsoft}}
{{^useNewtonsoft}}
using System.Text.Json;
using System.Text.Json.Serialization;
{{/useNewtonsoft}}
using {{packageName}}.Authentication;
{{#useSwashbuckle}}
using {{packageName}}.Filters;
Expand Down Expand Up @@ -73,16 +77,24 @@ namespace {{packageName}}
// Don't need this for 3.x - see https://docs.microsoft.com/en-us/aspnet/core/mvc/compatibility-version?view=aspnetcore-3.1
//.SetCompatibilityVersion(CompatibilityVersion.{{.}})
{{/compatibilityVersion}}
.{{#useNewtonsoft}}AddNewtonsoftJson{{/useNewtonsoft}}{{^useNewtonsoft}}AddJsonOptions{{/useNewtonsoft}}(opts =>
{{#useNewtonsoft}}
.AddNewtonsoftJson(opts =>
{
opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
opts.SerializerSettings.Converters.Add(new StringEnumConverter
{
{{#useNewtonsoft}}NamingStrategy = new CamelCaseNamingStrategy(){{/useNewtonsoft}}{{^useNewtonsoft}}CamelCaseText = true{{/useNewtonsoft}}
NamingStrategy = new CamelCaseNamingStrategy()
});
});
{{/useNewtonsoft}}
{{^useNewtonsoft}}
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
});
{{/useNewtonsoft}}
{{#useSwashbuckle}}

services
.AddSwaggerGen(c =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ using Swashbuckle.AspNetCore.Annotations;
using Swashbuckle.AspNetCore.SwaggerGen;
{{/useSwashbuckle}}
{{^isLibrary}}
{{#useNewtonsoft}}
using Newtonsoft.Json;
{{/useNewtonsoft}}
{{^useNewtonsoft}}
using System.Text.Json;
{{/useNewtonsoft}}
{{/isLibrary}}
using {{packageName}}.Attributes;
using {{modelPackage}};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
{{#description}}
/// <value>{{{.}}}</value>
{{/description}}
{{#allowableValues}}{{#enumVars}}{{#-first}}{{#isString}}[TypeConverter(typeof(CustomEnumConverter<{{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}>))]
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]{{/isString}}{{/-first}}{{/enumVars}}{{/allowableValues}}
{{#allowableValues}}{{#enumVars}}{{#-first}}{{#isString}}{{#useNewtonsoft}}[TypeConverter(typeof(CustomEnumConverter<{{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}>))]
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]{{/useNewtonsoft}}{{/isString}}{{/-first}}{{/enumVars}}{{/allowableValues}}
public enum {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}
{
{{#allowableValues}}{{#enumVars}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

var example = exampleJson != null
? JsonConvert.DeserializeObject<{{returnContainer}}<{{{returnType}}}>>(exampleJson)
? {{#useNewtonsoft}}JsonConvert.DeserializeObject{{/useNewtonsoft}}{{^useNewtonsoft}}JsonSerializer.Deserialize{{/useNewtonsoft}}<{{returnContainer}}<{{{returnType}}}>>(exampleJson)
: Enumerable.Empty<{{{returnType}}}>();
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

var example = exampleJson != null
? JsonConvert.DeserializeObject<{{{returnType}}}>(exampleJson)
? {{#useNewtonsoft}}JsonConvert.DeserializeObject{{/useNewtonsoft}}{{^useNewtonsoft}}JsonSerializer.Deserialize{{/useNewtonsoft}}<{{{returnType}}}>(exampleJson)
: new {{{returnType}}}();
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
{{#useNewtonsoft}}
using Newtonsoft.Json;
{{/useNewtonsoft}}
{{^useNewtonsoft}}
using System.Text.Json;
{{/useNewtonsoft}}
{{#models}}
{{#model}}
{{#discriminator}}
Expand All @@ -28,7 +33,7 @@ namespace {{modelPackage}}
/// </summary>
[DataContract]
{{#discriminator}}
[JsonConverter(typeof(JsonSubtypes), "{{{discriminatorName}}}")]
{{#useNewtonsoft}}[JsonConverter(typeof(JsonSubtypes), "{{{discriminatorName}}}")]{{/useNewtonsoft}}
{{#useSwashbuckle}}
[SwaggerDiscriminator("{{{discriminatorName}}}")]
{{/useSwashbuckle}}
Expand Down Expand Up @@ -99,7 +104,17 @@ namespace {{modelPackage}}
/// <returns>JSON string presentation of the object</returns>
public {{#parent}}{{^isMap}}{{^isArray}}new {{/isArray}}{{/isMap}}{{/parent}}string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
{{#useNewtonsoft}}
return JsonConvert.SerializeObject(this, Formatting.Indented);
{{/useNewtonsoft}}
{{^useNewtonsoft}}
var options = new JsonSerializerOptions
{
WriteIndented = true
};

return JsonSerializer.Serialize(this, options);
{{/useNewtonsoft}}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

var example = exampleJson != null
? JsonConvert.DeserializeObject<{{{returnType}}}>(exampleJson)
? {{#useNewtonsoft}}JsonConvert.DeserializeObject{{/useNewtonsoft}}{{^useNewtonsoft}}JsonSerializer.Deserialize{{/useNewtonsoft}}<{{{returnType}}}>(exampleJson)
: default({{{returnType}}});
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
using System;
using System.ComponentModel;
using System.Globalization;
{{#useNewtonsoft}}
using Newtonsoft.Json;
{{/useNewtonsoft}}
{{^useNewtonsoft}}
using System.Text.Json;
{{/useNewtonsoft}}

namespace {{packageName}}.Converters
{
Expand Down Expand Up @@ -36,7 +41,7 @@ namespace {{packageName}}.Converters
return null;
}

return JsonConvert.DeserializeObject<T>(@"""" + value.ToString() + @"""");
return {{#useNewtonsoft}}JsonConvert.DeserializeObject{{/useNewtonsoft}}{{^useNewtonsoft}}JsonSerializer.Deserialize{{/useNewtonsoft}}<T>(@"""" + value.ToString() + @"""");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public override string ToString()
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public override string ToString()
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public override string ToString()
/// <returns>JSON string presentation of the object</returns>
public new string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public override string ToString()
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public override string ToString()
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public override string ToString()
/// <returns>JSON string presentation of the object</returns>
public new string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public override string ToString()
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public override string ToString()
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public override string ToString()
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public override string ToString()
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public override string ToString()
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public void ConfigureServices(IServiceCollection services)
NamingStrategy = new CamelCaseNamingStrategy()
});
});

services
.AddSwaggerGen(c =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public override string ToString()
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public override string ToString()
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public override string ToString()
/// <returns>JSON string presentation of the object</returns>
public new string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public override string ToString()
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public override string ToString()
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public override string ToString()
/// <returns>JSON string presentation of the object</returns>
public new string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public override string ToString()
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public override string ToString()
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public override string ToString()
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public override string ToString()
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public override string ToString()
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public void ConfigureServices(IServiceCollection services)
NamingStrategy = new CamelCaseNamingStrategy()
});
});

services
.AddSwaggerGen(c =>
{
Expand Down
Loading

0 comments on commit edad90e

Please sign in to comment.