diff --git a/generators/csharp/codegen/src/context/AbstractCsharpGeneratorContext.ts b/generators/csharp/codegen/src/context/AbstractCsharpGeneratorContext.ts index 57b4932baab..07544627ab6 100644 --- a/generators/csharp/codegen/src/context/AbstractCsharpGeneratorContext.ts +++ b/generators/csharp/codegen/src/context/AbstractCsharpGeneratorContext.ts @@ -172,6 +172,8 @@ export abstract class AbstractCsharpGeneratorContext< public abstract getNamespaceForTypeId(typeId: TypeId): string; + public abstract getExtraDependencies(): Record; + // STOLEN FROM: ruby/TypesGenerator.ts // We need a better way to share this sort of ir-processing logic. // diff --git a/generators/csharp/codegen/src/project/CsharpProject.ts b/generators/csharp/codegen/src/project/CsharpProject.ts index 46bf2a432c4..7c5bba182c6 100644 --- a/generators/csharp/codegen/src/project/CsharpProject.ts +++ b/generators/csharp/codegen/src/project/CsharpProject.ts @@ -131,7 +131,8 @@ export class CsharpProject { github: (github) => github.repoUrl, publish: () => undefined, _other: () => undefined - }) + }), + context: this.context }); const templateCsProjContents = csproj.toString(); await writeFile( @@ -248,6 +249,7 @@ declare namespace CsProj { version?: string; license?: string; githubUrl?: string; + context: AbstractCsharpGeneratorContext; } } @@ -257,15 +259,18 @@ class CsProj { private version: string | undefined; private license: string | undefined; private githubUrl: string | undefined; + private context: AbstractCsharpGeneratorContext; - public constructor({ version, license, githubUrl }: CsProj.Args) { + public constructor({ version, license, githubUrl, context }: CsProj.Args) { this.version = version; this.license = license; this.githubUrl = githubUrl; + this.context = context; } public toString(): string { const propertyGroups = this.getPropertyGroups(); + const dependencies = this.getDependencies(); return ` @@ -277,8 +282,7 @@ class CsProj { - - + ${dependencies.join(`\n${FOUR_SPACES}${FOUR_SPACES}`)} @@ -290,6 +294,16 @@ ${this.getAdditionalItemGroups().join(`\n${FOUR_SPACES}`)} `; } + private getDependencies(): string[] { + const result: string[] = []; + result.push(''); + result.push(''); + for (const [name, version] of Object.entries(this.context.getExtraDependencies())) { + result.push(``); + } + return result; + } + private getPropertyGroups(): string[] { const result: string[] = []; if (this.version != null) { diff --git a/generators/csharp/model/src/ModelGeneratorContext.ts b/generators/csharp/model/src/ModelGeneratorContext.ts index 684012c55f1..daa33822cf2 100644 --- a/generators/csharp/model/src/ModelGeneratorContext.ts +++ b/generators/csharp/model/src/ModelGeneratorContext.ts @@ -29,4 +29,8 @@ export class ModelGeneratorContext extends AbstractCsharpGeneratorContext { + return {}; + } } diff --git a/generators/csharp/sdk/CHANGELOG.md b/generators/csharp/sdk/CHANGELOG.md index 854c4c510bd..75a766b868a 100644 --- a/generators/csharp/sdk/CHANGELOG.md +++ b/generators/csharp/sdk/CHANGELOG.md @@ -5,6 +5,19 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.0.33 - 2024-06-21] + +- Improvement: The C# generator now supports configuration to specify extra dependencies. Below + is an example of the `generators.yml` configuration: + + ```yml + - name: fernapi/fern-csharp-sdk + version: 0.0.33 + config: + extra-dependencies: + moq: "0.23.4" + ``` + ## [0.0.32 - 2024-06-21] - Fix: Enum values are JSON serialized before they are sent to the server. For example, if the diff --git a/generators/csharp/sdk/VERSION b/generators/csharp/sdk/VERSION index 78bae5bb6d2..cd9d21e61aa 100644 --- a/generators/csharp/sdk/VERSION +++ b/generators/csharp/sdk/VERSION @@ -1 +1 @@ -0.0.32 +0.0.33 diff --git a/generators/csharp/sdk/src/SdkCustomConfig.ts b/generators/csharp/sdk/src/SdkCustomConfig.ts index 884aeec9876..8217684ccf7 100644 --- a/generators/csharp/sdk/src/SdkCustomConfig.ts +++ b/generators/csharp/sdk/src/SdkCustomConfig.ts @@ -2,7 +2,8 @@ import { z } from "zod"; export const SdkCustomConfigSchema = z.strictObject({ namespace: z.string().optional(), - "client-class-name": z.string().optional() + "client-class-name": z.string().optional(), + "extra-dependencies": z.record(z.string()).optional() }); export type SdkCustomConfigSchema = z.infer; diff --git a/generators/csharp/sdk/src/SdkGeneratorContext.ts b/generators/csharp/sdk/src/SdkGeneratorContext.ts index 280cbec9bc0..c16d41f4846 100644 --- a/generators/csharp/sdk/src/SdkGeneratorContext.ts +++ b/generators/csharp/sdk/src/SdkGeneratorContext.ts @@ -137,6 +137,10 @@ export class SdkGeneratorContext extends AbstractCsharpGeneratorContext { + return this.customConfig["extra-dependencies"] ?? {}; + } + private getNamespaceFromFernFilepath(fernFilepath: FernFilepath): string { return [this.getNamespace(), ...fernFilepath.packagePath.map((path) => path.pascalCase.safeName)].join("."); } diff --git a/seed/csharp-sdk/imdb/.github/workflows/ci.yml b/seed/csharp-sdk/imdb/extra-dependencies/.github/workflows/ci.yml similarity index 100% rename from seed/csharp-sdk/imdb/.github/workflows/ci.yml rename to seed/csharp-sdk/imdb/extra-dependencies/.github/workflows/ci.yml diff --git a/seed/csharp-sdk/imdb/.gitignore b/seed/csharp-sdk/imdb/extra-dependencies/.gitignore similarity index 100% rename from seed/csharp-sdk/imdb/.gitignore rename to seed/csharp-sdk/imdb/extra-dependencies/.gitignore diff --git a/seed/csharp-sdk/imdb/.mock/definition/api.yml b/seed/csharp-sdk/imdb/extra-dependencies/.mock/definition/api.yml similarity index 100% rename from seed/csharp-sdk/imdb/.mock/definition/api.yml rename to seed/csharp-sdk/imdb/extra-dependencies/.mock/definition/api.yml diff --git a/seed/csharp-sdk/imdb/.mock/definition/imdb.yml b/seed/csharp-sdk/imdb/extra-dependencies/.mock/definition/imdb.yml similarity index 100% rename from seed/csharp-sdk/imdb/.mock/definition/imdb.yml rename to seed/csharp-sdk/imdb/extra-dependencies/.mock/definition/imdb.yml diff --git a/seed/csharp-sdk/imdb/.mock/fern.config.json b/seed/csharp-sdk/imdb/extra-dependencies/.mock/fern.config.json similarity index 100% rename from seed/csharp-sdk/imdb/.mock/fern.config.json rename to seed/csharp-sdk/imdb/extra-dependencies/.mock/fern.config.json diff --git a/seed/csharp-sdk/imdb/.mock/generators.yml b/seed/csharp-sdk/imdb/extra-dependencies/.mock/generators.yml similarity index 100% rename from seed/csharp-sdk/imdb/.mock/generators.yml rename to seed/csharp-sdk/imdb/extra-dependencies/.mock/generators.yml diff --git a/seed/csharp-sdk/imdb/snippet-templates.json b/seed/csharp-sdk/imdb/extra-dependencies/snippet-templates.json similarity index 100% rename from seed/csharp-sdk/imdb/snippet-templates.json rename to seed/csharp-sdk/imdb/extra-dependencies/snippet-templates.json diff --git a/seed/csharp-sdk/imdb/snippet.json b/seed/csharp-sdk/imdb/extra-dependencies/snippet.json similarity index 100% rename from seed/csharp-sdk/imdb/snippet.json rename to seed/csharp-sdk/imdb/extra-dependencies/snippet.json diff --git a/seed/csharp-sdk/imdb/src/SeedApi.Test/SeedApi.Test.csproj b/seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi.Test/SeedApi.Test.csproj similarity index 100% rename from seed/csharp-sdk/imdb/src/SeedApi.Test/SeedApi.Test.csproj rename to seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi.Test/SeedApi.Test.csproj diff --git a/seed/csharp-sdk/imdb/src/SeedApi.Test/TestClient.cs b/seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi.Test/TestClient.cs similarity index 100% rename from seed/csharp-sdk/imdb/src/SeedApi.Test/TestClient.cs rename to seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi.Test/TestClient.cs diff --git a/seed/csharp-sdk/imdb/src/SeedApi/Core/ClientOptions.cs b/seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi/Core/ClientOptions.cs similarity index 100% rename from seed/csharp-sdk/imdb/src/SeedApi/Core/ClientOptions.cs rename to seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi/Core/ClientOptions.cs diff --git a/seed/csharp-sdk/imdb/src/SeedApi/Core/CollectionItemSerializer.cs b/seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi/Core/CollectionItemSerializer.cs similarity index 100% rename from seed/csharp-sdk/imdb/src/SeedApi/Core/CollectionItemSerializer.cs rename to seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi/Core/CollectionItemSerializer.cs diff --git a/seed/csharp-sdk/imdb/src/SeedApi/Core/OneOfSerializer.cs b/seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi/Core/OneOfSerializer.cs similarity index 100% rename from seed/csharp-sdk/imdb/src/SeedApi/Core/OneOfSerializer.cs rename to seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi/Core/OneOfSerializer.cs diff --git a/seed/csharp-sdk/imdb/src/SeedApi/Core/RawClient.cs b/seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi/Core/RawClient.cs similarity index 100% rename from seed/csharp-sdk/imdb/src/SeedApi/Core/RawClient.cs rename to seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi/Core/RawClient.cs diff --git a/seed/csharp-sdk/imdb/src/SeedApi/Core/StringEnumSerializer.cs b/seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi/Core/StringEnumSerializer.cs similarity index 100% rename from seed/csharp-sdk/imdb/src/SeedApi/Core/StringEnumSerializer.cs rename to seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi/Core/StringEnumSerializer.cs diff --git a/seed/csharp-sdk/imdb/src/SeedApi/Imdb/ImdbClient.cs b/seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi/Imdb/ImdbClient.cs similarity index 100% rename from seed/csharp-sdk/imdb/src/SeedApi/Imdb/ImdbClient.cs rename to seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi/Imdb/ImdbClient.cs diff --git a/seed/csharp-sdk/imdb/src/SeedApi/Imdb/Types/CreateMovieRequest.cs b/seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi/Imdb/Types/CreateMovieRequest.cs similarity index 100% rename from seed/csharp-sdk/imdb/src/SeedApi/Imdb/Types/CreateMovieRequest.cs rename to seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi/Imdb/Types/CreateMovieRequest.cs diff --git a/seed/csharp-sdk/imdb/src/SeedApi/Imdb/Types/Movie.cs b/seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi/Imdb/Types/Movie.cs similarity index 100% rename from seed/csharp-sdk/imdb/src/SeedApi/Imdb/Types/Movie.cs rename to seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi/Imdb/Types/Movie.cs diff --git a/seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi/SeedApi.csproj b/seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi/SeedApi.csproj new file mode 100644 index 00000000000..80e72871166 --- /dev/null +++ b/seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi/SeedApi.csproj @@ -0,0 +1,25 @@ + + + + + net8.0;net7.0;net6.0; + enable + false + 0.0.1 + README.md + https://github.com/imdb/fern + + + + + + + + + + + + + + + diff --git a/seed/csharp-sdk/imdb/src/SeedApi/SeedApiClient.cs b/seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi/SeedApiClient.cs similarity index 100% rename from seed/csharp-sdk/imdb/src/SeedApi/SeedApiClient.cs rename to seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi/SeedApiClient.cs diff --git a/seed/csharp-sdk/imdb/no-custom-config/.github/workflows/ci.yml b/seed/csharp-sdk/imdb/no-custom-config/.github/workflows/ci.yml new file mode 100644 index 00000000000..9f731ad2f4b --- /dev/null +++ b/seed/csharp-sdk/imdb/no-custom-config/.github/workflows/ci.yml @@ -0,0 +1,26 @@ +name: ci + +on: [push] + +jobs: + compile: + runs-on: ubuntu-latest + + steps: + - name: Checkout repo + uses: actions/checkout@v3 + + - uses: actions/checkout@master + + - name: Setup .NET + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 8.x + + - name: Install tools + run: | + dotnet tool restore + + - name: Build Release + run: dotnet build src -c Release /p:ContinuousIntegrationBuild=true + diff --git a/seed/csharp-sdk/imdb/no-custom-config/.gitignore b/seed/csharp-sdk/imdb/no-custom-config/.gitignore new file mode 100644 index 00000000000..9965de29662 --- /dev/null +++ b/seed/csharp-sdk/imdb/no-custom-config/.gitignore @@ -0,0 +1,477 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET +project.lock.json +project.fragment.lock.json +artifacts/ + +# Tye +.tye/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.tlog +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio 6 auto-generated project file (contains which files were open etc.) +*.vbp + +# Visual Studio 6 workspace and project file (working project files containing files to include in project) +*.dsw +*.dsp + +# Visual Studio 6 technical files +*.ncb +*.aps + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# Visual Studio History (VSHistory) files +.vshistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd + +# VS Code files for those working on multiple tools +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ + +# Windows Installer files from build outputs +*.cab +*.msi +*.msix +*.msm +*.msp + +# JetBrains Rider +*.sln.iml + +## +## Visual studio for Mac +## + + +# globs +Makefile.in +*.userprefs +*.usertasks +config.make +config.status +aclocal.m4 +install-sh +autom4te.cache/ +*.tar.gz +tarballs/ +test-results/ + +# Mac bundle stuff +*.dmg +*.app + +# content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk diff --git a/seed/csharp-sdk/imdb/no-custom-config/.mock/definition/api.yml b/seed/csharp-sdk/imdb/no-custom-config/.mock/definition/api.yml new file mode 100644 index 00000000000..c437dc0ab29 --- /dev/null +++ b/seed/csharp-sdk/imdb/no-custom-config/.mock/definition/api.yml @@ -0,0 +1,4 @@ +name: api +error-discrimination: + strategy: status-code +auth: bearer diff --git a/seed/csharp-sdk/imdb/no-custom-config/.mock/definition/imdb.yml b/seed/csharp-sdk/imdb/no-custom-config/.mock/definition/imdb.yml new file mode 100644 index 00000000000..7919173fe57 --- /dev/null +++ b/seed/csharp-sdk/imdb/no-custom-config/.mock/definition/imdb.yml @@ -0,0 +1,42 @@ +types: + MovieId: string + + Movie: + properties: + id: MovieId + title: string + rating: + type: double + docs: The rating scale is one to five stars + + CreateMovieRequest: + properties: + title: string + rating: double + +service: + auth: false + base-path: /movies + endpoints: + createMovie: + docs: Add a movie to the database + method: POST + path: /create-movie + request: CreateMovieRequest + response: + type: MovieId + status-code: 201 + + getMovie: + method: GET + path: /{movieId} + path-parameters: + movieId: MovieId + response: Movie + errors: + - MovieDoesNotExistError + +errors: + MovieDoesNotExistError: + status-code: 404 + type: MovieId diff --git a/seed/csharp-sdk/imdb/no-custom-config/.mock/fern.config.json b/seed/csharp-sdk/imdb/no-custom-config/.mock/fern.config.json new file mode 100644 index 00000000000..4c8e54ac313 --- /dev/null +++ b/seed/csharp-sdk/imdb/no-custom-config/.mock/fern.config.json @@ -0,0 +1 @@ +{"organization": "fern-test", "version": "*"} \ No newline at end of file diff --git a/seed/csharp-sdk/imdb/no-custom-config/.mock/generators.yml b/seed/csharp-sdk/imdb/no-custom-config/.mock/generators.yml new file mode 100644 index 00000000000..0967ef424bc --- /dev/null +++ b/seed/csharp-sdk/imdb/no-custom-config/.mock/generators.yml @@ -0,0 +1 @@ +{} diff --git a/seed/csharp-sdk/imdb/no-custom-config/snippet-templates.json b/seed/csharp-sdk/imdb/no-custom-config/snippet-templates.json new file mode 100644 index 00000000000..e69de29bb2d diff --git a/seed/csharp-sdk/imdb/no-custom-config/snippet.json b/seed/csharp-sdk/imdb/no-custom-config/snippet.json new file mode 100644 index 00000000000..e69de29bb2d diff --git a/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi.Test/SeedApi.Test.csproj b/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi.Test/SeedApi.Test.csproj new file mode 100644 index 00000000000..4ca52d8f5a3 --- /dev/null +++ b/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi.Test/SeedApi.Test.csproj @@ -0,0 +1,24 @@ + + + + net7.0 + enable + enable + + false + true + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi.Test/TestClient.cs b/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi.Test/TestClient.cs new file mode 100644 index 00000000000..fe8edba1604 --- /dev/null +++ b/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi.Test/TestClient.cs @@ -0,0 +1,3 @@ +namespace SeedApi.Test; + +public class TestClient { } diff --git a/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Core/ClientOptions.cs b/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Core/ClientOptions.cs new file mode 100644 index 00000000000..f0872471a3b --- /dev/null +++ b/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Core/ClientOptions.cs @@ -0,0 +1,24 @@ +namespace SeedApi; + +public partial class ClientOptions +{ + /// + /// The Base URL for the API. + /// + public string BaseUrl { get; init; } + + /// + /// The http client used to make requests. + /// + public HttpClient HttpClient { get; init; } = new HttpClient(); + + /// + /// The http client used to make requests. + /// + public int MaxRetries { get; init; } = 2; + + /// + /// The timeout for the request in seconds. + /// + public int TimeoutInSeconds { get; init; } = 30; +} diff --git a/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Core/CollectionItemSerializer.cs b/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Core/CollectionItemSerializer.cs new file mode 100644 index 00000000000..10121ac93ae --- /dev/null +++ b/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Core/CollectionItemSerializer.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace SeedApi; + +/// +/// Json collection converter. +/// +/// Type of item to convert. +/// Converter to use for individual items. +public class CollectionItemSerializer + : JsonConverter> + where TConverterType : JsonConverter +{ + /// + /// Reads a json string and deserializes it into an object. + /// + /// Json reader. + /// Type to convert. + /// Serializer options. + /// Created object. + public override IEnumerable Read( + ref Utf8JsonReader reader, + Type typeToConvert, + JsonSerializerOptions options + ) + { + if (reader.TokenType == JsonTokenType.Null) + { + return default(IEnumerable); + } + + JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions(options); + jsonSerializerOptions.Converters.Clear(); + jsonSerializerOptions.Converters.Add(Activator.CreateInstance()); + + List returnValue = new List(); + + while (reader.TokenType != JsonTokenType.EndArray) + { + if (reader.TokenType != JsonTokenType.StartArray) + { + returnValue.Add( + (TDatatype) + JsonSerializer.Deserialize( + ref reader, + typeof(TDatatype), + jsonSerializerOptions + ) + ); + } + + reader.Read(); + } + + return returnValue; + } + + /// + /// Writes a json string. + /// + /// Json writer. + /// Value to write. + /// Serializer options. + public override void Write( + Utf8JsonWriter writer, + IEnumerable value, + JsonSerializerOptions options + ) + { + if (value == null) + { + writer.WriteNullValue(); + return; + } + + JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions(options); + jsonSerializerOptions.Converters.Clear(); + jsonSerializerOptions.Converters.Add(Activator.CreateInstance()); + + writer.WriteStartArray(); + + foreach (TDatatype data in value) + { + JsonSerializer.Serialize(writer, data, jsonSerializerOptions); + } + + writer.WriteEndArray(); + } +} diff --git a/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Core/OneOfSerializer.cs b/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Core/OneOfSerializer.cs new file mode 100644 index 00000000000..1cc77f77870 --- /dev/null +++ b/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Core/OneOfSerializer.cs @@ -0,0 +1,67 @@ +using System.Reflection; +using System.Text.Json; +using System.Text.Json.Serialization; +using OneOf; + +namespace SeedApi; + +public class OneOfSerializer : JsonConverter + where TOneOf : IOneOf +{ + public override TOneOf Read( + ref Utf8JsonReader reader, + Type typeToConvert, + JsonSerializerOptions options + ) + { + if (reader.TokenType is JsonTokenType.Null) + return default; + + foreach (var (type, cast) in s_types) + { + try + { + Utf8JsonReader readerCopy = reader; + var result = JsonSerializer.Deserialize(ref readerCopy, type, options); + reader.Skip(); + return (TOneOf)cast.Invoke(null, new[] { result })!; + } + catch (JsonException) { } + } + + throw new JsonException( + $"Cannot deserialize into one of the supported types for {typeToConvert}" + ); + } + + private static readonly (Type type, MethodInfo cast)[] s_types = GetOneOfTypes(); + + public override void Write(Utf8JsonWriter writer, TOneOf value, JsonSerializerOptions options) + { + JsonSerializer.Serialize(writer, value.Value, options); + } + + private static (Type type, MethodInfo cast)[] GetOneOfTypes() + { + var casts = typeof(TOneOf) + .GetRuntimeMethods() + .Where(m => m.IsSpecialName && m.Name == "op_Implicit") + .ToArray(); + var type = typeof(TOneOf); + while (type != null) + { + if ( + type.IsGenericType + && (type.Name.StartsWith("OneOf`") || type.Name.StartsWith("OneOfBase`")) + ) + { + return type.GetGenericArguments() + .Select(t => (t, casts.First(c => c.GetParameters()[0].ParameterType == t))) + .ToArray(); + } + + type = type.BaseType; + } + throw new InvalidOperationException($"{typeof(TOneOf)} isn't OneOf or OneOfBase"); + } +} diff --git a/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Core/RawClient.cs b/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Core/RawClient.cs new file mode 100644 index 00000000000..0f9166ce597 --- /dev/null +++ b/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Core/RawClient.cs @@ -0,0 +1,143 @@ +using System.Text; +using System.Text.Json; + +namespace SeedApi; + +#nullable enable + +/// +/// Utility class for making raw HTTP requests to the API. +/// +public class RawClient +{ + /// + /// The http client used to make requests. + /// + private readonly ClientOptions _clientOptions; + + /// + /// Global headers to be sent with every request. + /// + private readonly Dictionary _headers; + + public RawClient(Dictionary headers, ClientOptions clientOptions) + { + _clientOptions = clientOptions; + _headers = headers; + } + + public async Task MakeRequestAsync(BaseApiRequest request) + { + var url = this.BuildUrl(request.Path, request.Query); + var httpRequest = new HttpRequestMessage(request.Method, url); + if (request.ContentType != null) + { + request.Headers.Add("Content-Type", request.ContentType); + } + // Add global headers to the request + foreach (var (key, value) in _headers) + { + httpRequest.Headers.Add(key, value); + } + // Add request headers to the request + foreach (var (key, value) in request.Headers) + { + httpRequest.Headers.Add(key, value); + } + // Add the request body to the request + if (request is JsonApiRequest jsonRequest) + { + if (jsonRequest.Body != null) + { + var serializerOptions = new JsonSerializerOptions { WriteIndented = true, }; + httpRequest.Content = new StringContent( + JsonSerializer.Serialize(jsonRequest.Body, serializerOptions), + Encoding.UTF8, + "application/json" + ); + } + } + else if (request is StreamApiRequest streamRequest) + { + if (streamRequest.Body != null) + { + httpRequest.Content = new StreamContent(streamRequest.Body); + } + } + // Send the request + HttpResponseMessage response = await _clientOptions.HttpClient.SendAsync(httpRequest); + return new ApiResponse { StatusCode = (int)response.StatusCode, Raw = response }; + } + + public abstract class BaseApiRequest + { + public HttpMethod Method; + + public string Path; + + public string? ContentType = null; + + public Dictionary Query { get; init; } = new(); + + public Dictionary Headers { get; init; } = new(); + + public object RequestOptions { get; init; } + } + + /// + /// The request object to be sent for streaming uploads. + /// + public class StreamApiRequest : BaseApiRequest + { + public Stream? Body { get; init; } = null; + } + + /// + /// The request object to be sent for JSON APIs. + /// + public class JsonApiRequest : BaseApiRequest + { + public object? Body { get; init; } = null; + } + + /// + /// The response object returned from the API. + /// + public class ApiResponse + { + public int StatusCode; + + public HttpResponseMessage Raw; + } + + private Dictionary GetHeaders(BaseApiRequest request) + { + var headers = new Dictionary(); + foreach (var (key, value) in request.Headers) + { + headers.Add(key, value); + } + foreach (var (key, value) in _headers) + { + headers.Add(key, value); + } + return headers; + } + + public string BuildUrl(string path, Dictionary query) + { + var trimmedBaseUrl = _clientOptions.BaseUrl.TrimEnd('/'); + var trimmedBasePath = path.TrimStart('/'); + var url = $"{trimmedBaseUrl}/{trimmedBasePath}"; + if (query.Count > 0) + { + url += "?"; + foreach (var (key, value) in query) + { + url += $"{key}={value}&"; + } + url = url.Substring(0, url.Length - 1); + } + return url; + } +} diff --git a/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Core/StringEnumSerializer.cs b/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Core/StringEnumSerializer.cs new file mode 100644 index 00000000000..9c2af25b378 --- /dev/null +++ b/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Core/StringEnumSerializer.cs @@ -0,0 +1,60 @@ +using System.Runtime.Serialization; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace SeedApi; + +public class StringEnumSerializer : JsonConverter + where TEnum : struct, System.Enum +{ + private readonly Dictionary _enumToString = new Dictionary(); + private readonly Dictionary _stringToEnum = new Dictionary(); + + public StringEnumSerializer() + { + var type = typeof(TEnum); + var values = System.Enum.GetValues(); + + foreach (var value in values) + { + var enumMember = type.GetMember(value.ToString())[0]; + var attr = enumMember + .GetCustomAttributes(typeof(EnumMemberAttribute), false) + .Cast() + .FirstOrDefault(); + + _stringToEnum.Add(value.ToString(), value); + + if (attr?.Value != null) + { + _enumToString.Add(value, attr.Value); + _stringToEnum.Add(attr.Value, value); + } + else + { + _enumToString.Add(value, value.ToString()); + } + } + } + + public override TEnum Read( + ref Utf8JsonReader reader, + Type typeToConvert, + JsonSerializerOptions options + ) + { + var stringValue = reader.GetString(); + + if (_stringToEnum.TryGetValue(stringValue, out var enumValue)) + { + return enumValue; + } + + return default; + } + + public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options) + { + writer.WriteStringValue(_enumToString[value]); + } +} diff --git a/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Imdb/ImdbClient.cs b/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Imdb/ImdbClient.cs new file mode 100644 index 00000000000..2c3d488bdf6 --- /dev/null +++ b/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Imdb/ImdbClient.cs @@ -0,0 +1,50 @@ +using System.Text.Json; +using SeedApi; + +#nullable enable + +namespace SeedApi; + +public class ImdbClient +{ + private RawClient _client; + + public ImdbClient(RawClient client) + { + _client = client; + } + + /// + /// Add a movie to the database + /// + public async Task CreateMovieAsync(CreateMovieRequest request) + { + var response = await _client.MakeRequestAsync( + new RawClient.JsonApiRequest + { + Method = HttpMethod.Post, + Path = "/movies/create-movie", + Body = request + } + ); + string responseBody = await response.Raw.Content.ReadAsStringAsync(); + if (response.StatusCode >= 200 && response.StatusCode < 400) + { + return JsonSerializer.Deserialize(responseBody); + } + throw new Exception(responseBody); + } + + public async Task GetMovieAsync(string movieId) + { + var response = await _client.MakeRequestAsync( + new RawClient.JsonApiRequest { Method = HttpMethod.Get, Path = $"/movies/{movieId}" } + ); + string responseBody = await response.Raw.Content.ReadAsStringAsync(); + if (response.StatusCode >= 200 && response.StatusCode < 400) + { + return JsonSerializer.Deserialize(responseBody); + } + throw new Exception(responseBody); + } +} diff --git a/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Imdb/Types/CreateMovieRequest.cs b/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Imdb/Types/CreateMovieRequest.cs new file mode 100644 index 00000000000..1bb14d0291d --- /dev/null +++ b/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Imdb/Types/CreateMovieRequest.cs @@ -0,0 +1,14 @@ +using System.Text.Json.Serialization; + +#nullable enable + +namespace SeedApi; + +public class CreateMovieRequest +{ + [JsonPropertyName("title")] + public string Title { get; init; } + + [JsonPropertyName("rating")] + public double Rating { get; init; } +} diff --git a/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Imdb/Types/Movie.cs b/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Imdb/Types/Movie.cs new file mode 100644 index 00000000000..189ef78faf6 --- /dev/null +++ b/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/Imdb/Types/Movie.cs @@ -0,0 +1,20 @@ +using System.Text.Json.Serialization; + +#nullable enable + +namespace SeedApi; + +public class Movie +{ + [JsonPropertyName("id")] + public string Id { get; init; } + + [JsonPropertyName("title")] + public string Title { get; init; } + + /// + /// The rating scale is one to five stars + /// + [JsonPropertyName("rating")] + public double Rating { get; init; } +} diff --git a/seed/csharp-sdk/imdb/src/SeedApi/SeedApi.csproj b/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/SeedApi.csproj similarity index 100% rename from seed/csharp-sdk/imdb/src/SeedApi/SeedApi.csproj rename to seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/SeedApi.csproj diff --git a/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/SeedApiClient.cs b/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/SeedApiClient.cs new file mode 100644 index 00000000000..a9ae820e82f --- /dev/null +++ b/seed/csharp-sdk/imdb/no-custom-config/src/SeedApi/SeedApiClient.cs @@ -0,0 +1,31 @@ +using SeedApi; + +#nullable enable + +namespace SeedApi; + +public partial class SeedApiClient +{ + private RawClient _client; + + public SeedApiClient(string token, ClientOptions clientOptions = null) + { + _client = new RawClient( + new Dictionary() { { "X-Fern-Language", "C#" }, }, + clientOptions ?? new ClientOptions() + ); + Imdb = new ImdbClient(_client); + } + + public ImdbClient Imdb { get; } + + private string GetFromEnvironmentOrThrow(string env, string message) + { + var value = System.Environment.GetEnvironmentVariable(env); + if (value == null) + { + throw new Exception(message); + } + return value; + } +} diff --git a/seed/csharp-sdk/seed.yml b/seed/csharp-sdk/seed.yml index 03960a1746f..034844a884c 100644 --- a/seed/csharp-sdk/seed.yml +++ b/seed/csharp-sdk/seed.yml @@ -14,6 +14,15 @@ local: buildCommand: - yarn workspace @fern-api/fern-csharp-sdk dist:cli runCommand: node sdk/dist/bundle.cjs +fixtures: + imdb: + - customConfig: null + outputFolder: no-custom-config + - customConfig: + extra-dependencies: + Moq: 4.20.70 + Moq.Contrib.HttpClient: "1.4.0" + outputFolder: extra-dependencies allowedFailures: - alias - auth-environment-variables