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

feat(csharp): support skipResponseValidation config in c# #5994

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

dsinghvi
Copy link
Member

Description

Add new configuration skipResponseValidation to skip response validation. When set to true, the generated client will fail fast if the response schema does not match the generated code. This is particularly useful if you are concerned about your API Definition (e.g. OpenAPI, AsyncAPI) not being entirely correct.

- name: fernapi/fern-csharp-sdk
  version: 1.9.22
  config: 
    skip-response-validation: true

Changes Made

  • Updated JsonUtils.Template.cs
  • Added a new method to the SDKGeneratorContext that returns Deserialize or DeserializeWithFallbacks depending on the feature flag

Testing

  • Unit tests added/updated
  • Manual testing completed

@dsinghvi dsinghvi marked this pull request as ready for review February 13, 2025 23:55
Copy link

github-actions bot commented Feb 14, 2025

Comment on lines +22 to +38
JsonSerializerOptionsWithFallback = new JsonSerializerOptions(options)
{
TypeInfoResolver = new DefaultJsonTypeInfoResolver
{
Modifiers =
{
static typeInfo =>
{
foreach (var propertyInfo in typeInfo.Properties)
{
// Strip IsRequired constraint from every property.
propertyInfo.IsRequired = false;
}
}
}
}
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ConfigureJsonSerializerOptions method is an extension point for SDK maintainers so they can customize some of the options.
I think that this would prevent them from modifying the type info resolvers and modifiers like we are doing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah thats fair, i'm not sure how valuable it is to keep that extension method

@Swimburger
Copy link
Member

I'd rather see a single Serialize/Deserialize method. Is there a reason we would surface a validating and non-validating variant at the same time?

@dsinghvi
Copy link
Member Author

I'd rather see a single Serialize/Deserialize method. Is there a reason we would surface a validating and non-validating variant at the same time?

Yes -- we would surface it so that SDK users can decide how they want validation to work. For example, internal consumers of the SDK would turn validation on and external ones would keep it off.

@@ -39,4 +58,9 @@ public static T Deserialize<T>(string json)
{
return JsonSerializer.Deserialize<T>(json, JsonOptions.JsonSerializerOptions)!;
}

public static T DeserializeWithFallback<T>(string json)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

swap out public -> internal

@@ -6,6 +6,7 @@ namespace <%= namespace%>;
internal static partial class JsonOptions
{
public static readonly JsonSerializerOptions JsonSerializerOptions;
public static readonly JsonSerializerOptions JsonSerializerOptionsWithFallback;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it internal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants