Skip to content

Commit

Permalink
Fix issue #2057 related to readonly properties set via constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
rmorris committed Mar 16, 2021
1 parent 68d7ef8 commit a6eda1a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 24 deletions.
14 changes: 7 additions & 7 deletions README.md
Expand Up @@ -22,7 +22,7 @@ Once you have an API that can describe itself in Swagger, you've opened the trea
|Swashbuckle Version|ASP.NET Core|Swagger / OpenAPI Spec.|swagger-ui|ReDoc UI|
|----------|----------|----------|----------|----------|
|[master](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/tree/master/README.md)|>= 2.0.0|2.0, 3.0|3.42.0|2.0.0-rc.40|
|[6.1.0](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/tree/v6.1.0)|>= 2.0.0|2.0, 3.0|3.42.0|2.0.0-rc.40|
|[6.1.1](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/tree/v6.1.1)|>= 2.0.0|2.0, 3.0|3.42.0|2.0.0-rc.40|
|[5.6.3](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/tree/v5.6.3)|>= 2.0.0|2.0, 3.0|3.32.5|2.0.0-rc.40|
|[4.0.0](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/tree/v4.0.0)|>= 2.0.0, < 3.0.0|2.0|3.19.5|1.22.2|
|[3.0.0](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/tree/v3.0.0)|>= 1.0.4, < 3.0.0|2.0|3.17.1|1.20.0|
Expand All @@ -33,8 +33,8 @@ Once you have an API that can describe itself in Swagger, you've opened the trea
1. Install the standard Nuget package into your ASP.NET Core application.

```
Package Manager : Install-Package Swashbuckle.AspNetCore -Version 6.1.0
CLI : dotnet add package --version 6.1.0 Swashbuckle.AspNetCore
Package Manager : Install-Package Swashbuckle.AspNetCore -Version 6.1.1
CLI : dotnet add package --version 6.1.1 Swashbuckle.AspNetCore
```

2. In the `ConfigureServices` method of `Startup.cs`, register the Swagger generator, defining one or more Swagger documents.
Expand Down Expand Up @@ -98,8 +98,8 @@ If you're using **System.Text.Json (STJ)**, then the setup described above will
If you're using **Newtonsoft**, then you'll need to install a separate package and explicitly opt-in to ensure that *Newtonsoft* settings/attributes are automatically honored by the Swagger generator:

```
Package Manager : Install-Package Swashbuckle.AspNetCore.Newtonsoft -Version 6.1.0
CLI : dotnet add package --version 6.1.0 Swashbuckle.AspNetCore.Newtonsoft
Package Manager : Install-Package Swashbuckle.AspNetCore.Newtonsoft -Version 6.1.1
CLI : dotnet add package --version 6.1.1 Swashbuckle.AspNetCore.Newtonsoft
```

```csharp
Expand Down Expand Up @@ -1495,7 +1495,7 @@ It's packaged as a [.NET Core Tool](https://docs.microsoft.com/en-us/dotnet/core
1. Install as a [global tool](https://docs.microsoft.com/en-us/dotnet/core/tools/global-tools#install-a-global-tool)
```
dotnet tool install -g --version 6.1.0 Swashbuckle.AspNetCore.Cli
dotnet tool install -g --version 6.1.1 Swashbuckle.AspNetCore.Cli
```

2. Verify that the tool was installed correctly
Expand Down Expand Up @@ -1526,7 +1526,7 @@ It's packaged as a [.NET Core Tool](https://docs.microsoft.com/en-us/dotnet/core
2. Install as a [local tool](https://docs.microsoft.com/en-us/dotnet/core/tools/global-tools#install-a-local-tool)
```
dotnet tool install --version 6.1.0 Swashbuckle.AspNetCore.Cli
dotnet tool install --version 6.1.1 Swashbuckle.AspNetCore.Cli
```

3. Verify that the tool was installed correctly
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Expand Up @@ -6,7 +6,7 @@
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<LicenseUrl>https://licenses.nuget.org/MIT</LicenseUrl>
<VersionPrefix>6.1.0</VersionPrefix>
<VersionPrefix>6.1.1</VersionPrefix>
</PropertyGroup>

<ItemGroup>
Expand Down
Expand Up @@ -146,7 +146,13 @@ private IEnumerable<DataProperty> GetDataPropertiesFor(JsonObjectContract jsonOb

var isSetViaConstructor = jsonProperty.DeclaringType != null && jsonProperty.DeclaringType.GetConstructors()
.SelectMany(c => c.GetParameters())
.Any(p => string.Equals(p.Name, jsonProperty.PropertyName, StringComparison.OrdinalIgnoreCase));
.Any(p =>
{
// Newtonsoft supports setting via constructor if either underlying OR JSON names match
return
string.Equals(p.Name, jsonProperty.UnderlyingName, StringComparison.OrdinalIgnoreCase) ||
string.Equals(p.Name, jsonProperty.PropertyName, StringComparison.OrdinalIgnoreCase);
});

jsonProperty.TryGetMemberInfo(out MemberInfo memberInfo);

Expand Down
@@ -0,0 +1,18 @@
using Newtonsoft.Json;

namespace Swashbuckle.AspNetCore.Newtonsoft.Test
{
public class TypeWithPropertiesSetViaConstructor
{
public TypeWithPropertiesSetViaConstructor(int id, string description)
{
Id = id;
Description = description;
}

public int Id { get; }

[JsonProperty("Desc")]
public string Description { get; }
}
}
Expand Up @@ -327,14 +327,15 @@ public void GenerateSchema_SetsReadOnlyAndWriteOnlyFlags_IfPropertyIsRestricted(
}

[Fact]
public void GenerateSchema_DoesNotSetReadOnlyFlag_IfPropertyIsReadOnlyButSetViaConstructor()
public void GenerateSchema_DoesNotSetReadOnlyFlag_IfPropertyIsReadOnlyButCanBeSetViaConstructor()
{
var schemaRepository = new SchemaRepository();

var referenceSchema = Subject().GenerateSchema(typeof(TypeWithPropertySetViaConstructor), schemaRepository);
var referenceSchema = Subject().GenerateSchema(typeof(TypeWithPropertiesSetViaConstructor), schemaRepository);

var schema = schemaRepository.Schemas[referenceSchema.Reference.Id];
Assert.False(schema.Properties["ReadOnlyProperty"].ReadOnly);
Assert.False(schema.Properties["Id"].ReadOnly);
Assert.False(schema.Properties["Desc"].ReadOnly);
}

[Theory]
Expand Down

This file was deleted.

0 comments on commit a6eda1a

Please sign in to comment.