openapi-contrib / openapi3-generator

Use your API OpenAPI 3 definition to generate code, documentation, and literally anything you need.
Apache License 2.0
90 stars 27 forks source link

requestBody is not present in express #22

Open mehtatejas opened 5 years ago

mehtatejas commented 5 years ago

Hi, I am generating code for petstore-expanded.yaml. However I found that requestBody schema is not present in router. og -o ./my-docs petstore.yaml express

Kindly guide.

fmvilas commented 5 years ago

Hi @mehtatejas. I'm not sure I understand your concern. Can you please provide a better explanation? Thanks.

fmvilas commented 5 years ago

Oh, I understood now. You're right, this is missing. It's being done via parameters with in: body property.

Would you mind adding this feature? It's should be a matter of iterating over requestBody here: https://github.com/fmvilas/openapi3-generator/blob/master/templates/express/src/api/routes/%24%24path%24%24.js.hbs#L15.

mehtatejas commented 5 years ago

Hi @fmvilas.

I tried as follows :

{{#each ../requestBody  as |a content|}}
    {{#match content "content"}}
    {{#each a as |b json|}}
        {{#each b as |c schema|}}
          {{#match schema "schema"}}
            {{#each c as |d properties|}}
              {{#match properties "properties"}}
                {{#each d as |e properties|}}
{{properties}}: req.body.{{properties}}{{#unless @last}},{{/unless}}
                {{/each}}
              {{/match}}
            {{/each}}
          {{/match}}
        {{/each}}

    {{/each}}
    {{/match}}
{{/each}}

However, I am not sure this is correct approach

fmvilas commented 5 years ago

I haven't tested it but I think is like this:

{{#each ../requestBody.content.['application/json'].schema.properties as |prop|}}
  {{prop}}: req.body.{{prop}}{{#unless @last}},{{/unless}}
{{/each}}

There's no need to check for existence in Handlebars. For instance, in the expression ../requestBody.content.['application/json'].schema.properties, if ../requestBody.content is undefined nothing will happen and it will not fail.

mehtatejas commented 5 years ago

Following code works.

{{#each ../requestBody.content}}
  {{#each this.schema.properties}}
{{@key}}: req.body.{{@key}}{{#unless @last}},{{/unless}}
  {{/each}}     
{{/each}}
fmvilas commented 5 years ago

Can you paste your OpenAPI description here? Just the excerpt where requestBody is.

mehtatejas commented 5 years ago

https://github.com/fmvilas/openapi3-generator/blob/master/tests/openapi3/petstore-expanded.yaml

post: description: Creates a new pet in the store. Duplicates are allowed operationId: addPet requestBody: description: Pet to add to the store required: true content: application/json: schema: $ref: '#/components/schemas/NewPet'

fmvilas commented 5 years ago

Oh! I see. Yeah it should be correct :+1: