Skip to content

Instantly share code, notes, and snippets.

@Magnuti
Last active November 29, 2024 13:46
Show Gist options
  • Save Magnuti/adf008e3b69a97d483db79eb09c5057b to your computer and use it in GitHub Desktop.
Save Magnuti/adf008e3b69a97d483db79eb09c5057b to your computer and use it in GitHub Desktop.
C# client generation from Swagger specification with NSwag on Docker

C# client generation from Swagger specification with NSwag on Docker

FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine

# Install Node.js - we use Node.js 20 here, pick another version if you like that
RUN apk update \
    && apk add --no-cache curl \
    && curl -sL https://deb.nodesource.com/setup_20.x \
    && apk add --no-cache nodejs npm build-base

WORKDIR /app

# RUN npm init -y
RUN npm install -g nswag
docker build -t <your tag> .
docker run --rm --volume "${PWD}:/app" generator npx nswag openapi2csclient /input:https://<base_url>/swagger/v1/swagger.json  /classname:GeneratedClient /namespace:Namespace /output:GeneratedClient.cs

After running the docker run command above you should now see a new file called GeneratedClient.cs.

Alternatively on Debian

FROM mcr.microsoft.com/dotnet/sdk:8.0

# Install Node.js
RUN apt-get update -yq && apt-get upgrade -yq && apt-get install -yq curl
# We use Node.js 20 here, pick another version if you like that
RUN curl -sL https://deb.nodesource.com/setup_20.x | bash - && apt-get install -yq nodejs build-essential

WORKDIR /app

# RUN npm init -y
RUN npm install -g nswag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment