-
Notifications
You must be signed in to change notification settings - Fork 487
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
Generated JSON missing schemas #1123
Comments
hi, @willin you can leave a minimal reproduction of a clonable git repo so the core team can evaluate the problem you reported. |
https://github.com/wshow/nest-swagger-generate-demo
|
for the timebeing #!/usr/bin/env bash
# Generate api.json
mv src/main.ts src/main.ts.bak
cp src/doc.ts src/main.ts
nest start
rm -f src/main.ts
mv src/main.ts.bak src/main.ts we use this script temporarily but i have no idea why |
Hi @willin, i took a look at your repo to generate a json schema i tried to make some changes try this in the {
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true,
"plugins": ["@nestjs/swagger/plugin"]
}
} then in the main.ts file add this line: writeFileSync("./ swagger.json", JSON.stringify(document)); the complete file becomes like this: import {NestFactory} from '@nestjs/core';
import {DocumentBuilder, SwaggerModule} from '@nestjs/swagger';
import {AppModule} from './app.module';
import {writeFileSync} from "fs";
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const options = new DocumentBuilder()
.setTitle('Cats example')
.setDescription('The cats API description')
.setVersion('1.0')
.addTag('cats')
.addBearerAuth()
.build();
const document = SwaggerModule.createDocument(app, options);
writeFileSync("./ swagger.json", JSON.stringify(document));
SwaggerModule.setup ('api', app, document);
await app.listen(3000);
console.log(`Application is running on: $ {await app.getUrl ()}`);
}
bootstrap(); i called it as soon as you run from terminal The json that I generate is the following: {
"openapi": "3.0.0",
"info": {
"title": "Cats example",
"description": "The cats API description",
"version": "1.0",
"contact": {}
},
"tags": [{
"name": "cats",
"description": ""
}],
"servers": [],
"components": {
"securitySchemes": {
"bearer": {
"scheme": "bearer",
"bearerFormat": "JWT",
"type": "http"
}
},
"schemas": {
"CreateCatDto": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "number",
"example": 1,
"description": "The age of the Cat"
},
"breed": {
"type": "string",
"example": "Maine Coon",
"description": "The breed of the Cat"
}
},
"required": ["name", "age", "breed"]
},
"Cat": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "number"
},
"breed": {
"type": "string"
}
},
"required": ["name", "age", "breed"]
}
}
},
"paths": {
"/cats": {
"post": {
"operationId": "CatsController_create",
"summary": "Create cat",
"parameters": [],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CreateCatDto"
}
}
}
},
"responses": {
"201": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Cat"
}
}
}
},
"403": {
"description": "Forbidden."
}
},
"tags": ["cats"],
"security": [{
"bearer": []
}]
}
},
"/cats/{id}": {
"get": {
"operationId": "CatsController_findOne",
"parameters": [{
"name": "id",
"required": true,
"in": "path",
"schema": {
"type": "string"
}
}],
"responses": {
"200": {
"description": "The found record",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Cat"
}
}
}
}
},
"tags": ["cats"],
"security": [{
"bearer": []
}]
}
}
}
} alternatively to generate the file in json you can also create an endpoint that generates the file in json, the endpoint and the following Remember, if you want to view everything in swagger you have to add the however if you have difficulty use the discord channel for support |
@Tony133 i don't want to start up a swagger service at PRODUCTION ENV, so that i need another script to generate the thing confuse me is that |
@willin Ok I misunderstood, but add a description of your problem to specify it better, so to the eye it seems that your problem is that it does not generate the json file. |
ts-node src/ search "CreateCatDto": {
"type": "object",
"properties": {}
}, nest start found: "CreateCatDto": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "number",
"example": 1,
"description": "The age of the Cat"
},
"breed": {
"type": "string",
"example": "Maine Coon",
"description": "The breed of the Cat"
}
}, |
This is the expected behavior. CLI plugins run as part of the Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements. |
For anyone that arrived here, like me, and is just looking for a solution, the nest cli has an --entryFile option. This can be used to set the entry file when doing nest start instead of ts-node. In the exampe repo changing:
to:
Should fix the problem. |
This has been plaguing me for a while, thank you so much @javiermuniz!!! |
I'm submitting a...
Current behavior
json file:
Expected behavior
same with: http://localhost:3000/api-json
Minimal reproduction of the problem with instructions
https://github.com/nestjs/nest/tree/master/sample/11-swagger
add a generator script:
Environment
The text was updated successfully, but these errors were encountered: