-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
The generated nswag contract from NSwag v14.0.0-preview009 failed to build #4572
Comments
I have the same problem, is there any workaround? |
Same issue. Interestingly I only discovered it because it generated the following invalid code. Yes it's duplicates some enum values and not put a
|
I couldn't find any workable solution right now other than to switch to using
By default in v14.0 it uses the
This allowed me to get back to where I was before in .NET Core 7. Be careful to check for places where you have |
I parked this issue for a while until now I am back to look at this again. |
Tried with the version: 14.0.3, still having same issue |
@RanjithkumarRajendran Unfortunately, I have not find a workable solution yet |
I was able to work around it by adding the following to my NSwagStudio configuration. I'm not sure why NSwag was trying to generate code for a bunch of built-in reflection types, but here we are... "codeGenerators": {
"excludedTypeNames": [
"MemberInfo",
"EventInfo",
"Anonymous",
"TypeInfo",
"MethodBase",
"RuntimeMethodHandle",
"IntPtr",
"MethodAttributes",
"MethodImplAttributes",
"CallingConventions",
"MemberTypes",
"Module",
"Assembly",
"ConstructorInfo",
"EventAttributes",
"MethodInfo",
"ParameterInfo",
"ParameterAttributes",
"CustomAttributeData",
"CustomAttributeTypedArgument",
"PropertyInfo",
"FieldAttributes",
"RuntimeFieldHandle",
"PropertyAttributes",
"SecurityRuleSet",
"ModuleHandle",
"FieldInfo",
"ICustomAttributeProvider",
"CustomAttributeNamedArgument"
], I, also, had to correct some of the generated code at the end of the client file. The commented-out code and the line beneath it are the key. The rest is provided for context. private void SetExceptionFieldValue(Newtonsoft.Json.Linq.JObject jObject, string propertyName, object value, string fieldName, Newtonsoft.Json.Serialization.IContractResolver resolver, Newtonsoft.Json.JsonSerializer serializer)
{
var field = System.Reflection.IntrospectionExtensions.GetTypeInfo(typeof(System.Exception)).GetDeclaredField(fieldName);
var jsonPropertyName = resolver is Newtonsoft.Json.Serialization.DefaultContractResolver ? ((Newtonsoft.Json.Serialization.DefaultContractResolver)resolver).GetResolvedPropertyName(propertyName) : propertyName;
// Workaround due to bug in NSwagStudio 14.0.3.0
// foreach (var p in jObject.Properties())
foreach (var property in jObject.Properties())
{
if (System.String.Equals(property.Name, jsonPropertyName, System.StringComparison.OrdinalIgnoreCase))
{
var fieldValue = property.Value.ToObject(field.FieldType, serializer);
field.SetValue(value, fieldValue);
break;
}
}
} |
On further investigation, it looks like NSwagStudio is trying to generate these types because NSwag.AspNetCore is generating an OpenAPI spec with an endpoint that uses the UPDATE: |
Generated the c# contract from the NSwagStudio (v14.0.00) based on the API spec from NSwag v14.0.0-preview009.
The generated contracts code failed to compile due to the below issues:
(1) new generated partial class
IntPtr
confused the complierIssue message: The call is ambitious between the following methods or properties.
Screen shot:
It looks like it got confused with the new introduced class
IntPtr
at the "contracts.nswag.cs"(2) Several new properties in the new class
IntPtr
errorsIssue message: Attribute 'xxx' is not valid on this declaration type. It is only valid on 'property, indexer' declarations.
Screen shot:
Update: It looks like the problem is caused by difference with the swagger json generated between the old version and the newer version:
Using the older version of Nswag( NSwag.AspNetCore version: 13.18.0 and NSwag.MSBuild version: 13.15.0)
The swagger json has the below section:
Whereas using the newer version of Nswag (NSwag.AspNetCore version: 14.0.0-preview009 and NSwag.MSBuild version: 14.0.0-preview009)
The swagger json has added extra bit required fields for the "Exception" and also have extra new models, such as "MethodBase", "MethodAttributes" and ect which are the public properties of the class "System.Exception":
.....
When I tried to use the contracts that generated by the new version of Nswag, these extra class models have been conflicting the same class models from "system.namespace", which caused the project failed to build. I was expecting the models would stay unchanged because my code stays same.
The text was updated successfully, but these errors were encountered: