Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add JsonSchema support to fastify plugin. #60

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

Merged
merged 4 commits into from
Jan 2, 2024

Conversation

mitchknife
Copy link
Contributor

https://fastify.dev/docs/latest/Reference/Validation-and-Serialization/#serialization

I opted to only handle response schemas since (from my understanding) that is where most of the performance gain is at using fast-json-stringify. The request schemas are mostly for validation, which Facility can already take care of for us.

https://fastify.dev/docs/latest/Reference/Validation-and-Serialization/#serialization

I opted to only handle response schemas since (from my understanding) that is where most of the performance gain is at using `fast-json-stringify`. The request schemas are mostly for validation, which Facility can already take care of for us.

if (response.BodyField is not null)
{
code.WriteLine($"{GetJsonSchemaType(response.BodyField.ServiceField.TypeName)},");
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
code.WriteLine($"{GetJsonSchemaType(response.BodyField.ServiceField.TypeName)},");
code.WriteLine($"{GetJsonSchemaType(service.GetFieldType(response.BodyField.ServiceField))},");

This would be a more typical implementation, which would allow GetJsonSchemaType to switch on fieldType.Kind instead of reverse-engineering the type name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You beat me to it! I was going to ask if there was a better way, which I assumed there was. πŸ‘

@@ -543,6 +543,7 @@ private CodeGenOutput GenerateFastifyPluginOutput(ServiceInfo service)
var capModuleName = CodeGenUtility.Capitalize(moduleName);
var camelCaseModuleName = CodeGenUtility.ToCamelCase(moduleName);
var pluginFileName = CodeGenUtility.Uncapitalize(moduleName) + "Plugin" + (TypeScript ? ".ts" : ".js");
var customTypes = new HashSet<string>(service.Dtos.Select(x => x.Name).Concat(service.Enums.Select(x => x.Name)));
Copy link
Contributor

Choose a reason for hiding this comment

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

I think when I add .NET 8 support, I'll drop .NET Standard 2.0 from the code gen libraries so they can use modern constructs like ToHashSet().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good!

instead of digging into the type name directly.
Comment on lines 825 to 826
if (serviceType is null)
throw new ArgumentNullException(nameof(serviceType));
Copy link
Contributor Author

@mitchknife mitchknife Jan 1, 2024

Choose a reason for hiding this comment

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

ServiceInfo.GetServiceType can return null, so I opted to handle that explicitly here instead of using ! at the call sites. @ejball What is the scenario where this will be null?

Copy link
Contributor

@ejball ejball Jan 1, 2024

Choose a reason for hiding this comment

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

I think it would only be null if the type name was invalid, which shouldn't be possible if the service parsed successfully. Probably it should have thrown an exception on an invalid type and introduced a separate Try method for possibly returning null.

In any case, it doesn't seem correct to communicate that null is okay in the signature and then throw if it is passed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will change to use ! at the call sites then. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed: 51f5c1f


return serviceType.Kind switch
{
ServiceTypeKind.String or ServiceTypeKind.Bytes or ServiceTypeKind.DateTime or ServiceTypeKind.ExternalEnum => $"{{ type: '{"string"}' }}",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
ServiceTypeKind.String or ServiceTypeKind.Bytes or ServiceTypeKind.DateTime or ServiceTypeKind.ExternalEnum => $"{{ type: '{"string"}' }}",
ServiceTypeKind.String or ServiceTypeKind.Bytes or ServiceTypeKind.DateTime or ServiceTypeKind.ExternalEnum => "{ type: 'string' }",

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I did have this originally, but was getting a Probable JSON string detected (JSON002) linting error, which seemed strange. I'm not actually sure where it is coming from, I can't find anything online about it. I can disable it inline for this method, or maybe make it a suggestion instead of a an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here's what I've found on that now: https://www.infoq.com/news/2022/11/visual-studio-tagged-strings/

Basically, I can add /*lang=json*/ to each line to let Roslyn check it for syntax errors. TIL.

Copy link
Contributor Author

@mitchknife mitchknife Jan 1, 2024

Choose a reason for hiding this comment

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

I think the comments (in this particular case) hurt readability though, so I vote for adding dotnet_diagnostic.JSON002.severity = suggestion to .editorconfig.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fix: 580c68b

Copy link
Contributor

Choose a reason for hiding this comment

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

TIL

ServiceTypeKind.Error => $"{{ $ref: '{"_error"}' }}",
ServiceTypeKind.Dto => $"{{ $ref: '{serviceType.Dto!.Name}' }}",
ServiceTypeKind.Enum => $"{{ $ref: '{serviceType.Enum!.Name}' }}",
ServiceTypeKind.Result => $"{{ type: 'object', properties: {{ value: {GetJsonSchemaType(serviceType.ValueType!)}, error: {{ $ref: '_error' }} }} }}",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
ServiceTypeKind.Result => $"{{ type: 'object', properties: {{ value: {GetJsonSchemaType(serviceType.ValueType!)}, error: {{ $ref: '_error' }} }} }}",
ServiceTypeKind.Result => $$"""{ type: 'object', properties: { value: {{GetJsonSchemaType(serviceType.ValueType!)}}, error: { $ref: '_error' } } }",

I can't tell if this is better or not...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You've got to double on braces somewhere I suppose. The current version is what I'm more familiar with as I don't see the """ syntax as much.

@mitchknife
Copy link
Contributor Author

@ejball πŸ‘πŸΌ πŸ‘ŽπŸΌ ?

@mitchknife mitchknife merged commit fff1953 into FacilityApi:master Jan 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants