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

[ASPNETCORE] Add support for central package version management #20192

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
@@ -0,0 +1,10 @@
generatorName: aspnetcore
outputDir: samples/server/petstore/aspnetcore-8.0-use-centralized-package-version-management
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: 9.0
userSecretsGuid: 'cb87e868-8646-48ef-9bb6-344b537d0d37'
useCentralizedPackageVersionManagement: true
centralizedPackageVersionManagementOptOut: false
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ public class AspNetServerCodegen extends AbstractCSharpCodegen {
public static final String USE_NEWTONSOFT = "useNewtonsoft";
public static final String USE_DEFAULT_ROUTING = "useDefaultRouting";
public static final String NEWTONSOFT_VERSION = "newtonsoftVersion";
public static final String USE_CENTRALIZED_PACKAGE_VERSION_MANAGEMENT = "useCentralizedPackageVersionManagement";
public static final String CENTRALIZED_PACKAGE_VERSION_MANAGEMENT_OPT_OUT = "centralizedPackageVersionManagementOptOut";

@Setter private String packageGuid = "{" + randomUUID().toString().toUpperCase(Locale.ROOT) + "}";
@Setter
private String packageGuid = "{" + randomUUID().toString().toUpperCase(Locale.ROOT) + "}";
private String userSecretsGuid = randomUUID().toString();

protected final Logger LOGGER = LoggerFactory.getLogger(AspNetServerCodegen.class);
Expand All @@ -91,6 +94,8 @@ public class AspNetServerCodegen extends AbstractCSharpCodegen {
private boolean useNewtonsoft = true;
private boolean useDefaultRouting = true;
private String newtonsoftVersion = "3.0.0";
private boolean useCentralizedPackageVersionManagement = false;
private boolean centralizedPackageVersionManagementOptOut = false;

public AspNetServerCodegen() {
super();
Expand Down Expand Up @@ -249,6 +254,14 @@ public AspNetServerCodegen() {
"Uses the Newtonsoft JSON library.",
useNewtonsoft);

addSwitch(USE_CENTRALIZED_PACKAGE_VERSION_MANAGEMENT,
"Uses centralized package version management. https://devblogs.microsoft.com/nuget/introducing-central-package-management/#disabling-central-package-management",
useCentralizedPackageVersionManagement);

addSwitch(CENTRALIZED_PACKAGE_VERSION_MANAGEMENT_OPT_OUT,
"Opt out of centralized package version management. Set this if you have a Directory.Packages.pros file but want this project to ignore it. https://devblogs.microsoft.com/nuget/introducing-central-package-management/#disabling-central-package-management",
centralizedPackageVersionManagementOptOut);

addOption(NEWTONSOFT_VERSION,
"Version for Microsoft.AspNetCore.Mvc.NewtonsoftJson for ASP.NET Core 3.0+",
newtonsoftVersion);
Expand Down Expand Up @@ -307,7 +320,7 @@ public AspNetServerCodegen() {
@Override
protected Set<String> getNullableTypes() {
return new HashSet<>(Arrays.asList("decimal", "bool", "int", "uint", "long", "ulong", "float", "double",
"DateTime", "DateOnly", "DateTimeOffset", "Guid"));
"DateTime", "DateOnly", "DateTimeOffset", "Guid"));
}

@Override
Expand Down Expand Up @@ -378,6 +391,8 @@ public void processOpts() {
newtonsoftVersion = (String) additionalProperties.get(NEWTONSOFT_VERSION);
}

setCentralizedPackageVersionManagementSettings();

// Check for the modifiers etc.
// The order of the checks is important.
setBuildTarget();
Expand Down Expand Up @@ -680,6 +695,19 @@ private void setBuildTarget() {
additionalProperties.put(IS_LIBRARY, isLibrary);
}

/**
* This method sets both the USE_CENTRALIZED_PACKAGE_VERSION_MANAGEMENT and CENTRALIZED_PACKAGE_VERSION_MANAGEMENT_OPT_OUT setting
*/
private void setCentralizedPackageVersionManagementSettings() {

additionalProperties.putIfAbsent(CENTRALIZED_PACKAGE_VERSION_MANAGEMENT_OPT_OUT, centralizedPackageVersionManagementOptOut);

// If opt out is true then the package versions should always be added regardless of USE_CENTRALIZED_PACKAGE_VERSION_MANAGEMENT value
if ((boolean) additionalProperties.get(CENTRALIZED_PACKAGE_VERSION_MANAGEMENT_OPT_OUT) && additionalProperties.containsKey(USE_CENTRALIZED_PACKAGE_VERSION_MANAGEMENT)) {
additionalProperties.replace(USE_CENTRALIZED_PACKAGE_VERSION_MANAGEMENT, false);
}
}

private void setAspnetCoreVersion(String packageFolder) {
setCliOption(aspnetCoreVersion);

Expand Down Expand Up @@ -808,10 +836,10 @@ private void setIsFramework() {
}

private void setAddititonalPropertyForFramework() {
String targetFramework = ((String)additionalProperties.get(TARGET_FRAMEWORK));
String targetFramework = ((String) additionalProperties.get(TARGET_FRAMEWORK));
if (targetFramework.startsWith("net6.0") ||
targetFramework.startsWith("net7.0") ||
targetFramework.startsWith("net8.0")) {
targetFramework.startsWith("net7.0") ||
targetFramework.startsWith("net8.0")) {
additionalProperties.put(NET_60_OR_LATER, true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,32 @@
<OutputType>Library</OutputType>
<AssemblyName>{{modelPackage}}</AssemblyName>
<PackageId>{{modelPackage}}</PackageId>
{{#useCentralizedPackageVersionManagement}}
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
{{/useCentralizedPackageVersionManagement}}
{{#centralizedPackageVersionManagementOptOut}}
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
{{/centralizedPackageVersionManagementOptOut}}

</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="{{aspnetCoreVersion}}.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" {{^useCentralizedPackageVersionManagement}}Version="{{aspnetCoreVersion}}.0" {{/useCentralizedPackageVersionManagement}}/>
{{#useSwashbuckle}}
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.8" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" {{^useCentralizedPackageVersionManagement}}Version="1.10.8" {{/useCentralizedPackageVersionManagement}}/>
{{#useNewtonsoft}}
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="{{swashbuckleVersion}}"/>
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" {{^useCentralizedPackageVersionManagement}}Version="{{swashbuckleVersion}}" {{/useCentralizedPackageVersionManagement}}/>
{{/useNewtonsoft}}
{{^useNewtonsoft}}
<PackageReference Include="Swashbuckle.AspNetCore" Version="{{swashbuckleVersion}}"/>
<PackageReference Include="Swashbuckle.AspNetCore" {{^useCentralizedPackageVersionManagement}}Version="{{swashbuckleVersion}}" {{/useCentralizedPackageVersionManagement}}/>
{{/useNewtonsoft}}
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="{{swashbuckleVersion}}" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" {{^useCentralizedPackageVersionManagement}}Version="{{swashbuckleVersion}}" {{/useCentralizedPackageVersionManagement}}/>
{{/useSwashbuckle}}
{{^useSwashbuckle}}
{{#useNewtonsoft}}
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="{{newtonsoftVersion}}" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" {{^useCentralizedPackageVersionManagement}}Version="{{newtonsoftVersion}}" {{/useCentralizedPackageVersionManagement}}/>
{{/useNewtonsoft}}
{{/useSwashbuckle}}
<PackageReference Include="JsonSubTypes" Version="1.8.0" />
<PackageReference Include="JsonSubTypes" {{^useCentralizedPackageVersionManagement}}Version="1.8.0" {{/useCentralizedPackageVersionManagement}}useCentralizedPackageVersionManagement}}/>
</ItemGroup>
<ItemGroup>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,58 +1,64 @@
<Project Sdk="{{projectSdk}}">
<PropertyGroup>
<Description>{{packageDescription}}{{^packageDescription}}{{packageName}}{{/packageDescription}}</Description>
<Copyright>{{packageCopyright}}</Copyright>
<Authors>{{packageAuthors}}</Authors>
<TargetFramework>{{targetFramework}}</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PreserveCompilationContext>true</PreserveCompilationContext>
<Version>{{packageVersion}}</Version>
{{#nullableReferenceTypes}}
<Nullable>annotations</Nullable>
{{/nullableReferenceTypes}}
{{#isLibrary}}
<OutputType>Library</OutputType>
{{/isLibrary}}
<AssemblyName>{{packageName}}</AssemblyName>
<PackageId>{{packageName}}</PackageId>
<UserSecretsId>{{userSecretsGuid}}</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..</DockerfileContext>
</PropertyGroup>
<ItemGroup>
{{#useSeparateModelProject}}
<ProjectReference Include="../{{modelPackage}}/{{modelPackage}}.csproj"/>
{{/useSeparateModelProject}}
{{#useFrameworkReference}}
{{#isLibrary}}
<FrameworkReference Include="Microsoft.AspNetCore.App" />
{{/isLibrary}}
{{/useFrameworkReference}}
{{^useFrameworkReference}}
<PackageReference Include="Microsoft.AspNetCore.App" />
{{/useFrameworkReference}}
{{^useSeparateModelProject}}
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="{{aspnetCoreVersion}}.0"/>
{{/useSeparateModelProject}}
{{#useSwashbuckle}}
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.8" />
{{#useNewtonsoft}}
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="{{swashbuckleVersion}}"/>
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="{{swashbuckleVersion}}"/>
{{/useNewtonsoft}}
{{^useNewtonsoft}}
<PackageReference Include="Swashbuckle.AspNetCore" Version="{{swashbuckleVersion}}"/>
{{/useNewtonsoft}}
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="{{swashbuckleVersion}}" />
{{/useSwashbuckle}}
{{^useSwashbuckle}}
{{#useNewtonsoft}}
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="{{newtonsoftVersion}}" />
{{/useNewtonsoft}}
{{/useSwashbuckle}}
<PackageReference Include="JsonSubTypes" Version="1.8.0" />
</ItemGroup>
<ItemGroup>
<!--<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="{{aspnetCoreVersion}}.0" />-->
</ItemGroup>
</Project>
<PropertyGroup>
<Description>{{packageDescription}}{{^packageDescription}}{{packageName}}{{/packageDescription}}</Description>
<Copyright>{{packageCopyright}}</Copyright>
<Authors>{{packageAuthors}}</Authors>
<TargetFramework>{{targetFramework}}</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PreserveCompilationContext>true</PreserveCompilationContext>
<Version>{{packageVersion}}</Version>
{{#nullableReferenceTypes}}
<Nullable>annotations</Nullable>
{{/nullableReferenceTypes}}
{{#isLibrary}}
<OutputType>Library</OutputType>
{{/isLibrary}}
<AssemblyName>{{packageName}}</AssemblyName>
<PackageId>{{packageName}}</PackageId>
<UserSecretsId>{{userSecretsGuid}}</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..</DockerfileContext>
{{#useCentralizedPackageVersionManagement}}
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
{{/useCentralizedPackageVersionManagement}}
{{#centralizedPackageVersionManagementOptOut}}
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
{{/centralizedPackageVersionManagementOptOut}}
</PropertyGroup>
<ItemGroup>
{{#useSeparateModelProject}}
<ProjectReference Include="../{{modelPackage}}/{{modelPackage}}.csproj"/>
{{/useSeparateModelProject}}
{{#useFrameworkReference}}
{{#isLibrary}}
<FrameworkReference Include="Microsoft.AspNetCore.App" />
{{/isLibrary}}
{{/useFrameworkReference}}
{{^useFrameworkReference}}
<PackageReference Include="Microsoft.AspNetCore.App" />
{{/useFrameworkReference}}
{{^useSeparateModelProject}}
<PackageReference Include="Microsoft.Extensions.Configuration.Json" {{^useCentralizedPackageVersionManagement}}Version="{{aspnetCoreVersion}}.0" {{/useCentralizedPackageVersionManagement}}/>
{{/useSeparateModelProject}}
{{#useSwashbuckle}}
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" {{^useCentralizedPackageVersionManagement}}Version="1.10.8" {{/useCentralizedPackageVersionManagement}}/>
{{#useNewtonsoft}}
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" {{^useCentralizedPackageVersionManagement}}Version="{{swashbuckleVersion}}" {{/useCentralizedPackageVersionManagement}}/>
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" {{^useCentralizedPackageVersionManagement}}Version="{{swashbuckleVersion}}" {{/useCentralizedPackageVersionManagement}}/>
{{/useNewtonsoft}}
{{^useNewtonsoft}}
<PackageReference Include="Swashbuckle.AspNetCore" {{^useCentralizedPackageVersionManagement}}Version="{{swashbuckleVersion}}" {{/useCentralizedPackageVersionManagement}}/>
{{/useNewtonsoft}}
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" {{^useCentralizedPackageVersionManagement}}Version="{{swashbuckleVersion}}" {{/useCentralizedPackageVersionManagement}}/>
{{/useSwashbuckle}}
{{^useSwashbuckle}}
{{#useNewtonsoft}}
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" {{^useCentralizedPackageVersionManagement}}Version="{{newtonsoftVersion}}" {{/useCentralizedPackageVersionManagement}}/>
{{/useNewtonsoft}}
{{/useSwashbuckle}}
<PackageReference Include="JsonSubTypes" {{^useCentralizedPackageVersionManagement}}Version="1.8.0" {{/useCentralizedPackageVersionManagement}}/>
</ItemGroup>
<ItemGroup>
<!--<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="{{aspnetCoreVersion}}.0" />-->
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Org.OpenAPITools.sln
README.md
build.bat
build.sh
src/Org.OpenAPITools/.gitignore
src/Org.OpenAPITools/Attributes/ValidateModelStateAttribute.cs
src/Org.OpenAPITools/Authentication/ApiAuthentication.cs
src/Org.OpenAPITools/Controllers/DefaultApi.cs
src/Org.OpenAPITools/Controllers/FakeApi.cs
src/Org.OpenAPITools/Controllers/PetApi.cs
src/Org.OpenAPITools/Controllers/StoreApi.cs
src/Org.OpenAPITools/Controllers/UserApi.cs
src/Org.OpenAPITools/Converters/CustomEnumConverter.cs
src/Org.OpenAPITools/Dockerfile
src/Org.OpenAPITools/Filters/BasePathFilter.cs
src/Org.OpenAPITools/Filters/GeneratePathParamsValidationFilter.cs
src/Org.OpenAPITools/Formatters/InputFormatterStream.cs
src/Org.OpenAPITools/Models/Animal.cs
src/Org.OpenAPITools/Models/ApiResponse.cs
src/Org.OpenAPITools/Models/Cat.cs
src/Org.OpenAPITools/Models/Category.cs
src/Org.OpenAPITools/Models/Dog.cs
src/Org.OpenAPITools/Models/Order.cs
src/Org.OpenAPITools/Models/Pet.cs
src/Org.OpenAPITools/Models/Tag.cs
src/Org.OpenAPITools/Models/TestEnum.cs
src/Org.OpenAPITools/Models/TestNullable.cs
src/Org.OpenAPITools/Models/User.cs
src/Org.OpenAPITools/OpenApi/TypeExtensions.cs
src/Org.OpenAPITools/Org.OpenAPITools.csproj
src/Org.OpenAPITools/Program.cs
src/Org.OpenAPITools/Properties/launchSettings.json
src/Org.OpenAPITools/Startup.cs
src/Org.OpenAPITools/appsettings.Development.json
src/Org.OpenAPITools/appsettings.json
src/Org.OpenAPITools/wwwroot/README.md
src/Org.OpenAPITools/wwwroot/index.html
src/Org.OpenAPITools/wwwroot/openapi-original.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.11.0-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27428.2043
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Org.OpenAPITools", "src\Org.OpenAPITools\Org.OpenAPITools.csproj", "{3C799344-F285-4669-8FD5-7ED9B795D5C5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3C799344-F285-4669-8FD5-7ED9B795D5C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3C799344-F285-4669-8FD5-7ED9B795D5C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C799344-F285-4669-8FD5-7ED9B795D5C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C799344-F285-4669-8FD5-7ED9B795D5C5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Loading