Skip to content

Commit

Permalink
Add test for form parameter handling based on requestType
Browse files Browse the repository at this point in the history
  • Loading branch information
Philzen committed May 29, 2024
1 parent 5413054 commit 3229727
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import org.testng.annotations.Test;
import org.junit.jupiter.api.Assertions;


import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -4945,7 +4944,13 @@ public static class fromParameter {
DefaultCodegen codegen = new DefaultCodegen();

@BeforeTest void setOpenAPI() {
codegen.setOpenAPI(new OpenAPI().components(new Components().addSchemas("SampleRequestInput", new ObjectSchema().addProperty("FormProp1", new StringSchema()))));
codegen.setOpenAPI(new OpenAPI().components(new Components().addSchemas(
"SampleRequestInput",
new ObjectSchema().addProperty(
"FormProp1",
new StringSchema()
)
)));
}

@Test public void resolvesReferencedSchema() {
Expand All @@ -4960,11 +4965,58 @@ public static class fromParameter {
.returns("Object", CodegenProperty::getDataType)
.returns(null, CodegenProperty::getRef)
.returns(true, CodegenProperty::getHasVars)

.extracting(CodegenProperty::getVars).asList().hasSize(1)
.first(type(CodegenProperty.class))
.returns("FormProp1", CodegenProperty::getName)
.returns("String", CodegenProperty::getDataType);
}
}

@SuppressWarnings("NewClassNamingConvention")
public static class fromOperation {

DefaultCodegen codegen = new DefaultCodegen();

@BeforeTest void setOpenAPI() {
codegen.setOpenAPI(new OpenAPI().components(new Components().addSchemas("SampleRequestInput", new ObjectSchema().addProperty("FormProp1", new StringSchema()))));
}


@Test public void addsFormParametersFromRequestProperties_forFormContentType() {
var operation = new Operation()
.parameters(List.of())
.operationId("foobar")
.requestBody(new RequestBody().content(new Content().addMediaType(
"application/x-www-form-urlencoded",
new MediaType().schema(new Schema<>().$ref("SampleRequestInput"))
)));

CodegenOperation result = codegen.fromOperation("/", "GET", operation, List.of());

assertThat(result.bodyParams).isEmpty();
assertThat(result.formParams).hasSize(1).first()
.hasFieldOrPropertyWithValue("baseName", "FormProp1")
.hasFieldOrPropertyWithValue("dataType", "String")
.hasFieldOrPropertyWithValue("paramName", "formProp1");
}

@Test public void doesNotAddFormParametersFromRequestProperties_forJsonContentType() {
var operation = new Operation()
.parameters(List.of())
.operationId("foobar")
.requestBody(new RequestBody().content(new Content().addMediaType(
"application/json",
new MediaType().schema(new Schema<>().$ref("SampleRequestInput"))
)));

CodegenOperation result = codegen.fromOperation("", "GET", operation, List.of());

assertThat(result.formParams).isEmpty();
assertThat(result.bodyParams).hasSize(1).first()
.hasFieldOrPropertyWithValue("baseName", "SampleRequestInput")
.hasFieldOrPropertyWithValue("dataType", "SampleRequestInput")
.hasFieldOrPropertyWithValue("paramName", "sampleRequestInput");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.openapitools.codegen.languages;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.*;
import io.swagger.v3.oas.models.parameters.RequestBody;
import org.assertj.core.api.Assertions;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.GeneratorLanguage;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class RustAxumServerCodegenTest {

@Test public void generatorLanguageIsRust() {
Assertions.assertThat(new RustAxumServerCodegen().generatorLanguage())
.isSameAs(GeneratorLanguage.RUST);
}

@SuppressWarnings("NewClassNamingConvention")
public static class fromOperation {

RustAxumServerCodegen codegen = new RustAxumServerCodegen();

@BeforeTest void setOpenAPI() {
codegen.setOpenAPI(new OpenAPI().components(new Components().addSchemas("SampleRequestInput", new ObjectSchema().addProperty("FormProp1", new StringSchema()))));
}


@Test public void addsFormParametersFromRequestProperties_forFormContentType() {
var operation = new Operation()
.parameters(List.of())
.operationId("foobar")
.requestBody(new RequestBody().content(new Content().addMediaType(
"application/x-www-form-urlencoded",
new MediaType().schema(new Schema<>().$ref("SampleRequestInput"))
)));

CodegenOperation result = codegen.fromOperation("/", "GET", operation, List.of());

assertThat(result.formParams).isEmpty();
assertThat(result.bodyParams).hasSize(1).first()
.hasFieldOrPropertyWithValue("baseName", "SampleRequestInput")
.hasFieldOrPropertyWithValue("dataType", "models::SampleRequestInput")
.hasFieldOrPropertyWithValue("paramName", "sample_request_input");
}

@Test public void doesNotAddFormParametersFromRequestProperties_forJsonContentType() {
var operation = new Operation()
.parameters(List.of())
.operationId("foobar")
.requestBody(new RequestBody().content(new Content().addMediaType(
"application/json",
new MediaType().schema(new Schema<>().$ref("SampleRequestInput"))
)));

CodegenOperation result = codegen.fromOperation("", "GET", operation, List.of());

assertThat(result.formParams).isEmpty();
assertThat(result.bodyParams).hasSize(1).first()
.hasFieldOrPropertyWithValue("baseName", "SampleRequestInput")
.hasFieldOrPropertyWithValue("dataType", "models::SampleRequestInput")
.hasFieldOrPropertyWithValue("paramName", "sample_request_input");
}
}
}

0 comments on commit 3229727

Please sign in to comment.