Compare commits

..

11 Commits

Author SHA1 Message Date
William Cheng
c107bfa6e1 Merge remote-tracking branch 'origin/master' into fix-release2 2023-07-07 08:43:13 +08:00
William Cheng
ba5c5f478e remove openapi parameter from method (#16017) 2023-07-07 08:32:38 +08:00
Bodo Graumann
ba1c600830 [typescript] Fix support for relative URLs (#15482)
* Add test for different url types

* Fix tests for typescript inversify framework

* Add workaround for relative URLs

* Regenerate samples
2023-07-06 17:30:19 +02:00
William Cheng
a16a315fee Revert "7.0.0-beta release (#16018)"
This reverts commit 635f7952ce.
2023-07-06 17:21:35 +08:00
William Cheng
e297d4dd59 Revert "v6.5.0 portal release"
This reverts commit 8a91858df5.
2023-04-01 21:14:20 +08:00
William Cheng
537734404e Revert "add back docker publish"
This reverts commit a279145cc6.
2023-04-01 21:14:14 +08:00
William Cheng
73cf95c3ef Revert "trigger build"
This reverts commit f7ea8baf35.
2023-04-01 21:13:59 +08:00
William Cheng
f7ea8baf35 trigger build 2023-04-01 19:00:42 +08:00
William Cheng
a279145cc6 add back docker publish 2023-04-01 16:24:37 +08:00
William Cheng
8a91858df5 v6.5.0 portal release 2023-04-01 15:35:06 +08:00
William Cheng
35430d9406 fix publising to gradle portal 2023-04-01 15:31:05 +08:00
35 changed files with 157 additions and 65 deletions

View File

@@ -4,7 +4,7 @@
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-project</artifactId>
<!-- RELEASE_VERSION -->
<version>7.0.0-beta</version>
<version>7.0.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<relativePath>../..</relativePath>
</parent>

View File

@@ -6,7 +6,7 @@
<artifactId>openapi-generator-project</artifactId>
<groupId>org.openapitools</groupId>
<!-- RELEASE_VERSION -->
<version>7.0.0-beta</version>
<version>7.0.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<relativePath>../..</relativePath>
</parent>

View File

@@ -3,7 +3,7 @@ import io.github.gradlenexus.publishplugin.ReleaseNexusStagingRepository
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("com.gradle.plugin-publish") version "1.0.0"
id("com.gradle.plugin-publish") version "1.1.0"
id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
id("java-gradle-plugin")
id("maven-publish")

View File

@@ -1,5 +1,5 @@
# RELEASE_VERSION
openApiGeneratorVersion=7.0.0-beta
openApiGeneratorVersion=7.0.0-SNAPSHOT
# /RELEASE_VERSION
# BEGIN placeholders

View File

@@ -4,7 +4,7 @@
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-project</artifactId>
<!-- RELEASE_VERSION -->
<version>7.0.0-beta</version>
<version>7.0.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<relativePath>../..</relativePath>
</parent>
@@ -93,11 +93,14 @@
<gradleVersion>${gradleVersion}</gradleVersion>
<args>
<arg>-P openApiGeneratorVersion=${project.version}</arg>
<arg>-Psigning.keyId=${env.SIGNING_KEY}</arg>
<arg>-Psigning.password=${env.SIGNING_PASSPHRASE}</arg>
<arg>-Psigning.secretKeyRingFile=${env.TRAVIS_BUILD_DIR}/sec.gpg</arg>
</args>
</configuration>
<executions>
<execution>
<phase>install</phase>
<phase>compile</phase>
<goals>
<!-- goal must be "invoke" -->
<goal>invoke</goal>

View File

@@ -86,13 +86,13 @@ task buildGoSdk(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTas
}
task buildDotnetSdk(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask){
generatorName = "csharp"
generatorName = "csharp-netcore"
inputSpec = "$rootDir/petstore-v3.0.yaml".toString()
additionalProperties = [
packageGuid: "{321C8C3F-0156-40C1-AE42-D59761FB9B6C}",
useCompareNetObjects: "true"
]
outputDir = "$buildDir/csharp".toString()
outputDir = "$buildDir/csharp-netcore".toString()
globalProperties = [
models: "",
apis : "",

View File

@@ -1,3 +1,3 @@
# RELEASE_VERSION
openApiGeneratorVersion=7.0.0-beta
openApiGeneratorVersion=7.0.0-SNAPSHOT
# /RELEASE_VERSION

View File

@@ -13,7 +13,7 @@
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<!-- RELEASE_VERSION -->
<version>7.0.0-beta</version>
<version>7.0.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<executions>
<execution>

View File

@@ -15,7 +15,7 @@
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<!-- RELEASE_VERSION -->
<version>7.0.0-beta</version>
<version>7.0.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<executions>
<execution>

View File

@@ -19,7 +19,7 @@
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<!-- RELEASE_VERSION -->
<version>7.0.0-beta</version>
<version>7.0.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<dependencies>
<dependency>

View File

@@ -13,7 +13,7 @@
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<!-- RELEASE_VERSION -->
<version>7.0.0-beta</version>
<version>7.0.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<executions>
<execution>

View File

@@ -13,7 +13,7 @@
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<!-- RELEASE_VERSION -->
<version>7.0.0-beta</version>
<version>7.0.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<executions>
<execution>

View File

@@ -20,7 +20,7 @@
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<!-- RELEASE_VERSION -->
<version>7.0.0-beta</version>
<version>7.0.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<executions>
<execution>

View File

@@ -5,7 +5,7 @@
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-project</artifactId>
<!-- RELEASE_VERSION -->
<version>7.0.0-beta</version>
<version>7.0.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<relativePath>../..</relativePath>
</parent>

View File

@@ -4,7 +4,7 @@
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-project</artifactId>
<!-- RELEASE_VERSION -->
<version>7.0.0-beta</version>
<version>7.0.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<relativePath>../..</relativePath>
</parent>

View File

@@ -4,7 +4,7 @@
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-project</artifactId>
<!-- RELEASE_VERSION -->
<version>7.0.0-beta</version>
<version>7.0.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<relativePath>../..</relativePath>
</parent>

View File

@@ -1025,7 +1025,7 @@ public class DefaultCodegen implements CodegenConfig {
if (e.getKey().contains("/")) {
// if this is property schema, we also need to generate the oneOf interface model
addOneOfNameExtension((ComposedSchema) s, nOneOf);
addOneOfInterfaceModel((ComposedSchema) s, nOneOf, openAPI);
addOneOfInterfaceModel((ComposedSchema) s, nOneOf);
} else {
// else this is a component schema, so we will just use that as the oneOf interface model
addOneOfNameExtension((ComposedSchema) s, n);
@@ -1034,13 +1034,13 @@ public class DefaultCodegen implements CodegenConfig {
Schema items = ((ArraySchema) s).getItems();
if (ModelUtils.isComposedSchema(items)) {
addOneOfNameExtension((ComposedSchema) items, nOneOf);
addOneOfInterfaceModel((ComposedSchema) items, nOneOf, openAPI);
addOneOfInterfaceModel((ComposedSchema) items, nOneOf);
}
} else if (ModelUtils.isMapSchema(s)) {
Schema addProps = getAdditionalProperties(s);
if (addProps != null && ModelUtils.isComposedSchema(addProps)) {
addOneOfNameExtension((ComposedSchema) addProps, nOneOf);
addOneOfInterfaceModel((ComposedSchema) addProps, nOneOf, openAPI);
addOneOfInterfaceModel((ComposedSchema) addProps, nOneOf);
}
}
}
@@ -2662,7 +2662,7 @@ public class DefaultCodegen implements CodegenConfig {
for (Schema innerSchema : composed.getAllOf()) { // TODO need to work with anyOf, oneOf as well
if (m.discriminator == null && innerSchema.getDiscriminator() != null) {
LOGGER.debug("discriminator is set to null (not correctly set earlier): {}", m.name);
m.setDiscriminator(createDiscriminator(m.name, innerSchema, this.openAPI));
m.setDiscriminator(createDiscriminator(m.name, innerSchema));
modelDiscriminators++;
}
@@ -3029,7 +3029,7 @@ public class DefaultCodegen implements CodegenConfig {
}
m.isAlias = (typeAliases.containsKey(name)
|| isAliasOfSimpleTypes(schema)); // check if the unaliased schema is an alias of simple OAS types
m.setDiscriminator(createDiscriminator(name, schema, this.openAPI));
m.setDiscriminator(createDiscriminator(name, schema));
if (schema.getDeprecated() != null) {
m.isDeprecated = schema.getDeprecated();
@@ -3385,10 +3385,9 @@ public class DefaultCodegen implements CodegenConfig {
* @param composedSchemaName The String model name of the composed schema where we are setting the discriminator map
* @param discPropName The String that is the discriminator propertyName in the schema
* @param c The ComposedSchema that contains the discriminator and oneOf/anyOf schemas
* @param openAPI The OpenAPI spec that we are using
* @return the list of oneOf and anyOf MappedModel that need to be added to the discriminator map
*/
protected List<MappedModel> getOneOfAnyOfDescendants(String composedSchemaName, String discPropName, ComposedSchema c, OpenAPI openAPI) {
protected List<MappedModel> getOneOfAnyOfDescendants(String composedSchemaName, String discPropName, ComposedSchema c) {
ArrayList<List<Schema>> listOLists = new ArrayList<>();
listOLists.add(c.getOneOf());
listOLists.add(c.getAnyOf());
@@ -3452,7 +3451,7 @@ public class DefaultCodegen implements CodegenConfig {
return descendentSchemas;
}
protected List<MappedModel> getAllOfDescendants(String thisSchemaName, OpenAPI openAPI) {
protected List<MappedModel> getAllOfDescendants(String thisSchemaName) {
ArrayList<String> queue = new ArrayList();
List<MappedModel> descendentSchemas = new ArrayList();
Map<String, Schema> schemas = ModelUtils.getSchemas(openAPI);
@@ -3508,7 +3507,7 @@ public class DefaultCodegen implements CodegenConfig {
return descendentSchemas;
}
protected CodegenDiscriminator createDiscriminator(String schemaName, Schema schema, OpenAPI openAPI) {
protected CodegenDiscriminator createDiscriminator(String schemaName, Schema schema) {
Discriminator sourceDiscriminator = recursiveGetDiscriminator(schema, new ArrayList<Schema>());
if (sourceDiscriminator == null) {
return null;
@@ -3563,7 +3562,7 @@ public class DefaultCodegen implements CodegenConfig {
boolean legacyUseCase = (this.getLegacyDiscriminatorBehavior() && uniqueDescendants.isEmpty());
if (!this.getLegacyDiscriminatorBehavior() || legacyUseCase) {
// for schemas that allOf inherit from this schema, add those descendants to this discriminator map
List<MappedModel> otherDescendants = getAllOfDescendants(schemaName, openAPI);
List<MappedModel> otherDescendants = getAllOfDescendants(schemaName);
for (MappedModel otherDescendant : otherDescendants) {
// add only if the mapping names are not the same and the model names are not the same
boolean matched = false;
@@ -3582,7 +3581,7 @@ public class DefaultCodegen implements CodegenConfig {
}
// if there are composed oneOf/anyOf schemas, add them to this discriminator
if (ModelUtils.isComposedSchema(schema) && !this.getLegacyDiscriminatorBehavior()) {
List<MappedModel> otherDescendants = getOneOfAnyOfDescendants(schemaName, discriminatorPropertyName, (ComposedSchema) schema, openAPI);
List<MappedModel> otherDescendants = getOneOfAnyOfDescendants(schemaName, discriminatorPropertyName, (ComposedSchema) schema);
for (MappedModel otherDescendant : otherDescendants) {
if (!uniqueDescendants.contains(otherDescendant)) {
uniqueDescendants.add(otherDescendant);
@@ -6736,7 +6735,7 @@ public class DefaultCodegen implements CodegenConfig {
return requestBody.getContent().keySet();
}
public boolean hasFormParameter(OpenAPI openAPI, Operation operation) {
public boolean hasFormParameter(Operation operation) {
Set<String> consumesInfo = getConsumesInfo(openAPI, operation);
if (consumesInfo == null || consumesInfo.isEmpty()) {
@@ -6754,7 +6753,7 @@ public class DefaultCodegen implements CodegenConfig {
return false;
}
public boolean hasBodyParameter(OpenAPI openAPI, Operation operation) {
public boolean hasBodyParameter(Operation operation) {
RequestBody requestBody = ModelUtils.getReferencedRequestBody(openAPI, operation.getRequestBody());
if (requestBody == null) {
return false;
@@ -7869,16 +7868,15 @@ public class DefaultCodegen implements CodegenConfig {
*
* @param cs ComposedSchema object to create as interface model
* @param type name to use for the generated interface model
* @param openAPI OpenAPI spec that we are using
*/
public void addOneOfInterfaceModel(ComposedSchema cs, String type, OpenAPI openAPI) {
public void addOneOfInterfaceModel(ComposedSchema cs, String type) {
if (cs.getOneOf() == null) {
return;
}
CodegenModel cm = new CodegenModel();
cm.setDiscriminator(createDiscriminator("", cs, openAPI));
cm.setDiscriminator(createDiscriminator("", cs));
for (Schema o : Optional.ofNullable(cs.getOneOf()).orElse(Collections.emptyList())) {
if (o.get$ref() == null) {

View File

@@ -1579,8 +1579,8 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
}
for (Operation operation : path.readOperations()) {
LOGGER.info("Processing operation {}", operation.getOperationId());
if (hasBodyParameter(openAPI, operation) || hasFormParameter(openAPI, operation)) {
String defaultContentType = hasFormParameter(openAPI, operation) ? "application/x-www-form-urlencoded" : "application/json";
if (hasBodyParameter(operation) || hasFormParameter(operation)) {
String defaultContentType = hasFormParameter(operation) ? "application/x-www-form-urlencoded" : "application/json";
List<String> consumes = new ArrayList<>(getConsumesInfo(openAPI, operation));
String contentType = consumes.isEmpty() ? defaultContentType : consumes.get(0);
operation.addExtension("x-content-type", contentType);

View File

@@ -550,8 +550,8 @@ public class DartDioClientCodegen extends AbstractDartCodegen {
/// to remove extra mappings added as a side effect of setLegacyDiscriminatorBehavior(false)
/// this ensures 1-1 schema mapping instead of 1-many
@Override
protected CodegenDiscriminator createDiscriminator(String schemaName, Schema schema, OpenAPI openAPI) {
CodegenDiscriminator sub = super.createDiscriminator(schemaName, schema, openAPI);
protected CodegenDiscriminator createDiscriminator(String schemaName, Schema schema) {
CodegenDiscriminator sub = super.createDiscriminator(schemaName, schema);
Discriminator originalDiscriminator = schema.getDiscriminator();
if (originalDiscriminator!=null) {
Map<String,String> originalMapping = originalDiscriminator.getMapping();

View File

@@ -537,8 +537,8 @@ public class K6ClientCodegen extends DefaultCodegen implements CodegenConfig {
}
}
if (hasBodyParameter(openAPI, operation) || hasFormParameter(openAPI, operation)) {
String defaultContentType = hasFormParameter(openAPI, operation) ? "application/x-www-form-urlencoded" : "application/json";
if (hasBodyParameter(operation) || hasFormParameter(operation)) {
String defaultContentType = hasFormParameter(operation) ? "application/x-www-form-urlencoded" : "application/json";
List<String> consumes = new ArrayList<>(getConsumesInfo(openAPI, operation));
String contentTypeValue = consumes.isEmpty() ? defaultContentType : consumes.get(0);
if (contentTypeValue.equals("*/*"))

View File

@@ -835,7 +835,7 @@ public class TypeScriptClientCodegen extends AbstractTypeScriptClientCodegen imp
} else if (ModelUtils.isObjectSchema(schema)) {
fullPrefix += "{";
closeChars = "}";
CodegenDiscriminator disc = createDiscriminator(modelName, schema, openAPI);
CodegenDiscriminator disc = createDiscriminator(modelName, schema);
if (disc != null) {
MappedModel mm = getDiscriminatorMappedModel(disc);
if (mm != null) {

View File

@@ -63,6 +63,20 @@ export class HttpException extends Error {
*/
export type RequestBody = undefined | string | FormData | URLSearchParams;
function ensureAbsoluteUrl(url: string) {
if (url.startsWith("http://") || url.startsWith("https://")) {
return url;
}
{{#platforms}}
{{#node}}
throw new Error("You need to define an absolute base url for the server.");
{{/node}}
{{^node}}
return window.location.origin + url;
{{/node}}
{{/platforms}}
}
/**
* Represents an HTTP request context
*/
@@ -83,7 +97,7 @@ export class RequestContext {
* @param httpMethod http method
*/
public constructor(url: string, private httpMethod: HttpMethod) {
this.url = new URL(url);
this.url = new URL(ensureAbsoluteUrl(url));
}
/*
@@ -101,7 +115,7 @@ export class RequestContext {
*
*/
public setUrl(url: string) {
this.url = new URL(url);
this.url = new URL(ensureAbsoluteUrl(url));
}
/**

View File

@@ -102,9 +102,10 @@ public class DefaultCodegenTest {
openAPI.getComponents().addSchemas("Pet", new ObjectSchema());
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
Assert.assertFalse(codegen.hasBodyParameter(openAPI, pingOperation));
Assert.assertTrue(codegen.hasBodyParameter(openAPI, createOperation));
Assert.assertFalse(codegen.hasBodyParameter(pingOperation));
Assert.assertTrue(codegen.hasBodyParameter(createOperation));
}
@Test(expectedExceptions = RuntimeException.class)

View File

@@ -13,7 +13,7 @@
<packaging>pom</packaging>
<name>openapi-generator-project</name>
<!-- RELEASE_VERSION -->
<version>7.0.0-beta</version>
<version>7.0.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<url>https://github.com/openapitools/openapi-generator</url>
<scm>

View File

@@ -33,6 +33,13 @@ export class HttpException extends Error {
*/
export type RequestBody = undefined | string | FormData | URLSearchParams;
function ensureAbsoluteUrl(url: string) {
if (url.startsWith("http://") || url.startsWith("https://")) {
return url;
}
return window.location.origin + url;
}
/**
* Represents an HTTP request context
*/
@@ -48,7 +55,7 @@ export class RequestContext {
* @param httpMethod http method
*/
public constructor(url: string, private httpMethod: HttpMethod) {
this.url = new URL(url);
this.url = new URL(ensureAbsoluteUrl(url));
}
/*
@@ -66,7 +73,7 @@ export class RequestContext {
*
*/
public setUrl(url: string) {
this.url = new URL(url);
this.url = new URL(ensureAbsoluteUrl(url));
}
/**

View File

@@ -33,6 +33,13 @@ export class HttpException extends Error {
*/
export type RequestBody = undefined | string | FormData | URLSearchParams;
function ensureAbsoluteUrl(url: string) {
if (url.startsWith("http://") || url.startsWith("https://")) {
return url;
}
return window.location.origin + url;
}
/**
* Represents an HTTP request context
*/
@@ -48,7 +55,7 @@ export class RequestContext {
* @param httpMethod http method
*/
public constructor(url: string, private httpMethod: HttpMethod) {
this.url = new URL(url);
this.url = new URL(ensureAbsoluteUrl(url));
}
/*
@@ -66,7 +73,7 @@ export class RequestContext {
*
*/
public setUrl(url: string) {
this.url = new URL(url);
this.url = new URL(ensureAbsoluteUrl(url));
}
/**

View File

@@ -33,6 +33,13 @@ export class HttpException extends Error {
*/
export type RequestBody = undefined | string | FormData | URLSearchParams;
function ensureAbsoluteUrl(url: string) {
if (url.startsWith("http://") || url.startsWith("https://")) {
return url;
}
return window.location.origin + url;
}
/**
* Represents an HTTP request context
*/
@@ -48,7 +55,7 @@ export class RequestContext {
* @param httpMethod http method
*/
public constructor(url: string, private httpMethod: HttpMethod) {
this.url = new URL(url);
this.url = new URL(ensureAbsoluteUrl(url));
}
/*
@@ -66,7 +73,7 @@ export class RequestContext {
*
*/
public setUrl(url: string) {
this.url = new URL(url);
this.url = new URL(ensureAbsoluteUrl(url));
}
/**

View File

@@ -41,6 +41,13 @@ export class HttpException extends Error {
*/
export type RequestBody = undefined | string | FormData | URLSearchParams;
function ensureAbsoluteUrl(url: string) {
if (url.startsWith("http://") || url.startsWith("https://")) {
return url;
}
throw new Error("You need to define an absolute base url for the server.");
}
/**
* Represents an HTTP request context
*/
@@ -57,7 +64,7 @@ export class RequestContext {
* @param httpMethod http method
*/
public constructor(url: string, private httpMethod: HttpMethod) {
this.url = new URL(url);
this.url = new URL(ensureAbsoluteUrl(url));
}
/*
@@ -75,7 +82,7 @@ export class RequestContext {
*
*/
public setUrl(url: string) {
this.url = new URL(url);
this.url = new URL(ensureAbsoluteUrl(url));
}
/**

View File

@@ -32,6 +32,13 @@ export class HttpException extends Error {
*/
export type RequestBody = undefined | string | FormData | URLSearchParams;
function ensureAbsoluteUrl(url: string) {
if (url.startsWith("http://") || url.startsWith("https://")) {
return url;
}
return window.location.origin + url;
}
/**
* Represents an HTTP request context
*/
@@ -47,7 +54,7 @@ export class RequestContext {
* @param httpMethod http method
*/
public constructor(url: string, private httpMethod: HttpMethod) {
this.url = new URL(url);
this.url = new URL(ensureAbsoluteUrl(url));
}
/*
@@ -65,7 +72,7 @@ export class RequestContext {
*
*/
public setUrl(url: string) {
this.url = new URL(url);
this.url = new URL(ensureAbsoluteUrl(url));
}
/**

View File

@@ -41,6 +41,13 @@ export class HttpException extends Error {
*/
export type RequestBody = undefined | string | FormData | URLSearchParams;
function ensureAbsoluteUrl(url: string) {
if (url.startsWith("http://") || url.startsWith("https://")) {
return url;
}
throw new Error("You need to define an absolute base url for the server.");
}
/**
* Represents an HTTP request context
*/
@@ -57,7 +64,7 @@ export class RequestContext {
* @param httpMethod http method
*/
public constructor(url: string, private httpMethod: HttpMethod) {
this.url = new URL(url);
this.url = new URL(ensureAbsoluteUrl(url));
}
/*
@@ -75,7 +82,7 @@ export class RequestContext {
*
*/
public setUrl(url: string) {
this.url = new URL(url);
this.url = new URL(ensureAbsoluteUrl(url));
}
/**

View File

@@ -33,6 +33,13 @@ export class HttpException extends Error {
*/
export type RequestBody = undefined | string | FormData | URLSearchParams;
function ensureAbsoluteUrl(url: string) {
if (url.startsWith("http://") || url.startsWith("https://")) {
return url;
}
return window.location.origin + url;
}
/**
* Represents an HTTP request context
*/
@@ -48,7 +55,7 @@ export class RequestContext {
* @param httpMethod http method
*/
public constructor(url: string, private httpMethod: HttpMethod) {
this.url = new URL(url);
this.url = new URL(ensureAbsoluteUrl(url));
}
/*
@@ -66,7 +73,7 @@ export class RequestContext {
*
*/
public setUrl(url: string) {
this.url = new URL(url);
this.url = new URL(ensureAbsoluteUrl(url));
}
/**

View File

@@ -41,6 +41,13 @@ export class HttpException extends Error {
*/
export type RequestBody = undefined | string | FormData | URLSearchParams;
function ensureAbsoluteUrl(url: string) {
if (url.startsWith("http://") || url.startsWith("https://")) {
return url;
}
throw new Error("You need to define an absolute base url for the server.");
}
/**
* Represents an HTTP request context
*/
@@ -57,7 +64,7 @@ export class RequestContext {
* @param httpMethod http method
*/
public constructor(url: string, private httpMethod: HttpMethod) {
this.url = new URL(url);
this.url = new URL(ensureAbsoluteUrl(url));
}
/*
@@ -75,7 +82,7 @@ export class RequestContext {
*
*/
public setUrl(url: string) {
this.url = new URL(url);
this.url = new URL(ensureAbsoluteUrl(url));
}
/**

View File

@@ -0,0 +1,19 @@
import { expect } from '@esm-bundle/chai';
import { ServerConfiguration, HttpMethod } from 'ts-petstore-client'
describe("ServerConfiguration", () => {
it("supports absolute http URLs", async () => {
const config = new ServerConfiguration("http://localhost/v2", {});
expect(config.makeRequestContext("/resource", HttpMethod.PUT).getUrl()).to.equal("http://localhost/v2/resource");
})
it("supports absolute https URLs", async () => {
const config = new ServerConfiguration("https://localhost/v2", {});
expect(config.makeRequestContext("/resource", HttpMethod.PUT).getUrl()).to.equal("https://localhost/v2/resource");
})
it("supports relative URLs", async () => {
const config = new ServerConfiguration("/api", {});
expect(config.makeRequestContext("/resource", HttpMethod.PUT).getUrl()).to.equal("http://localhost:8080/api/resource");
})
})

View File

@@ -4,6 +4,7 @@ export default {
files: "./dist/*.test.js",
nodeResolve: true,
manual: false,
port: 8080,
browsers: [
puppeteerLauncher(),
],

View File

@@ -2,7 +2,7 @@ import "reflect-metadata";
import { Container } from "inversify";
import * as petstore from "ts-petstore-client";
import * as petstoreInternals from "ts-petstore-client/dist/apis/PetApi";
import * as petstoreInternals from "../../../builds/inversify/dist/apis/PetApi";
import { expect, assert } from "chai";
import * as fs from "fs";
@@ -20,7 +20,7 @@ describe("ApiServiceBinder", () => {
});
it("binds server config", async () => {
const url = "foobar";
const url = "http://foobar";
let callCount = 0;
const mockServer = {
makeRequestContext(endpoint: string, httpMethod: petstore.HttpMethod): petstore.RequestContext {
@@ -45,7 +45,7 @@ describe("ApiServiceBinder", () => {
});
it("binds server config to url", async () => {
const url = "foobar";
const url = "http://foobar";
const petId = 42;
apiServiceBinder.bindServerConfigurationToURL(url);