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

NSwag contracts #186

Closed
bielik01 opened this issue Oct 11, 2023 · 14 comments · Fixed by #188
Closed

NSwag contracts #186

bielik01 opened this issue Oct 11, 2023 · 14 comments · Fixed by #188
Assignees
Labels
enhancement New feature, bug fix, or request

Comments

@bielik01
Copy link

Maybe it's there and I couldn't find it, but when generating contracts, I'd like to specify some NSwag options like dateType (I don't like DateTimeOffset) or arrayType.

@bielik01 bielik01 added the enhancement New feature, bug fix, or request label Oct 11, 2023
@bielik01
Copy link
Author

Yeah, there's a lot of default parameters in CSharpClientGeneratorFactory.

@christianhelle
Copy link
Owner

@bielik01 I actually have this in my backlog. I plan to expose settings to customize the entire CSharpGeneratorSettings

You have things like these options that can be customized

AnyType = "object";
Namespace = "MyNamespace";

DateType = "System.DateTimeOffset";
DateTimeType = "System.DateTimeOffset";
TimeType = "System.TimeSpan";
TimeSpanType = "System.TimeSpan";

ArrayType = "System.Collections.Generic.ICollection";
ArrayInstanceType = "System.Collections.ObjectModel.Collection";
ArrayBaseType = "System.Collections.ObjectModel.Collection";

DictionaryType = "System.Collections.Generic.IDictionary";
DictionaryInstanceType = "System.Collections.Generic.Dictionary";
DictionaryBaseType = "System.Collections.Generic.Dictionary";

ClassStyle = CSharpClassStyle.Poco;
JsonLibrary = CSharpJsonLibrary.NewtonsoftJson;

RequiredPropertiesMustBeDefined = true;
GenerateDataAnnotations = true;
TypeAccessModifier = "public";
PropertySetterAccessModifier = string.Empty;
GenerateJsonMethods = false;
EnforceFlagEnums = false;

InlineNamedArrays = false;
InlineNamedDictionaries = false;
InlineNamedTuples = true;

So the .refitter settings file might look something like:

{
  "openApiPath": "./OpenAPI/v3.0/petstore.json",
  "namespace": "Petstore",
  "contractsGeneratorSettings": {
    "dateType": "System.DateTime",
    "dateTimeType": "System.DateTime",
    "arrayType": "System.Collections.Generic.IList"
  }
}

@christianhelle
Copy link
Owner

@all-contributors please add @bielik01 for ideas

@allcontributors
Copy link
Contributor

@christianhelle

I've put up a pull request to add @bielik01! 🎉

@alrz
Copy link

alrz commented Oct 13, 2023

Is it "contractsGeneratorSettings" or "codeGeneratorSettings"? In the README it's the latter.

Also I just confused with "namespace" being mentioned at the top level AND the settings part. The former is valid.

@christianhelle
Copy link
Owner

christianhelle commented Oct 13, 2023

Is it "contractsGeneratorSettings" or "codeGeneratorSettings"? In the README it's the latter.

@alrz In the end, I went with codeGeneratorSettings as it's not only the types used in the contract/dto, it's also the arguments and return types in the Refit interface

I try to keep the READMEs up-to-date. Recently, I also introduced a documentation site - https://refitter.github.io that I will also try my best to keep up-to-date

@christianhelle
Copy link
Owner

Also I just confused with "namespace" being mentioned at the top level AND the settings part. The former is valid.

This is a mistake and only the top level namespace is used. The codeGeneratorSettings was a copy of the NSwag type, and I was supposed to clean it up

Thanks for pointing that out @alrz

@christianhelle
Copy link
Owner

@all-contributors please add @alrz for bugs

@allcontributors
Copy link
Contributor

@christianhelle

I've put up a pull request to add @alrz! 🎉

@bielik01
Copy link
Author

but that was namespace for contracts, it was perfectly fine there

@christianhelle
Copy link
Owner

but that was namespace for contracts, it was perfectly fine there

@bielik01 Are you interested in having different namespaces for contracts and the interfaces? That's not how its currently implemented and I originally wanted to use the same namespace for all generated types, be it DTO objects or the interface(s), since they are in the same file. Having different namespaces for contracts and interfaces would require importing the contracts namespace into each interface, or specifying the full type name on all usages

I have toyed with the idea of splitting the contracts and interfaces into different files, but it's not how I personally use the tool, and I didn't want to assume how others might use it

Is there an interest for splitting the output into multiple files? It's not really that hard to implement, and I won't use it myself, but if there are others who need it then why not 😃

@bielik01
Copy link
Author

bielik01 commented Oct 15, 2023

namespace from codeGeneratorSettings worked for me with additionalNamespaces, but yeah, output (from openApiToCSharpClient in NSwag Configuration Document) is missing to generate contracts separately from the interface, but I don't know how to place it...

and #184 is missing to completion 😅

I wonder if it should stay codeGeneratorSettings, maybe contractsGeneratorSettings as you first envisioned it would be better, to indicate it's only for NSwag contracts

@christianhelle
Copy link
Owner

namespace from codeGeneratorSettings worked for me with additionalNamespaces, but yeah, output (from openApiToCSharpClient in NSwag Configuration Document) is missing to generate contracts separately from the interface, but I don't know how to place it...

Currently, NSwag returns me a string containing all the generated contracts. I can write this to another file if we can introduce a setting that describes it.

A lot of the options in openApiToCSharpClient are options for how NSwag will generate the API Client and they are irrelevant to Refitter since Refitter generates the Refit interface manually, only using NSwag to parse the OpenAPI spec

and #184 is missing to completion 😅

Coming soon :)

I wonder if it should stay codeGeneratorSettings, maybe contractsGeneratorSettings as you first envisioned it would be better, to indicate it's only for NSwag contracts

I renamed it to codeGeneratorSettings because these settings affect how the OpenAPI spec is parsed, which I use for generating the Refit interface. So if you specify the dateTimeType to be System.DateTime it also affects query string parameters, the same goes for arrayType which can be at times used as a return type to methods in the Refit interface

@christianhelle
Copy link
Owner

Not really much left for me to do here so I'll close this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature, bug fix, or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants