Skip to content

implement render endpoint #12346

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

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Build Status](https://img.shields.io/jenkins/build.svg?jobUrl=https://jenkins.swagger.io/job/oss-swagger-codegen-3)](https://jenkins.swagger.io/view/OSS%20-%20Java/job/oss-swagger-codegen-3)

- 3.0.53-SNAPSHOT: [![Build Status](https://img.shields.io/travis/swagger-api/swagger-codegen/3.0.0.svg?label=Petstore%20Integration%20Test)](https://travis-ci.org/swagger-api/swagger-codegen)
- 3.0.54-SNAPSHOT: [![Build Status](https://img.shields.io/travis/swagger-api/swagger-codegen/3.0.0.svg?label=Petstore%20Integration%20Test)](https://travis-ci.org/swagger-api/swagger-codegen)
[![Java Test](https://img.shields.io/jenkins/build.svg?jobUrl=https://jenkins.swagger.io/job/oss-swagger-codegen-3)](https://jenkins.swagger.io/view/OSS%20-%20Java/job/oss-swagger-codegen-3)
- Master (2.4.40-SNAPSHOT): [![Build Status](https://img.shields.io/travis/swagger-api/swagger-codegen/master.svg?label=Petstore%20Integration%20Test)](https://travis-ci.org/swagger-api/swagger-codegen)
[![Java Test](https://img.shields.io/jenkins/build.svg?jobUrl=https://jenkins.swagger.io/job/oss-swagger-codegen-master)](https://jenkins.swagger.io/view/OSS%20-%20Java/job/oss-swagger-codegen-master)
Expand Down Expand Up @@ -112,8 +112,9 @@ The OpenAPI Specification has undergone 3 revisions since initial creation in 20

Swagger Codegen Version | Release Date | OpenAPI Spec compatibility | Notes
-------------------------- |--------------| -------------------------- | -----
3.0.53-SNAPSHOT (current 3.0.0, upcoming minor release) [SNAPSHOT](https://oss.sonatype.org/content/repositories/snapshots/io/swagger/codegen/v3/swagger-codegen-cli/3.0.53-SNAPSHOT/)| TBD | 1.0, 1.1, 1.2, 2.0, 3.0 | Minor release
[3.0.52](https://github.com/swagger-api/swagger-codegen/releases/tag/v3.0.52) (**current stable**) | 2023-12-30 | 1.0, 1.1, 1.2, 2.0, 3.0 | [tag v3.0.52](https://github.com/swagger-api/swagger-codegen/tree/v3.0.52)
3.0.54-SNAPSHOT (current 3.0.0, upcoming minor release) [SNAPSHOT](https://oss.sonatype.org/content/repositories/snapshots/io/swagger/codegen/v3/swagger-codegen-cli/3.0.54-SNAPSHOT/)| TBD | 1.0, 1.1, 1.2, 2.0, 3.0 | Minor release
[3.0.53](https://github.com/swagger-api/swagger-codegen/releases/tag/v3.0.53) (**current stable**) | 2024-02-14 | 1.0, 1.1, 1.2, 2.0, 3.0 | [tag v3.0.53](https://github.com/swagger-api/swagger-codegen/tree/v3.0.53)
[3.0.52](https://github.com/swagger-api/swagger-codegen/releases/tag/v3.0.52) | 2023-12-30 | 1.0, 1.1, 1.2, 2.0, 3.0 | [tag v3.0.52](https://github.com/swagger-api/swagger-codegen/tree/v3.0.52)
[3.0.51](https://github.com/swagger-api/swagger-codegen/releases/tag/v3.0.51) | 2023-11-21 | 1.0, 1.1, 1.2, 2.0, 3.0 | [tag v3.0.51](https://github.com/swagger-api/swagger-codegen/tree/v3.0.51)
[3.0.50](https://github.com/swagger-api/swagger-codegen/releases/tag/v3.0.50) | 2023-10-26 | 1.0, 1.1, 1.2, 2.0, 3.0 | [tag v3.0.50](https://github.com/swagger-api/swagger-codegen/tree/v3.0.50)
[3.0.49](https://github.com/swagger-api/swagger-codegen/releases/tag/v3.0.49) | 2023-10-23 | 1.0, 1.1, 1.2, 2.0, 3.0 | [tag v3.0.49](https://github.com/swagger-api/swagger-codegen/tree/v3.0.49)
Expand Down
2 changes: 1 addition & 1 deletion modules/swagger-codegen-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-project</artifactId>
<version>3.0.53</version>
<version>3.0.54-SNAPSHOT</version>
<relativePath>../..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion modules/swagger-codegen-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-project</artifactId>
<version>3.0.53</version>
<version>3.0.54-SNAPSHOT</version>
<relativePath>../..</relativePath>
</parent>
<artifactId>swagger-codegen-maven-plugin</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion modules/swagger-codegen/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-project</artifactId>
<version>3.0.53</version>
<version>3.0.54-SNAPSHOT</version>
<relativePath>../..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.swagger.codegen.v3;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Template;
import io.swagger.codegen.v3.ignore.CodegenIgnoreProcessor;
import io.swagger.codegen.v3.templates.TemplateEngine;
import io.swagger.codegen.v3.utils.ImplementationVersion;
Expand Down Expand Up @@ -29,6 +32,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL;
import java.time.ZonedDateTime;
import java.util.ArrayList;
Expand Down Expand Up @@ -792,12 +797,27 @@ public List<File> generate() {

// supporting files
Map<String, Object> bundle = buildSupportFileBundle(allOperations, allModels);
Json.prettyPrint(bundle);
generateSupportingFiles(files, bundle);
config.processOpenAPI(openAPI);
return files;
}

@Override
public String renderTemplate(String template, String context) {

try {
Map<String, Object> bundle = new ObjectMapper().readValue(context, Map.class);
Handlebars handlebars = new Handlebars();
Template hTemplate = handlebars.compileInline(template);
return hTemplate.apply(bundle);
} catch (IOException e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
return "Error rendering template: " + e.getMessage() + "\n" + sw.toString();
}
}

@Override
public Map<String, Object> generateBundle() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ public interface Generator {

List<File> generate();
Map<String, Object> generateBundle();

String renderTemplate(String template, String context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,8 @@ public Map<String, Object> generateBundle() {
throw new RuntimeException("missing opts input");
}

public String renderTemplate(RenderRequest renderRequest) {
return new DefaultGenerator().renderTemplate(renderRequest.getTemplate(), renderRequest.getContext());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.swagger.codegen.v3.service;

import java.io.Serializable;

public class RenderRequest implements Serializable {
private static final long serialVersionUID = 1L;

private String template;
private String context;

public RenderRequest template(String template) {
this.template = template;
return this;
}

public String getTemplate() {
return template;
}
public void setTemplate(String template) {
this.template = template;
}

public RenderRequest context(String context) {
this.context = context;
return this;
}

public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.swagger.codegen.v3.service;

import java.io.Serializable;

public class RenderResponse implements Serializable {
private static final long serialVersionUID = 1L;

private String value;

public RenderResponse value(String value) {
this.value = value;
return this;
}

public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}

}
2 changes: 1 addition & 1 deletion modules/swagger-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-project</artifactId>
<version>3.0.53</version>
<version>3.0.54-SNAPSHOT</version>
<relativePath>../..</relativePath>
</parent>
<artifactId>swagger-generator</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import io.swagger.codegen.v3.CodegenConfig;
import io.swagger.codegen.v3.CodegenType;
import io.swagger.codegen.v3.service.GeneratorService;
import io.swagger.codegen.v3.service.RenderRequest;
import io.swagger.codegen.v3.service.RenderResponse;
import io.swagger.v3.core.util.Yaml;
import io.swagger.codegen.v3.service.GenerationRequest;
import io.swagger.v3.generator.model.HiddenOptions;
Expand Down Expand Up @@ -484,6 +486,16 @@ public ResponseContext generate(RequestContext context, GenerationRequest genera

}

public ResponseContext renderTemplate(RequestContext context, RenderRequest renderRequest) {
String result = new GeneratorService().renderTemplate(renderRequest);
RenderResponse response = new RenderResponse();
response.setValue(result);
return new ResponseContext()
.status(200)
.contentType(MediaType.APPLICATION_JSON_TYPE)
.entity(response);
}

public ResponseContext generateBundle(RequestContext context, GenerationRequest generationRequest) {

String requestLog = requestLog(generationRequest);
Expand Down
90 changes: 66 additions & 24 deletions modules/swagger-generator/src/main/resources/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ servers:
info:
description: 'This is an online swagger codegen server. You can find out more at
https://github.com/swagger-api/swagger-codegen or on [irc.freenode.net, #swagger](http://swagger.io/irc/).'
version: 3.0.53
version: 3.0.54-SNAPSHOT
title: Swagger Generator
license:
name: Apache 2.0
Expand All @@ -15,29 +15,6 @@ tags:
- name: documentation
- name: config
paths:
"/model":
post:
tags:
- clients
- servers
- documentation
- config
x-swagger-router-controller: io.swagger.v3.generator.online.GeneratorController
operationId: generateBundle
summary: Generates the intermediate model ("bundle") and returns it as a JSON.
body.
requestBody:
content:
application/json:
schema:
"$ref": "#/components/schemas/GenerationRequest"
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: object
"/generate":
get:
tags:
Expand Down Expand Up @@ -228,6 +205,48 @@ paths:
type: "object"
additionalProperties:
$ref: "#/components/schemas/CliOption"
"/model":
post:
tags:
- clients
- servers
- documentation
- config
x-swagger-router-controller: io.swagger.v3.generator.online.GeneratorController
operationId: generateBundle
summary: Generates the intermediate model ("bundle") and returns it as a JSON.
body.
requestBody:
content:
application/json:
schema:
"$ref": "#/components/schemas/GenerationRequest"
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: object
"/render":
post:
tags:
- documentation
x-swagger-router-controller: io.swagger.v3.generator.online.GeneratorController
operationId: renderTemplate
summary: render a template using the provided data
requestBody:
content:
application/json:
schema:
"$ref": "#/components/schemas/RenderRequest"
responses:
'200':
description: successful operation
content:
application/json:
schema:
"$ref": "#/components/schemas/RenderResponse"
components:
parameters:
version:
Expand Down Expand Up @@ -465,3 +484,26 @@ components:
type: "string"
default:
type: "string"
RenderRequest:
x-swagger-router-model: io.swagger.codegen.v3.service.RenderRequest
required:
- template
- context
properties:
template:
type: string
title: template
description: template as string
example: "{{!mustache}}"
context:
type: string
title: context
description: context as string
example: "{}"
RenderResponse:
x-swagger-router-model: io.swagger.codegen.v3.service.RenderResponse
required:
- value
properties:
value:
type: string
2 changes: 1 addition & 1 deletion pom.docker.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<artifactId>swagger-codegen-project</artifactId>
<packaging>pom</packaging>
<name>swagger-codegen-project</name>
<version>3.0.53</version>
<version>3.0.54-SNAPSHOT</version>
<url>https://github.com/swagger-api/swagger-codegen</url>
<scm>
<connection>scm:git:[email protected]:swagger-api/swagger-codegen.git</connection>
Expand Down
2 changes: 1 addition & 1 deletion pom.java11.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<artifactId>swagger-codegen-project</artifactId>
<packaging>pom</packaging>
<name>swagger-codegen-project</name>
<version>3.0.53</version>
<version>3.0.54-SNAPSHOT</version>
<url>https://github.com/swagger-api/swagger-codegen</url>
<scm>
<connection>scm:git:[email protected]:swagger-api/swagger-codegen.git</connection>
Expand Down
2 changes: 1 addition & 1 deletion pom.java8.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<artifactId>swagger-codegen-project</artifactId>
<packaging>pom</packaging>
<name>swagger-codegen-project</name>
<version>3.0.53</version>
<version>3.0.54-SNAPSHOT</version>
<url>https://github.com/swagger-api/swagger-codegen</url>
<scm>
<connection>scm:git:[email protected]:swagger-api/swagger-codegen.git</connection>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<artifactId>swagger-codegen-project</artifactId>
<packaging>pom</packaging>
<name>swagger-codegen-project</name>
<version>3.0.53</version>
<version>3.0.54-SNAPSHOT</version>
<url>https://github.com/swagger-api/swagger-codegen</url>
<scm>
<connection>scm:git:[email protected]:swagger-api/swagger-codegen.git</connection>
Expand Down
2 changes: 1 addition & 1 deletion samples/meta-codegen/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<swagger-codegen-version>3.0.53</swagger-codegen-version>
<swagger-codegen-version>3.0.54-SNAPSHOT</swagger-codegen-version>
<swagger-codegen-generators-version>1.0.47</swagger-codegen-generators-version>
<maven-plugin-version>1.0.0</maven-plugin-version>
<junit-version>4.13.2</junit-version>
Expand Down