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 #20401

Merged
merged 21 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from 19 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
1 change: 1 addition & 0 deletions .github/workflows/samples-dotnet8-server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
- samples/server/petstore/aspnetcore-8.0-pocoModels
- samples/server/petstore/aspnetcore-8.0-project4Models
- samples/server/petstore/aspnetcore-8.0-useSwashBuckle
- samples/server/petstore/aspnetcore-8.0-use-centralized-package-version-management
steps:
- uses: actions/checkout@v4
- uses: actions/[email protected]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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: "8.0"
userSecretsGuid: 'cb87e868-8646-48ef-9bb6-344b537d0d37'
centralizedPackageVersionManagement: "enable"
1 change: 1 addition & 0 deletions docs/generators/aspnetcore.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
| ------ | ----------- | ------ | ------- |
|aspnetCoreVersion|ASP.NET Core version: 6.0, 5.0, 3.1, 3.0, 2.2, 2.1, 2.0 (deprecated)|<dl><dt>**2.0**</dt><dd>ASP.NET Core 2.0</dd><dt>**2.1**</dt><dd>ASP.NET Core 2.1</dd><dt>**2.2**</dt><dd>ASP.NET Core 2.2</dd><dt>**3.0**</dt><dd>ASP.NET Core 3.0</dd><dt>**3.1**</dt><dd>ASP.NET Core 3.1</dd><dt>**5.0**</dt><dd>ASP.NET Core 5.0</dd><dt>**6.0**</dt><dd>ASP.NET Core 6.0</dd><dt>**7.0**</dt><dd>ASP.NET Core 7.0</dd><dt>**8.0**</dt><dd>ASP.NET Core 8.0</dd></dl>|8.0|
|buildTarget|Target to build an application or library|<dl><dt>**program**</dt><dd>Generate code for a standalone server</dd><dt>**library**</dt><dd>Generate code for a server abstract class library</dd></dl>|program|
|centralizedPackageVersionManagement|Option to control the usage of centralized package version management. https://devblogs.microsoft.com/nuget/introducing-central-package-management/#disabling-central-package-management|<dl><dt>**optout**</dt><dd>Opt out of centralized package version management. Set this if you have a Directory.Packages.pros file but want this project to ignore it.</dd><dt>**default**</dt><dd>Property in project won't be used</dd><dt>**enable**</dt><dd>Centralized package version management will be used</dd></dl>|null|
|classModifier|Class Modifier for controller classes: Empty string or abstract.| ||
|compatibilityVersion|ASP.Net Core CompatibilityVersion| |Version_2_2|
|enumNameSuffix|Suffix that will be appended to all enum names.| |Enum|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.parser.util.SchemaTypeUtil;
import lombok.Getter;
import lombok.Setter;
import org.openapitools.codegen.*;
import org.openapitools.codegen.meta.features.*;
Expand Down Expand Up @@ -64,8 +65,14 @@ 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";

@Setter private String packageGuid = "{" + randomUUID().toString().toUpperCase(Locale.ROOT) + "}";
public static final String CENTRALIZED_PACKAGE_VERSION_MANAGEMENT = "centralizedPackageVersionManagement";
public static final String USE_PACKAGE_VERSIONS = "usePackageVersions";
public static final String DEFAULT = "default";
public static final String ENABLE = "enable";
public static final String OPTOUT = "optout";

@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 +98,7 @@ public class AspNetServerCodegen extends AbstractCSharpCodegen {
private boolean useNewtonsoft = true;
private boolean useDefaultRouting = true;
private String newtonsoftVersion = "3.0.0";
private CliOption centralizedPackageVersionManagement = new CliOption(CENTRALIZED_PACKAGE_VERSION_MANAGEMENT, "Option to control the usage of centralized package version management. https://devblogs.microsoft.com/nuget/introducing-central-package-management/#disabling-central-package-management");

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


addOption(NEWTONSOFT_VERSION,
"Version for Microsoft.AspNetCore.Mvc.NewtonsoftJson for ASP.NET Core 3.0+",
newtonsoftVersion);
Expand Down Expand Up @@ -301,13 +310,24 @@ public AspNetServerCodegen() {
modelClassModifier.setDefault("partial");
modelClassModifier.setOptValue(modelClassModifier.getDefault());
addOption(modelClassModifier.getOpt(), modelClassModifier.getDescription(), modelClassModifier.getOptValue());

addCentralizedPackageManagementOption();
}

private void addCentralizedPackageManagementOption(){
Map<String, String> centralizedPackageVersionManagementOptions = new HashMap<>();
centralizedPackageVersionManagementOptions.put(DEFAULT, "Property in project won't be used");
centralizedPackageVersionManagementOptions.put(ENABLE, "Centralized package version management will be used");
centralizedPackageVersionManagementOptions.put(OPTOUT, "Opt out of centralized package version management. Set this if you have a Directory.Packages.pros file but want this project to ignore it.");
centralizedPackageVersionManagement.setEnum(centralizedPackageVersionManagementOptions);
cliOptions.add(centralizedPackageVersionManagement);
}

@Deprecated
@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 @@ -463,6 +483,30 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("Formatters" + File.separator + "InputFormatterStream.mustache", packageFolder + File.separator + "Formatters", "InputFormatterStream.cs"));

this.setTypeMapping();


setCentralizedPackageManagementOption();
}

private void setCentralizedPackageManagementOption() {
additionalProperties.put(USE_PACKAGE_VERSIONS, true);

if (additionalProperties.containsKey(CENTRALIZED_PACKAGE_VERSION_MANAGEMENT)) {
switch ((String) additionalProperties.get(CENTRALIZED_PACKAGE_VERSION_MANAGEMENT)) {
case DEFAULT:
additionalProperties.remove(CENTRALIZED_PACKAGE_VERSION_MANAGEMENT);
break;
case ENABLE:
additionalProperties.replace(CENTRALIZED_PACKAGE_VERSION_MANAGEMENT, "true");
additionalProperties.put(USE_PACKAGE_VERSIONS, false);
break;
case OPTOUT:
additionalProperties.replace(CENTRALIZED_PACKAGE_VERSION_MANAGEMENT, "false");
break;
default:
throw new RuntimeException("Invalid value `" + additionalProperties.get(CENTRALIZED_PACKAGE_VERSION_MANAGEMENT) + "` for the option `centralizedPackageVersionManagement`. Please refer to the documentation for more information.");
}
}
}

@Override
Expand Down Expand Up @@ -808,10 +852,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}}/>
</ItemGroup>
<ItemGroup>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,58 +1,61 @@
<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>
{{#centralizedPackageVersionManagement}}
<ManagePackageVersionsCentrally>{{.}}</ManagePackageVersionsCentrally>
{{/centralizedPackageVersionManagement}}
</PropertyGroup>
<ItemGroup>
{{#useSeparateModelProject}}
<ProjectReference Include="../{{modelPackage}}/{{modelPackage}}.csproj"/>
wing328 marked this conversation as resolved.
Show resolved Hide resolved
{{/useSeparateModelProject}}
{{#useFrameworkReference}}
{{#isLibrary}}
<FrameworkReference Include="Microsoft.AspNetCore.App" />
{{/isLibrary}}
{{/useFrameworkReference}}
{{^useFrameworkReference}}
<PackageReference Include="Microsoft.AspNetCore.App" />
{{/useFrameworkReference}}
{{^useSeparateModelProject}}
<PackageReference Include="Microsoft.Extensions.Configuration.Json" {{#usePackageVersions}}Version="{{aspnetCoreVersion}}.0" {{/usePackageVersions}}/>
{{/useSeparateModelProject}}
{{#useSwashbuckle}}
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" {{#usePackageVersions}}Version="1.10.8" {{/usePackageVersions}}/>
{{#useNewtonsoft}}
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" {{#usePackageVersions}}Version="{{swashbuckleVersion}}" {{/usePackageVersions}}/>
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" {{#usePackageVersions}}Version="{{swashbuckleVersion}}" {{/usePackageVersions}}/>
{{/useNewtonsoft}}
{{^useNewtonsoft}}
<PackageReference Include="Swashbuckle.AspNetCore" {{#usePackageVersions}}Version="{{swashbuckleVersion}}" {{/usePackageVersions}}/>
{{/useNewtonsoft}}
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" {{#usePackageVersions}}Version="{{swashbuckleVersion}}" {{/usePackageVersions}}/>
{{/useSwashbuckle}}
{{^useSwashbuckle}}
{{#useNewtonsoft}}
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" {{#usePackageVersions}}Version="{{newtonsoftVersion}}" {{/usePackageVersions}}/>
{{/useNewtonsoft}}
{{/useSwashbuckle}}
<PackageReference Include="JsonSubTypes" {{#usePackageVersions}}Version="1.8.0" {{/usePackageVersions}}/>
</ItemGroup>
<ItemGroup>
<!--<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="{{aspnetCoreVersion}}.0" />-->
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<Description>A library generated from a OpenAPI doc</Description>
<Copyright>No Copyright</Copyright>
<Authors>OpenAPI</Authors>
<TargetFramework>net6.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PreserveCompilationContext>true</PreserveCompilationContext>
<Version>1.0.0</Version>
<AssemblyName>Org.OpenAPITools</AssemblyName>
<PackageId>Org.OpenAPITools</PackageId>
<UserSecretsId>cb87e868-8646-48ef-9bb6-344b537d0d37</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..</DockerfileContext>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0"/>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.8" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.4.0" />
<PackageReference Include="JsonSubTypes" Version="1.8.0" />
</ItemGroup>
<ItemGroup>
<!--<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.0" />-->
</ItemGroup>
</Project>
<PropertyGroup>
<Description>A library generated from a OpenAPI doc</Description>
<Copyright>No Copyright</Copyright>
<Authors>OpenAPI</Authors>
<TargetFramework>net6.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PreserveCompilationContext>true</PreserveCompilationContext>
<Version>1.0.0</Version>
<AssemblyName>Org.OpenAPITools</AssemblyName>
<PackageId>Org.OpenAPITools</PackageId>
<UserSecretsId>cb87e868-8646-48ef-9bb6-344b537d0d37</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..</DockerfileContext>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.8" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.4.0" />
<PackageReference Include="JsonSubTypes" Version="1.8.0" />
</ItemGroup>
<ItemGroup>
<!--<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.0" />-->
</ItemGroup>
</Project>
Loading
Loading